UITableView:仅在编辑模式下显示选定的单元格背景

时间:2013-04-29 03:22:01

标签: ios objective-c cocoa-touch uitableview

当我的tableView未处于编辑模式时,我不希望用户能够触摸单元格并让它们突出显示。但是,因为我已将allowsSelectionDuringEditing设置为YES,所以用户可以在编辑模式下选择单元格。

如何仅在编辑模式下显示单元格突出显示的视图或颜色?

3 个答案:

答案 0 :(得分:0)

有趣的场景,幸运的是它就像这样简单:

// -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的评论中看到的那样,高亮是选择过程的第一步,因此在编辑表格的情况下,这是停止它的地方。

答案 1 :(得分:-1)

-(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
}

答案 2 :(得分:-1)

我明白了。这是我设置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。

相关问题