Wednesday 18 December 2013

UITableView in Delete rows with animation and checkmark in Iphone

Table.h

@interface ip12ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *dataArr;
}

Table.m

int checkFlags[]  = {0,0,0};

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataArr = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Strawberry", @"Mango", nil];
   
   
    NSDictionary *myDict0 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Apple", @"Fruit", @"iPhone", @"Device", nil];
   
    NSLog(@"%@", [myDict0 objectForKey:@"Device"]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   
    cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];
   
    //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
   
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
   
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
   
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   
    //cell.accessoryType = UITableViewCellAccessoryNone;
   
    if(checkFlags[indexPath.row] == 1)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
   
    return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [dataArr removeObjectAtIndex:indexPath.row];
   
    [tableView beginUpdates];
   
    [tableView deleteRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
   
    [tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    checkFlags[indexPath.row] = !checkFlags[indexPath.row];
   
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
}

No comments:

Post a Comment

Comment