我有一个 View Controller ,它有一个 UITableViewController 作为它的主 View 。在函数 - tableView:cellForRowAtIndexPath: 中,我使用以下代码为我的单元格创建自定义 View 。
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];
然后我将我自己的自定义 View 添加到 cell.backgroundView。但是,当我点击一个单元格时,该单元格变得完全灰色。我没有在我的代码中指定应该发生这种情况。为了进行一些测试,我向单元格的标准部分添加了一些文本,如下所示。
cell.textLabel.text = @"TEST";
当我运行它时,我看到了我的自定义 View 和“测试”标签。当按下一个单元格时,出现灰色屏幕,这次“测试”可见,而我的自定义 View 仍然不可见。我的猜测是必须采取一些其他措施来确保自定义 View 可见。我正在使用 tableView:didSelectRowAtIndexPath: 函数来注册点击,但奇怪的是,除了第一个之外的所有点击事件都调用了这个函数。另外,我尝试在这里设置背景颜色,但是没有用。
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.backgroundColor = [UIColor whiteColor];
}
任何帮助表示赞赏。
最佳答案
在 tableView:cellForRowAtIndexPath:
中,将每个单元格上的 selectionStyle
属性更改为 UITableViewCellSelectionStyleNone
。
关于ios - IOS 中表格 View 单元格点击的背景颜色更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26984997/