views:

45

answers:

1

I have a UITableView where every cell either gets/loses a checkbox or goes to another table view when clicked. For every table, I want to add a check all button that checks off every cell in that table and all the ones inside it. How do I allow one UITableViewCell to toggle the checkbox of every one in it's TableView?

EDIT: Removing the last index in the index path, and replacing it by the new row number, I'm able to go through the entire table and check it off, like this:

for(int i = 0; i < [tableView numberOfRowsInSection:0]; i++){
                UITableViewCell *newCell = [tableView cellForRowAtIndexPath:[[indexPath indexPathByRemovingLastIndex]
                                                                             indexPathByAddingIndex: i]];
                newCell.accessoryType = UITableViewCellAccessoryNone;
            }

Oddly, it skips over every tenth cell or so - and it's not even consistent. For a list of years, sometimes it doesn't check off 2003 and 1993, sometimes 2003 and 1991, and and so on. It's always around every tenth cell or so, but I can't find a pattern.

+1  A: 

When you click the cell didSelectRowAtIndexPath will be called, at that point all you gotta do is use tableView cellForRowAtIndexPath:indexPath method in order to get back all your cells one by one and set their accessory type to checkmark...hope this helps

EDIT: Another way to do it is, if you have maybe some sort of array (hard to tell you what to do exactly without looking at your code), but if you have some array that holds values as to which cells need have check marks on them, then you can use in cellForRowAtIndexPath to assign the checkmark accessory, so when your cell is selected, you can set all the values in the array to what you need them to be and just call UITableViews reloadData...hope that made sence :), heres a little snipet of code to try and make it more clear lets assume you have 1 section and x amount of rows

       NSMutableArray shouldHaveCheckmark; //this array will be of size x 
//(one for each row) and hold NSNUmbers 0 means not checked 1 means checked you initialize 
//this somewhere (viewDidLoad maybe) and should be declared in your .h file 
    //this is cellForRowAtIndex path
    -(UITableViewCell)cellForRowAtIndexPath:(NSIndexPath*)path
    {
        UITableViewCell *cell=...
        if([[shouldHaveCheckmark objectAtIndex:path.row] boolValue] )
           //assign checkmark accesory
    //other code 
     return cell;
    }

So you can see in didSelectRowAtIndexPath you can just flip the numbers in the array and call reloadData on the tableView...that approach will work as well

Daniel
Thanks, but how do I find the index path for each of the cells. I tried using:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];from 0 to the last table row, but that causes the application to crash when I go back to a previous table.
ike
the index paths of the cells you need to edit you should probably know, or you can store them somewhere...I dont know why you app is crashing you might be doing somethign wrong, if you post some code i can look at it and tell you if i see anything that could be causing the crash
Daniel
another way to do it is menitoned on my edit
Daniel