views:

38

answers:

2

hello everyone

I only know the first step is to check a row

next I think is use a NSMutableArray to record which row is been checked

and this is my code :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark){

                [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
            }

            else {

                [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

        }
    }

so,my question is

<1>How to put this checked row's indexPath into a Array ?

<2>How to add a delete button that I can delete all the row I selected ?

thanks ,and also thanks the god let the typhoon come to here and give me one more holiday !

A: 

First off declare a NSMutableArray in youre viewController's .h file.

Like so:

NSMutableArray *checkedRows;

Change youre didSelectRow method like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([checkedRows containsObject:indexPath]) {
        [checkedRows removeObject:indexPath];
    }
    else {
        [checkedRows addObject:indexpath];
    }
    [self.tableView beginUpdates]; //Just for animation..
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

And in youre cellForRow method add this:

if ([checkedRows containsObject:indexPath]){
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
Larsaronen
Thank you I'll try this method now !
WebberLai
soory,I got null when I Log checkedRows ...?
WebberLai
Sorry i didnt notice the second part of the question.. This will only handle checking the rows..The reason checkedRows is null is probaly because you didnt allocate it in viewDidLoad.. Like: checkedRows = [[NSMutableArray alloc] init];also release it in dealloc [checkedRows release];Do something like TechZen recomended..
Larsaronen
OK,I can get a checkedRows = [ s, r],s means Section,r means Rows.
WebberLai
+1  A: 

You are going about this wrong.

You don't handle the editing accessory views yourself. Instead, you send setEditing:animated: to the table and it alters the cells for you. Then you need to implement:

– tableView:commitEditingStyle:forRowAtIndexPath: 
– tableView:canEditRowAtIndexPath:

... to actually remove the row cells.

TechZen