HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用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文件,其中superview包含一个UITableViewCell元素.为了提供自定义,我将该元素的类型设置为CustomUITableViewCell,其中CustomUITableViewCell扩展了UITableViewCell.

@interface CustomTableViewCell : UITableViewCell

// do stuff here

@end

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

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

- (NSArray *)indexPathsForSELEctedRows

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

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

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

解决方法

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

> multi-select-table-view-cell-and-no-selection-style
> 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];

}

希望能帮助到你.

大佬总结

以上是大佬教程为你收集整理的ios – 自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing全部内容,希望文章能够帮你解决ios – 自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。