Wes*_*ton 2 uitableview ios ios6
我已阅读了不少帖子,但没有人回答这个问题.我创建了一个填充播放器名称列表的tableview.当用户点击名称时,我希望单元格的背景颜色变为红色,黄色或绿色,具体取决于它们在循环中的位置.我不是要改变selectionStyle颜色.背景颜色的改变将表明每个玩家在游戏中接收的问题有多难.这就是我到目前为止所尝试的.当我调试时,我可以看到didSelectRowAtIndexPath被击中,但没有任何变化.有任何想法吗?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Player Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor greenColor];
UILabel *playerName = (UILabel *)[cell viewWithTag:PLAYER_NAME];
playerName.backgroundColor = [UIColor redColor];
}
Run Code Online (Sandbox Code Playgroud)
尝试:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
UILabel *playerName = (UILabel *)[cell viewWithTag:PLAYER_NAME];
playerName.backgroundColor = [UIColor redColor];
}
Run Code Online (Sandbox Code Playgroud)
这应该有效,因为你只会要求一个可见的单元格.该表将返回已经可见的单元格.
您还需要更新数据源中的某个状态变量,因此如果此单元格滚动然后再返回视图,您将知道再次使用正确的颜色绘制单元格.
| 归档时间: |
|
| 查看次数: |
4230 次 |
| 最近记录: |