控制表视图中部分UITableViewCell不可选

首先保证:

1 self.tableView.allowsSelection = YES;  // 默认是 YES
2 self.tableView.allowsSelectionDuringEditing = YES;

UITableView中的声明:

1 @property(nonatomic) BOOL allowsSelection __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  // default is YES. Controls whether rows can be selected when not in editing mode
2 @property(nonatomic) BOOL allowsSelectionDuringEditing;                                     // default is NO. Controls whether rows can be selected when in editing mode

===========================================================================================

对于具体的UITableViewCell在不同时期的可选性单独控制,假设根据 [Edit/Done] editButtonItem 控件分别控制cell0,cell1:

实现 UITableView Delegate 中的 tableView:willSelectRowAtIndexPath: 方法

 1 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 2 {
 3     NSInteger row = indexPath.row;
 4     BOOL editing = self.editing;
 5     
 6     if ( (editing && row == 0) || (!editing && row == 1) ) {
 7         [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
 8         return nil;
 9     }
10     return indexPath;
11 }

参考:UITableView Setting some cells as “unselectable”

posted @ 2012-11-25 17:06  SubmarineX  阅读(1737)  评论(0编辑  收藏  举报