Wednesday, 19 June 2013

Table View With Section (Grouped Table View)

For grouped tableview fallow the following steps...

1. Create new Project.
2. Drag a uitableview from object library.
3. Change the style of uitableview from plain(default) to grouped from selecting the attribute inspector..
4. Make the chenges in .h file


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray* arrPhysics;
    NSMutableArray* arrMathematics;
    NSMutableArray* arrHeader;
}
@property (strong, nonatomic) IBOutlet UITableView *tblWithSections;

5.Make the changes in .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSDateFormatter* format = [[NSDateFormatter alloc]init];
    NSString* strDate = @"20/06/2013";
    [format setDateFormat:@"dd.MM.yyyy"];
    NSLog(@"%@", [format dateFromString:strDate]);
    
    
    arrHeader = [[NSMutableArray alloc]initWithObjects:@"Physics",@"Math", nil];
    arrPhysics = [[NSMutableArray alloc]initWithObjects:@"Amit",@"Sudheer", nil];
    arrMathematics = [[NSMutableArray alloc]initWithObjects:@"Kiran",@"Vijay", nil];
    
// Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arrHeader count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0)
    {
        return [arrPhysics count];
    }
    else if(section==1)
    {
        return [arrMathematics count];
    }
    else
        return 0;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [arrHeader objectAtIndex:section];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdent;
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    if(indexPath.section==0)
    {
        cell.textLabel.text = [arrPhysics objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [arrMathematics objectAtIndex:indexPath.row];
    }
    return cell;
}

6. Make sure the neccessary connection like UITableViewDelegate and UITableview Datasource.

Tuesday, 18 June 2013

Date formatter in iOS (NSDateFormatter) OR Reset the date formatter


-(void)currentDateWithRequiredFormatter
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    
    // Current Date with default formatter..
    
    NSLog(@"Current Date with default Formatter = %@",[NSString stringWithFormat:@"%@",[NSDate date]]);
    
    // Set Date format as per your requirment..
    
    [formatter setDateFormat:@"dd/MM/yyyy"];
    
    // Set Current date to which you want...
    
    [formatter stringFromDate:[NSDate date]];
    
    NSLog(@"Current Date after using date Formatter = %@",[formatter stringFromDate:[NSDate date]]);
}

Out Put :

Current Date with default Formatter = 2013-06-19 04:43:35 +0000

Current Date after using date Formatter = 19/06/2013




// Second scenario
    
    // Suppose you have date as a string like..
     NSString* strGettingDate = @"24/05/2013";//dd/MM/yyyy
    
    NSLog(@"Without formatter : %@",strGettingDate);
    
    // And you want to 05.24.2013 // MM.dd.yyyy
    
    [formatter setDateFormat:@"MM.dd.yyyy"];
    [formatter dateFromString:strGettingDate];
    NSLog(@"Using date Formatter = %@",[formatter dateFromString:strGettingDate]);







Out Put :

Without formatter : 24/05/2013
Using after using date Formatter = (null)





 NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

    // So solve this issue you have to set date format in which format you are getting first like..
    
    // Suppose you have date as a string like..
    NSString* strGettingDate = @"24/05/2013";//dd/MM/yyyy
    
     NSLog(@"Without formatter : %@",strGettingDate);
    
    [formatter setDateFormat:@"dd/MM/yyyy"];
    NSDate* dtDate = [formatter dateFromString:strGettingDate];
    
    // Now change the date format in which you want like..
    [formatter setDateFormat:@"MM.dd.yyyy"];
     NSString* strRequiredDate = [formatter stringFromDate:dtDate];
    
     NSLog(@"Using date Formatter : %@",strRequiredDate);

OUT PUT : 

 Without formatter : 24/05/2013
 Using date Formatter : 05.24.2013



You can also go for :
http://www.youtube.com/watch?v=p1FC9T7YdKI


Friday, 7 June 2013

UIAlertView in iOS,Iphone


Declare  UIAlertView in .h file like following...

UIAlertView * alert;

-(void)alertProgress
{
    alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [alert addSubview:activity];
    activity.center = CGPointMake(140,80);
    activity.color = [UIColor whiteColor];
    [activity startAnimating];
    [alert show];
}
-(void)stopAlertProgress
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}