Wednesday, 18 April 2012

adding labels in cell programatically

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    //}
   
    // Configure the cell...
    //codee=[[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:7];
    //NSLog(@"FFF--%@",codee);
   
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 2.0, 170.0, 20.0)];
    lbl.font=[UIFont boldSystemFontOfSize:13.0];
    lbl.textColor = [UIColor blackColor];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.highlightedTextColor = [UIColor whiteColor];
    lbl.text = [[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:2];
    [cell.contentView addSubview:lbl];
   
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(170, 2.0, 120.0, 20.0)];
    lbl.font=[UIFont boldSystemFontOfSize:12.0];
    lbl.textAlignment = UITextAlignmentRight;
    lbl.textColor = [UIColor blackColor];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.highlightedTextColor = [UIColor whiteColor];
    lbl.text =[NSString stringWithFormat:@"%.2f %@", [[[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:4] doubleValue],[[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:5]];
    [cell.contentView addSubview:lbl];
   
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 20.0, 228.0, 20.0)];
    lbl.font=[UIFont systemFontOfSize:10.0];
    lbl.textAlignment = UITextAlignmentLeft;
    lbl.textColor = [UIColor colorWithRed:57.0/255.0 green:122.0/255.0 blue:159.0/255.0 alpha:1.0];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.highlightedTextColor = [UIColor whiteColor];
    lbl.text =[NSString stringWithFormat:@"%@, %@", [[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:0],[[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:3]];
    [cell.contentView addSubview:lbl];
   
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(230.0, 20.0, 60.0, 20.0)];
    lbl.font=[UIFont systemFontOfSize:10.0];
    lbl.textAlignment = UITextAlignmentRight;
    lbl.textColor = [UIColor grayColor];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.highlightedTextColor = [UIColor whiteColor];
    //lbl.text =[NSString stringWithFormat:@"%@", [[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:0]];
    [cell.contentView addSubview:lbl];
   
    NSArray *Arr1 = [[[ArrPOList objectAtIndex:indexPath.row] objectAtIndex:6] componentsSeparatedByString:@"-"];
    lbl.text = [NSString stringWithFormat:@"%@/%@/%@",[Arr1 objectAtIndex:2],[Arr1 objectAtIndex:1],[Arr1 objectAtIndex:0]];
   
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor=[UIColor clearColor];
    return cell;
}

Converting date formats and comparing

 NSString *newlyFormattedDateString,*newlyFormattedDateString1;
    if([[NSUserDefaults standardUserDefaults]objectForKey:@"datetoken"]){
        NSString *date1=[[NSUserDefaults standardUserDefaults]objectForKey:@"datetoken"];
   
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM-dd-yyyy"];
        NSDate* datef = [dateFormatter dateFromString:date1];
       
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        newlyFormattedDateString = [dateFormatter stringFromDate:datef];
        [dateFormatter release], dateFormatter = nil;
        NSLog(@"datef-%@", newlyFormattedDateString);
       
      
    }
 
    if([[NSUserDefaults standardUserDefaults]objectForKey:@"tokenDate"]){
        NSString *date2=[[NSUserDefaults standardUserDefaults]objectForKey:@"tokenDate"];
    
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM-dd-yyyy"];
        NSDate* datet = [dateFormatter dateFromString:date2];
       
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        newlyFormattedDateString1 = [dateFormatter stringFromDate:datet];
        [dateFormatter release], dateFormatter = nil;
        NSLog(@"datet-%@", newlyFormattedDateString1);
       
    }
      
   
   
   
   
   
   
   
    //NSLog(@"Fromdate--%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"datetoken"]);
   //  NSDate * date1 = [[NSDate alloc] dateFromString:[[NSUserDefaults standardUserDefaults]objectForKey:@"datetoken"]];
    //NSLog(@"Todate--%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"tokenDate"]);
   // NSDate * date2 = [[NSDate alloc] initWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"tokenDate"]];
   int j=0;
    NSComparisonResult result = [newlyFormattedDateString compare:newlyFormattedDateString1];
   
    switch (result)
    {
        case NSOrderedAscending: NSLog(@"%@ is in future from %@", newlyFormattedDateString1, newlyFormattedDateString); break;
        case NSOrderedDescending: NSLog(@"%@ is in past from %@", newlyFormattedDateString1, newlyFormattedDateString);
            j=1;
            break;
        case NSOrderedSame: NSLog(@"%@ is the same as %@", newlyFormattedDateString1, newlyFormattedDateString); break;
        default: NSLog(@"erorr dates %@, %@", newlyFormattedDateString1, newlyFormattedDateString); break;
    }

    if(j==1){
       
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Oops"
                                                          message:@"The 'To date' should be larger than 'From date'"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
       
        [message show];
        [message  release];
    }else {

MailComposer (Email) in IOS

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
// in .h of the view controller

//call 

-(void) sendMail{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        if ([mailClass canSendMail])
        {
            MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
            controller.mailComposeDelegate = self;
            //Subject of the mail
            NSString *sub = self.navigationItem.title;
            [controller setSubject:sub];
            NSArray *toRecipients = [NSArray arrayWithObject:[[ArrPOList objectAtIndex:0]objectAtIndex:6]];
            [controller setToRecipients:toRecipients];
            //Body of the mail
            //body = [NSString stringWithFormat:@"Wooow"];
            NSString *body=[[NSUserDefaults standardUserDefaults]objectForKey:@"Signature"];
            [controller setMessageBody:body isHTML:NO];
            [self presentModalViewController:controller animated:YES];
            [controller release];
           
        }
        else
            [self launchMailAppOnDevice];
    }
    else
        [self launchMailAppOnDevice];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }
    [self becomeFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
   
}

-(void)launchMailAppOnDevice
{
    NSString *recipients = [[ArrPOList objectAtIndex:0]objectAtIndex:6];
    NSString *body=[[NSUserDefaults standardUserDefaults]objectForKey:@"Signature"];
//    NSUserDefaults *Defaults = [NSUserDefaults standardUserDefaults];
//    [Defaults setObject:body forKey:@"Signature"];
   
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
   
   /* NSString *recipients = @"post@iratedphrase.com";
    NSString *body = data.text;
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];*/
}

Message composer code (SMS).


//

 MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        if (picker == nil) {
            //[self ShowMsg"Message can be composed only in iPhone device."];
            return;
        }
        picker.messageComposeDelegate = self;
        picker.recipients = [NSArray arrayWithObject:telStr];
        [self presentModalViewController:picker animated:YES];
        [picker release];

Adding an image in Navigation bar in iOS5

Add the code just above the @implementation of rootview controller

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"Header.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

@implementation RootViewController


//Paste the following code wherever you rerequire.

- (void)viewDidLoad {
    [super viewDidLoad];
      
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"Header.png"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
}