首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在静态表格swift Xcode6中设置单元格的背景色

如何在静态表格swift Xcode6中设置单元格的背景色
EN

Stack Overflow用户
提问于 2015-04-11 03:23:54
回答 7查看 8.4K关注 0票数 3

我有静态表格,我设置了tableview -> tableView.backgroundColor = UIColor.redColor()的背景色,当我有3行静态表格,然后是表视图彩色和3行是白色,我如何为这个单元格固定背景颜色?此代码不适用于静态表->

代码语言:javascript
复制
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
}
EN

回答 7

Stack Overflow用户

发布于 2015-09-21 00:05:25

在静态表视图中,您不必实现cellForRowAtIndexPath。因此,我建议您实现以下代码:

代码语言:javascript
复制
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   // Change the color of all cells     
   cell.backgroundColor = UIColor.greenColor()
}
票数 6
EN

Stack Overflow用户

发布于 2015-10-13 19:37:51

请不要在这种情况下使用cellForRowAtIndexPath方法,因为正如您所说的,您使用的是静态表视图。

现在来看解决方案。今天我刚刚经历了同样的问题,场景几乎和你的一样。

代码语言:javascript
复制
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  cell.backgroundColor = UIColor .grayColor()
}

还有一件事,我只想告诉你,根据苹果的文档,所有单元格的默认颜色都是透明/白色的。因此,如果您尝试通过接口生成器或cellForRowAtIndexPath方法设置颜色,如果您使用的是iOS 7/ iOS 8/ iOS 9,则默认情况下它将返回白色。因此,不要在cellForRowAtIndexPath方法中声明,而是使用上面的代码。它会工作得很好。

谢谢

希望这能帮上忙。

票数 2
EN

Stack Overflow用户

发布于 2015-04-28 01:16:00

首先,确保每个单元格的背景色在UI模式中都设置为默认值。在检查器属性下。然后在代码集中

代码语言:javascript
复制
cell.backgroundcolor = UIColor.clearcolor()

干杯萨姆

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

https://stackoverflow.com/questions/29569246

复制
相关文章

相似问题

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