更改标准视图控制器中的表格视图单元格 [英] Changing a table view cell in a standard view controller'
问题描述
我有一个 TableView,其中有一个需要更改背景颜色的单元格.我尝试寻找更改单元格背景颜色的答案,但问题是我找到的每个答案都与覆盖 tableview 函数有关,该函数不起作用,因为我没有 tableviewcontroller,而是一个嵌入了 UITableView 的普通视图控制器.
如何简单地更改单元格的背景颜色?
首先通过故事板给你的表格视图单元一个标识符,如下所示:
然后另外
I have a TableView and in it I have a cell that I need to change the background color of. I've tried searching for an answer to change the background color of the cell but the problem is that every answer I find has to do with an override tableview function, which will not work because I do not have a tableviewcontroller, rather it is a normal view controller with a UITableView embedded.
How can I simply change the background color of the cell?
First give your table view cell an Identifier via the storyboard like this:
Then additionally to vadian's answer access your cell in the tableView(_:cellForRowAt:)
DataSource method like this and update the color, in the example I put the method in an extension:
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.cyan
return cell
}
}
Also don't forget to add the tableView(_:numberOfRowsInSection:)
method to conform the UITableViewDataSource protocol
Like rMickeyD mentioned in the comments, don't forget to set your table view as dataSource
and delegate
with Ctrl + Drag
from the table view to the view controller like this:
这篇关于更改标准视图控制器中的表格视图单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!