ios - 如何在声明函数之外访问单元格属性?

标签 ios swift xcode uitableview

我创建了一个返回单元格的 tableView。我希望能够在 dequeReusableCells 函数之外更改单元格属性。我试过这个:

let index = IndexPath(row: 1, section: 0)
let refCell = tableView.cellForRow(at: index)
refCell?.textLabel?.textColor = .white

但它只将第二个单元格的 textLabel 颜色更改为白色。我怎样才能对所有单元格执行此操作(除了使用 for 循环)?

最佳答案

为了改变你应该创建的所有 UITableViewCells

fileprivate var myColor: UIColor = .black

UIViewController

然后在 cellForRowAt indexPath

cell.textLabel?.textColor = self.myColor

然后每当你想改变这个颜色就做

self.myColor = .white
self.myTableView.reloadData()

关于ios - 如何在声明函数之外访问单元格属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094877/

相关文章:

ios - 在 Swift 中使用 UIAlertViewController 时无法从 shouldPerformSegue 返回控制

svn - 在 Xcode 中使用 Subversion

ios - MasterDetail 模板中的属性和 ivars

ios - Swift 4 UIButton 附加在 View 上

iphone - 通用应用程序将作为iPhone应用程序出现在应用程序商店中

ios - UISearchController 使 Controller 变黑

ios - Swift iOS 意图扩展 : The data couldn't be read because it isn't in the correct format

ios - UIImageView 在自定义 UICollectionViewCell 中返回 nil

ios - 为什么 UIImageView 不填满整个 CollectionViewCell 即使我有所有的约束

iphone - 操作系统版本中的“位置”(地理定位)支持