我有静态表格,我设置了tableview -> tableView.backgroundColor = UIColor.redColor()的背景色,当我有3行静态表格,然后是表视图彩色和3行是白色,我如何为这个单元格固定背景颜色?此代码不适用于静态表->
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = numbers[indexPath.row]
cell.backgroundColor = UIColor.greenColor()
return cell
}发布于 2015-09-21 00:05:25
在静态表视图中,您不必实现cellForRowAtIndexPath。因此,我建议您实现以下代码:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Change the color of all cells
cell.backgroundColor = UIColor.greenColor()
}发布于 2015-10-13 19:37:51
请不要在这种情况下使用cellForRowAtIndexPath方法,因为正如您所说的,您使用的是静态表视图。
现在来看解决方案。今天我刚刚经历了同样的问题,场景几乎和你的一样。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor .grayColor()
}还有一件事,我只想告诉你,根据苹果的文档,所有单元格的默认颜色都是透明/白色的。因此,如果您尝试通过接口生成器或cellForRowAtIndexPath方法设置颜色,如果您使用的是iOS 7/ iOS 8/ iOS 9,则默认情况下它将返回白色。因此,不要在cellForRowAtIndexPath方法中声明,而是使用上面的代码。它会工作得很好。
谢谢
希望这能帮上忙。
发布于 2015-04-28 01:16:00
首先,确保每个单元格的背景色在UI模式中都设置为默认值。在检查器属性下。然后在代码集中
cell.backgroundcolor = UIColor.clearcolor()干杯萨姆
https://stackoverflow.com/questions/29569246
复制相似问题