像下面 UITableView 中实现复数选择的设置,需要用到 Undocumented API。
首先,如下所示,在实现了 UITableViewDelegate 的类中实现下面的方法。
1 2 3 4 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return 3; } |
其中 UITableViewCellEditingStyle 的定义在 SDK 中如下所示:
1 2 3 4 5 | typedef enum { UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert } UITableViewCellEditingStyle; |
也就是说,我们使用了私有的TableView风格。另外取得被选择的item可以使用 indexPathsForSelectedRows 方法,该方法也也是私有的。
1 | - (NSArray *)indexPathsForSelectedRows; |
另外,我们也可以使用公开的方法来一个一个的选择/解除cell,而不用使用私有API。
1 2 3 4 | - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath; |