自定义 UITableViewCell、UITableView 并允许在编辑期间进行多重选择

发布于 2025-01-01 15:37:52 字数 1798 浏览 0 评论 0原文

我在使用 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 工作时,圆圈会正确更新其状态。

您有什么建议吗?先感谢您。

I have a problem using iOS 5 new functionality to select multiple cells during editing mode.
The application structure is the following:

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

where UIViewController is both the delegate and the data source for the UITableView (I'm using an UIViewController instead of UITableViewController for requirement reasons and I cannot change it). Cells are loaded into the UITableView like the following code.

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

The cell interface has been created from a xib file. In particular, I've created a new xib file where the superview consists of a UITableViewCell element. To provide customization, I set the type for that element as CustomUITableViewCell, where CustomUITableViewCell extends UITableViewCell.

@interface CustomTableViewCell : UITableViewCell

// do stuff here

@end

The code works well. In the table custom cells are displayed. Now, during application execution I set allowsMultipleSelectionDuringEditing to YES and I enter the edit mode for the UITableView. It seems working. In fact, alongside of each cell an empty circle appears. The problem is that when I select a row the empty circle doesn't change its state. In theory, the circle has to change from empty to red checkmark and viceversa. It seems that circle remains above the contentView for the cell.

I've made a lot of experiments. I've also checked also that following method.

- (NSArray *)indexPathsForSelectedRows

and it gets updated during selection in editing mode. It displays the right selected cells.

The thing that it's not so clear to me is that when I try to work without a custom cell, only with UITableViewCell, the circle updates its state correctly.

Do you have any suggestion? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2025-01-08 15:37:52

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

  1. multi -选择表视图单元格和无选择样式
  2. uitableviewcell-how-to-prevent-blue-selection-background- 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];

}

希望有帮助。

For those interested in, I've found a valid solution to fix the previous problem.
The problem is that when selection style is UITableViewCellSelectionStyleNone red checkmarks in editing mode aren't displayed correctly. The solution is to create a custom UITableViewCell and ovverride some methods. I'm using awakeFromNib because my cell has been created through xib. To reach the solution I've followed these two stackoverflow topics:

  1. multi-select-table-view-cell-and-no-selection-style
  2. uitableviewcell-how-to-prevent-blue-selection-background-w-o-borking-isselected

Here the code:

- (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];

}

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文