自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing

Lor*_*o B 4 uitableview multipleselection ios ios5

我在使用iOS 5新功能在编辑模式下选择多个单元格时遇到问题.应用程序结构如下:

-> UIViewController
---> UITableView
----> CustomUITableViewCell
Run Code Online (Sandbox Code Playgroud)

其中UIViewController是委托和数据源UITableView(我使用UIViewController而不是UITableViewController出于需求原因,我无法更改).将单元格加载到UITableView以下代码中.

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = (CustomTableViewCell*)[tv dequeueReusableCellWithIdentifier:kCellTableIdentifier];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCellXib" owner:self options:nil];     
        cell = self.customTableViewCellOutlet;    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // configure the cell with data
    [self configureCell:cell atIndexPath:indexPath];    

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

已从xib文件创建单元接口.特别是,我创建了一个新的xib文件,其中superview由一个UITableViewCell元素组成.为了提供自定义,我将该元素的类型设置为CustomUITableViewCell,其中CustomUITableViewCellextends UITableViewCell.

@interface CustomTableViewCell : UITableViewCell

// do stuff here

@end
Run Code Online (Sandbox Code Playgroud)

代码效果很好.在表格中显示自定义单元格.现在,在应用程序执行期间,我设置allowsMultipleSelectionDuringEditing为,YES然后进入编辑模式UITableView.看起来很有效.实际上,每个单元格旁边都会出现一个空圆圈.问题是,当我选择一行时,空圆圈不会改变其状态.理论上,圆圈必须从空变为红色复选标记,反之亦然.似乎圆圈仍然高于contentView细胞.

我做了很多实验.我还检查了以下方法.

- (NSArray *)indexPathsForSelectedRows
Run Code Online (Sandbox Code Playgroud)

并且在编辑模式中选择时会更新.它显示正确的选定单元格.

对我来说不太清楚的是,当我尝试在没有自定义单元格的情况下工作时,只有UITableViewCell圆圈才能正常更新其状态.

你有什么建议吗?先感谢您.

Lor*_*o B 5

对于那些感兴趣的人,我找到了解决上一个问题的有效解决方案.问题是当选择样式为UITableViewCellSelectionStyleNone红色时,编辑模式中的复选标记无法正确显示.解决方案是创建一个自定义UITableViewCell和ovverride的一些方法.我正在使用,awakeFromNib因为我的单元格是通过xib创建的.为了达到解决方案,我遵循了以下两个stackoverflow主题:

  1. 多选表 - 视图 - 细胞和无选择风格
  2. UITableViewCell的知识对防止蓝色选择​​背景-WO-borking-isselected

这里的代码:

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_normal"]] autorelease];
    self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_selected"]] autorelease];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected && !self.isEditing)
    {

        return;
    }        

    [super setSelected:selected animated:animated];
}

- (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
{
    // don't highlight
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.