我有一个视图控制器,它有一个UITableViewController作为其主视图.在函数 - tableView:cellForRowAtIndexPath中:我使用以下代码为我的单元格创建自定义视图.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundView = [[UIView alloc] init];
Run Code Online (Sandbox Code Playgroud)
然后我将自己的自定义视图添加到cell.backgroundView.但是,当我点击一个单元格时,单元格会变成灰色.我的代码中没有指定这应该发生的地方.为了进行一些测试,我将一些文本添加到单元格的标准部分,如下所示.
cell.textLabel.text = @"TEST";
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我看到了我的自定义视图和"TEST"标签.按下一个单元格时,出现灰色屏幕,此时"TEST"可见,我的自定义视图仍然不可见.我的猜测是必须采取其他措施来确保自定义视图可见.我正在使用tableView:didSelectRowAtIndexPath:函数来注册抽头,但奇怪的是,除第一个之外的所有抽头事件都会调用此函数.另外,我试图在这里设置背景颜色,但它不起作用.
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.backgroundColor = [UIColor whiteColor];
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
在tableView:cellForRowAtIndexPath:改变selectionStyle每个单元格的属性UITableViewCellSelectionStyleNone.
| 归档时间: |
|
| 查看次数: |
3054 次 |
| 最近记录: |