【发布时间】:2013-04-29 03:22:00
【问题描述】:
当我的tableView 未处于编辑模式时,我不希望用户能够触摸单元格并使其突出显示。但是,因为我已经将allowsSelectionDuringEditing 设置为YES,所以用户可以在编辑模式下选择单元格。
如何仅在编辑模式下显示单元格突出显示的视图或颜色?
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview
当我的tableView 未处于编辑模式时,我不希望用户能够触摸单元格并使其突出显示。但是,因为我已经将allowsSelectionDuringEditing 设置为YES,所以用户可以在编辑模式下选择单元格。
如何仅在编辑模式下显示单元格突出显示的视图或颜色?
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview
有趣的场景,幸好就这么简单:
// -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row.
// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return self.isEditing;
}
正如您从 Apple 的评论中看到的,shouldHighlight 是选择过程的第一步,因此在表格被编辑的情况下,这是停止它的地方。
【讨论】:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
//YOUR inits
}
if(self.editing){
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
}else
[cell setSelectionStyle:UITableViewCellEditingStyleBlue];
return cell;
}
和
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(self.editing)return; //NO ACTION
}
【讨论】:
cellForRowAtIndexPath 不会再次被调用并且单元格不会被重绘,所以他们不知道他们应该这样做在编辑模式下被触摸时显示颜色。
reloading桌子怎么样?
我想通了。这是我设置tableView编辑模式的方法:
- (void)tableViewEdit {
if (self.tableView.editing) {
[self.editButton setTitle:NSLocalizedString(@"Edit", nil) forState:UIControlStateNormal];
self.tableView.allowsSelection = NO;
} else {
[self.editButton setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
self.tableView.allowsSelection = YES;
}
[self.tableView setEditing:!self.tableView.editing animated:YES];
}//end
我之前也将self.tableView.allowsSelection设置为默认NO,所以只有进入编辑模式后才会是YES。
【讨论】: