ios - 无法使单元格出队

标签 ios swift uitableview

像这样的 TableView 问题:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellReuseIdentifier = "cell"
        let cell: UITableViewCell = autoCompleteTableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as UITableViewCell
        let index = indexPath.row as Int

        cell.textLabel!.text = tableArray[index]

        return cell


    }

当我运行该应用程序时,我得到:“无法使具有标识符 cell 的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元格”。我在这里做错了什么?

编辑:

我没有为这个 UITableView 使用 Storyboard

最佳答案

如果您不使用 Storyboard,则可以使用您的 cellIdentifier 创建默认的 UITableViewCell:

UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
   cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

此代码尝试查找具有给定标识符的 UITableViewCell。如果不这样做,它会使用给定的标识符创建一个新的 UITableViewCell 并将其添加到 UITableView

关于ios - 无法使单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34432675/

相关文章:

ios - UILabel 中文本之前和之后的行

ios - 如何在 iOS 11 中关闭通过 UITableView 调整大标题?

ios - cell.contentView.viewWithTag 在静态 tableView 中不起作用

ios - 如何访问 UITableViewCell 中的 UIButton 或 UILabel?

html - 背景图像在 iPhone 上显示不正确(居中)

iphone - 数据源中的 KVO 问题

ios - 匿名实例化数组中的 UIButton

swift - 无法覆盖 NSTableViewRow draw() 行为以在 NSOutlineView 中透明

swift - 如何在 swiftUI 中 x 秒后触发操作

iphone - 如何在运行时从 UITableView 添加或删除行?