ios - 在展开可选值访问 tableView Cell 时意外发现 nil

标签 ios swift tableview

我想从外部 tableView func 访问 tableView 的单元格, 例如,在这种情况下,我想从 IBAction Func 访问,然后我创建了 tableView 对象和 IndexPath 对象,当我运行我的项目时,Xcode 向我显示此错误:

Fatal error: unexpectedly found nil while unwrapping an Optional values

对于这一行:mytableView!.cellForRowAtIndexPath(indexPath!)

@IBAction func editButtonFunc(sender: AnyObject) {

    mytableView!.cellForRowAtIndexPath(indexPath!)
    let cell =
    mytableView!.dequeueReusableCellWithIdentifier(
        "mycell", forIndexPath: indexPath!)
        as! profileTableViewCell
    cell.contentOutlet.text = contentItems[indexPath!.row]
    cell.contentOutlet.textAlignment = NSTextAlignment.Right
    cell.contentOutlet.font = UIFont(name: "X Yekan", size: 18)

}

最佳答案

如果你只想获取某个单元格的引用,也许你需要:

func getTableViewCell() -> UITableViewCell? {
    guard let tableView = mytableView, index = indexPath 
    else { return nil }

    return tableView.cellForRowAtIndexPath(index)
}

而且,如果您想为 tableViewCell 设置数据,您应该在 UITableViewDataSource 方法中执行此操作:cellForRowAtIndexPath .

关于ios - 在展开可选值访问 tableView Cell 时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33495199/

相关文章:

iOS 11 设备方向自动旋转

ios - 在 iOS 中访问自定义 UITableViewCell 内的 View

iphone - IOS 7 TableView 内容大小问题

ios - cordova build ios 不创建 ipa 文件

ios - Swift:生成一个(Swift)字符数组

ios - 使用编码器在应用程序关闭之外快速保存 View Controller 状态

ios - 如何使用 GKStateMachine 的状态传递参数

swift - 无法为 'Darwin' 加载底层模块

ios - 实体关系中的数据属性更改但不触发 nsfetchresultsController

ios - IBDesignable 的自定义背景颜色属性不会根据目标进行更新