自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing [英] Custom UITableViewCell, UITableView and allowsMultipleSelectionDuringEditing
问题描述
使用 iOS 5 新功能在编辑模式下选择多个单元格时出现问题。
申请结构如下:
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
其中 UIViewController
是委托和 UITableView
的数据源(我使用的是 UIViewController
而不是 UITableViewController
因需求原因而无法更改)。单元格加载到 UITableView
中,如下面的代码。
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;
}
已从xib文件创建单元格界面。特别是,我创建了一个新的xib文件,其中superview包含一个 UITableViewCell
元素。为了提供自定义,我将该元素的类型设置为 CustomUITableViewCell
,其中 CustomUITableViewCell
extends UITableViewCell
。
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
代码效果很好。在表格中显示自定义单元格。现在,在应用程序执行期间,我将 allowsMultipleSelectionDuringEditing
设置为 YES
,然后进入的编辑模式的UITableView
。看起来很有效。实际上,每个单元格旁边都会出现一个空圆圈。问题是,当我选择一行时,空圆圈不会改变其状态。理论上,圆圈必须从空变为红色复选标记,反之亦然。似乎圆圈仍然在单元格的 contentView
之上。
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.
我不太清楚的是,当我尝试在没有自定义单元格的情况下工作时,仅使用 UITableViewCell
,圆圈正确更新其状态。
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.
推荐答案
对于那些感兴趣的人,我找到了解决上一个问题的有效解决方案。
问题是当选择样式为 UITableViewCellSelectionStyleNone
编辑模式下的红色复选标记无法正确显示。解决方案是创建一个自定义 UITableViewCell
并ovverride一些方法。我正在使用 awakeFromNib
,因为我的单元格是通过xib创建的。为了达到解决方案,我遵循了以下两个stackoverflow主题:
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:
- multi-select-table-view-cell-and-no-selection-style
- uitableviewcell-how-to-prevent-blue -selection-background-wo-borking-isselected
- 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];
}
希望有所帮助。
这篇关于自定义UITableViewCell,UITableView和allowsMultipleSelectionDuringEditing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!