你已经知道,表格单元可以通过accessoryType属性,显示一个对号标记的附件。当用户选中一个单元格时,表格代理的didSelectRowAtIndexPath方法会被调用。这个方法是UITableViewDelegate协议的一部分。在你的代理中加入这个方法,并且对需要的单元格设置对号附件,就可以为你的表格增加对多选的支持:
- - (void)tableView:(UITableView *)tableView
- didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSLog(@'Selected section %d, cell %d',
- [ indexPath indexAtPosition: 0], [indexPath indexAtPosition: 1 ]);
- /* 得到选中的表格单元的指针 */
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath ];
- /* 切换附件的类型 */
- if (cell.accessoryType == UITableViewCellAccessoryNone)
- cell.accessoryType = UITableViewCellAccessoryCheckmark;
- else
- cell.accessoryType = UITableViewCellAccessoryNone;
- }