开发者

How can I set all cells in a tableView as checked or unchecked?

I have a checklist in a UITableView and I have a UISegmentedControl with "Select All" and "Deselect All" options.

I am wondering how I can rese开发者_如何学Got all the cells while viewing the table. The [self.tableView reloadData]; function does not seem to do the trick.


You could keep a NSMutableArray* called checkmarks that stores NSNumber instances.

Set +numberWithBool: on each element of checkmarks as YES, for example, to mark all rows as checked.

Note that this is just a data model store. Thus, you have to actually read the values of checkmarks in your -tableView:cellForRowAtIndexPath: method to set the checkmark states for each row's accessoryType, e.g.:

cell.accessoryType = ([[checkmarks objectAtIndex:indexPath.row] boolValue] == YES) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

When you programmatically change the state of checkmarks, you can then call [tableView reloadData] so that the state of table view rows reflects the state of elements of checkmarks.


reloadData will just set up table cells with what you return from tableView:cellForRowAtIndexPath:. If you return a checked cell, it'll show up checked. UITableView doesn't maintain any sort of "checked" or "unchecked" state itself.

It does have "selected" and "unselected" states, but, according to Apple's HIG, you shouldn't be using that to indicate a selection. Those states are for the highlight you get when you've tapped a row, and only one row of a table should ever be "selected". Instead, you should use the accessory view or some custom subview of the cell to display a checkmark.

After you dequeue a table cell in tableView:cellForRowAtIndexPath:, make sure to explicitly set the accessory view to either UITableViewCellAccessoryCheckmark or UITableViewCellAccessoryNone. Otherwise, it will have whatever accessory view it had when it was last displayed.


try this:

    for(UITableViewCell* cell in tableAlert.tableView.visibleCells){
        cell.accessoryType = UITableViewCellAccessoryChecked;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜