UITableViewCell 在 Swift 中显示相同的内容

UITableViewCell showing same content in Swift

我正在尝试通过代码在 Swift 中实现自定义单元格。但是单元格的内容没有得到更新。我做错了什么吗?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell: MyOrdersViewControllerLatest! = tableView .dequeueReusableCellWithIdentifier(cellIdentifier) as? MyOrdersViewControllerLatest

    if (cell == nil) {
        cell = MyOrdersViewControllerLatest(style: UITableViewCellStyle.Default, reuseIdentifier:cellIdentifier)
        cell.selectionStyle = UITableViewCellSelectionStyle.None
    }

    cell.itemId = nil
    cell.itemName = nil
    return cell
}

您只需将 itemIditemName 设置为 nil,这意味着没有任何值。您必须将值设置为数据源中的值。例如,如果你有一个数组,你想访问等于行索引的数组元素:

cell.itemId = yourArray[indexPath.row].id
cell.itemName = yourArray[indexPath.row].name
return cell

tableView.registerClass() 解决了问题。