无法分配给 UITableViewCell 中的“accessoryType”

Posted

技术标签:

【中文标题】无法分配给 UITableViewCell 中的“accessoryType”【英文标题】:Cannot assign to 'accessoryType' in UITableViewCell 【发布时间】:2014-12-03 04:44:21 【问题描述】:

当一个单元格被选中时,我希望它被选中。我还想要任何其他被检查为没有附件的单元格。不幸的是,我收到一条错误消息,提示我无法更改附件类型。那是因为我使用visibleCells() 来获取单元格吗?如何克服这个错误?

我试过不使用常量;我得到了同样的错误。我查看了 UITableView 和 UITableViewCell 类参考,但没有看到任何可以帮助我的东西。我的备用计划是使用 Picker View,但由于它的设计,我更喜欢使用 Table View。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    let cells = tableView.visibleCells()
    for oldCell in cells 
        if(oldCell.accessoryType == UITableViewCellAccessoryType.Checkmark) 
            oldCell.accessoryType = .None //Cannot assign to 'accessoryType' in 'oldCell'
        
    
    cell!.accessoryType = .Checkmark
    day = cell!.textLabel!.text!
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

【问题讨论】:

【参考方案1】:

查看API documentation,visibleCells() 返回[AnyObject]。由于 Swifts 类型安全,它不会让你在 AnyObject 上设置 accessoryType - 它必须是 UITableViewCell

试试下面...

if let cells = tableView.visibleCells() as? [UITableViewCell] 
  for oldCell in cells 
    if oldCell.accessoryType == .Checkmark 
      oldCell.accessoryType = .None
    
  

【讨论】:

以上是关于无法分配给 UITableViewCell 中的“accessoryType”的主要内容,如果未能解决你的问题,请参考以下文章

无法更改 UITableViewCell 内的 UIButton 图像

将数据分配给 UILabel 后 UITableViewCell 显示不正确的数据

Swift 为 UITableViewCell 分配两个值

无法向 UITableViewCell 添加值

(iOS + Firebase)无法将图像从 UITableViewCell 传递给下一个 ViewController

当动画 UIImage 被分配两次时,UITableViewCell 中的 UIImageView 没有动画