Ios 在分组的UITableView中设置选定UITableView单元格的背景
这对于我的普通样式表视图很有效,但对于我的分组样式则不行。我正在尝试自定义选定单元格时的外观 这是我的密码:Ios 在分组的UITableView中设置选定UITableView单元格的背景,ios,uitableview,Ios,Uitableview,这对于我的普通样式表视图很有效,但对于我的分组样式则不行。我正在尝试自定义选定单元格时的外观 这是我的密码: + (void)customizeBackgroundForSelectedCell:(UITableViewCell *)cell { UIImage *image = [UIImage imageNamed:@"ipad-list-item-selected.png"]; UIImageView *imageView = [[UIImageView alloc] in
+ (void)customizeBackgroundForSelectedCell:(UITableViewCell *)cell {
UIImage *image = [UIImage imageNamed:@"ipad-list-item-selected.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
cell.selectedBackgroundView = imageView;
}
我已经验证了正确的单元格确实被传递到此函数中。我需要做些什么才能使这项工作正常进行?您的问题不清楚您是否知道tableViewCell根据其选择状态自动管理显示/隐藏其selectedBackgroundView。除了出现在
视图中之外,还有更好的地方可以放置该方法。一个是在最初创建tableViewCells时,即:
- (UITableViewCell *)tableView:(UITV*)tv cellForRowAtIP:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
cell = [tv dequeueCellWithIdentifier:@"SomeIdentifier"];
if (cell == nil) {
cell = /* alloc init the cell with the right reuse identifier*/;
[SomeClass customizeBackgroundForSelectedCell:cell];
}
return cell;
}
您只需在该单元格的生存期内设置selectedBackgroundView属性一次。单元格将在适当时管理显示/隐藏它
另一种更简洁的方法是将UITableViewCell子类化,并在子类的.m文件中覆盖:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithBla....];
if (self) {
UIImageView *selectedBGImageView = /* create your selected image view */;
self.selectedBackgroundView = selectedBGImageView;
}
return self;
}
从那时起,你的细胞应该显示它的自定义选择的背景没有任何进一步的修改。它只是工作
此外,当前推荐的做法是使用以下UITableView方法将表视图单元格类注册到viewdiload:
中的表视图中,这种方法效果更好:
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
您可以在表视图控制器的viewdiload
方法中使用此方法,以便表视图单元格出列实现更短、更易于阅读:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[SomeClass class]
forCellReuseIdentifier:@"Blah"];
}
- (UITableViewCell *)tableView:(UITV*)tv cellForRowAtIP:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Blah"
forIndexPath:indexPath];
/* set your cell properties */
return cell;
}
只要您使用@“Blah”
标识符注册了一个类,该方法就保证返回一个单元格。您在哪里调用该类方法?从viewDidLoad。我刚从ViewWillExample中尝试过,现在可以使用了。你可以写答案,我会把它标对的。谢谢如果您能解释为什么它只在ViewWillExample中工作,我们将不胜感激。