【问题标题】:Cannot assign to 'accessoryType' in UITableViewCell无法分配给 UITableViewCell 中的“accessoryType”
【发布时间】: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)
}

【问题讨论】:

    标签: ios uitableview swift xcode6.1


    【解决方案1】:

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

    试试下面...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多