自定义UITableViewCell,UITableView并允许MultipleSelectionDuringEditing
我在使用iOS 5新function在编辑模式下select多个单元时遇到问题。 应用程序结构如下:
-> 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文件创build。 特别是,我创build了一个新的xib文件,其中superview由一个UITableViewCell
元素组成。 为了提供定制,我将该元素的types设置为CustomUITableViewCell
,其中CustomUITableViewCell
扩展了UITableViewCell
。
@interface CustomTableViewCell : UITableViewCell // do stuff here @end
代码运行良好。 在表格中显示自定义单元格。 现在,在应用程序执行期间,我将allowsMultipleSelectionDuringEditing
设置为YES
并进入UITableView
的编辑模式。 看来工作。 事实上,每个单元格旁边都会出现一个空的圆圈。 问题是,当我select一个行时,空的圆形不会改变它的状态。 从理论上说,圆圈必须从空白转为红色,反之亦然。 看来这个圆圈仍然在contentView
之上。
我做了很多实验。 我也检查了下面的方法。
- (NSArray *)indexPathsForSelectedRows
并在编辑模式下进行select时更新。 它显示正确的选定单元格。
我不清楚的是,当我尝试没有自定义单元格的工作,只有与UITableViewCell
,圆圈正确地更新其状态。
你有什么build议吗? 先谢谢你。
对于那些感兴趣的,我find了一个有效的解决scheme来解决以前的问题。 问题是,当select风格是UITableViewCellSelectionStyleNone
编辑模式下的红色复选标记显示不正确。 解决的办法是创build一个自定义的UITableViewCell
和ovverride的一些方法。 我正在使用awakeFromNib
因为我的单元格是通过xib创build的。 为了达到解决scheme,我遵循了这两个stackoverflow主题:
- 多选表-视图-细胞和无select风格
- UITableViewCell的知识对防止蓝色select背景-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]; }
希望能帮助到你。