自定义UITableViewCell,UITableView和允许MultipleSelectionDuringEditing
我有一个问题,使用iOS 5新功能在编辑模式下选择多个单元格。 应用结构如下:自定义UITableViewCell,UITableView和允许MultipleSelectionDuringEditing
-> 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创建的。为了达到该解决方案我已经按照这两个计算器主题:
- 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];
}
希望它能帮助。