首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TableViewCell中选择

在TableViewCell中选择
EN

Stack Overflow用户
提问于 2015-05-14 15:39:57
回答 2查看 67关注 0票数 0

在选择tableviewcell时,我正在更改tableviewcell的颜色。当我选择tableViewCell上的单元格并滚动时,其他一些单元格也被选中。

滚动后,另一个单元格也会受到影响

这是我的代码

代码语言:javascript
复制
static NSString *identifier = @"TBDCreateGamePlayerCell";

TBDCreateGamePlayerCell *playerCell = (TBDCreateGamePlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"TBDCreateGamePlayerCell"];

if (!playerCell) {
    NSLog(@"creating a new cell : %d",row);
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TBDCreateGamePlayerCell" owner:nil options:nil];
    playerCell = [nib objectAtIndex:0];


}
EN

回答 2

Stack Overflow用户

发布于 2015-05-14 15:48:27

您必须实现此委托方法:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath

在此方法中,将单元格背景更改回默认颜色。

Apple Docs

讨论

表视图在使用单元格绘制行之前将此消息发送给其代理,从而允许代理在显示单元格对象之前对其进行自定义。此方法使委托有机会重写先前由表视图设置的基于状态的属性,如选择和背景色。委托返回后,表视图仅设置alpha和frame属性,并且仅在滑入或滑出时设置行的动画。

票数 0
EN

Stack Overflow用户

发布于 2015-05-14 17:37:01

请看下面的代码。

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

selected_index= indexPath.row;
[tableView reloadData];

}

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

 UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
 cell.accessoryType=UITableViewCellAccessoryNone;

for (UIView *view in cell.contentView.subviews) {

[view removeFromSuperview];

}

if(indexPath.row==selected_index)

{

cell.contentView.backgroundColor = [UIColor whiteColor];

}

else

{

 cell.contentView.backgroundColor = [UIColor clearColor];

}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30231916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档