【问题标题】:Custom UITableViewCell, UITableView and allowsMultipleSelectionDuringEditing自定义 UITableViewCell、UITableView 和 allowedMultipleSelectionDuringEditing
【发布时间】:2012-02-24 11:40:39
【问题描述】:

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

-> UIViewController
---> UITableView
----> CustomUITableViewCell

其中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;
}

单元界面已从 xib 文件创建。特别是,我创建了一个新的 xib 文件,其中超级视图包含一个 UITableViewCell 元素。为了提供自定义,我将该元素的类型设置为CustomUITableViewCell,其中CustomUITableViewCell 扩展了UITableViewCell

@interface CustomTableViewCell : UITableViewCell

// do stuff here

@end

代码运行良好。在表格中显示自定义单元格。现在,在应用程序执行期间,我将allowsMultipleSelectionDuringEditing 设置为YES,并进入UITableView 的编辑模式。它似乎工作。事实上,每个单元格旁边都会出现一个空圆圈。问题是当我选择一行时,空圆圈不会改变它的状态。理论上,圆圈必须从空变为红色复选标记,反之亦然。圆圈似乎仍位于单元格的 contentView 上方。

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

- (NSArray *)indexPathsForSelectedRows

它会在编辑模式下的选择过程中得到更新。它显示正确的选定单元格。

我不太清楚的是,当我尝试在没有自定义单元格的情况下工作时,仅使用 UITableViewCell,圆圈会正确更新其状态。

你有什么建议吗?提前谢谢你。

【问题讨论】:

    标签: ios uitableview ios5 multipleselection


    【解决方案1】:

    对于那些感兴趣的人,我找到了一个有效的解决方案来解决之前的问题。 The problem is that when selection style is UITableViewCellSelectionStyleNone red checkmarks in editing mode aren't displayed correctly.解决方案是创建一个自定义UITableViewCell 并覆盖一些方法。我正在使用awakeFromNib,因为我的单元格是通过 xib 创建的。为了找到解决方案,我遵循了这两个 stackoverflow 主题:

    1. multi-select-table-view-cell-and-no-selection-style
    2. uitableviewcell-how-to-prevent-blue-selection-background-w-o-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];
    
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2015-12-02
      相关资源
      最近更新 更多