没有内容的表格视图单元格

Posted

技术标签:

【中文标题】没有内容的表格视图单元格【英文标题】:tableview cell with no content 【发布时间】:2015-12-24 06:15:25 【问题描述】:

我的表格视图正在填充,但单元格上没有内容 当我试图把这条线//cell.textLabel?.text = todoItems[indexPath.row]。我收到此错误check the error

// Cell creation

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

    cell.selectionStyle = .None
    let item = todoItems[indexPath.row]

    cell.delegate = self
    cell.toDoItem = item

    // modify the cell to have check mark
    if (item.completed) 

        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
    
    else 

        cell.accessoryType = UITableViewCellAccessoryType.None
    

    return cell




// Data source

class DataSource: NSObject 

    let itenName: String

    var completed : Bool
    var deadline : NSDate
    var UUID :String

    init(itenName :String , completed :Bool = false , deadline : NSDate , UUID :String  ) 
        self.itenName = itenName
        self.completed = completed
        self.UUID = UUID
        self.deadline = deadline
        //self.subTitle = subTitle
    

【问题讨论】:

【参考方案1】:

你可以发现 todoItems[indexPath.row] 的类是DataSource with

print(todoItems[indexPath.row].classForCoder)

cell.textLabel?.text 的类是String。 所以你分配了错误的类型。你应该这样做:

cell.textLabel?.text = todoItems[indexPath.row].itenName

【讨论】:

请提供代码解释。与现在一样,此回复不是答案,正在审查中删除。 我在我的代码中添加了一个解释。你有更多的 cmets 吗?【参考方案2】:

您分配了错误的类型。你应该这样做:

let item = todoItems[indexPath.row]
cell.textLabel?.text = item.itenName

【讨论】:

【参考方案3】:

你应该这样做

let item = todoItems[indexPath.row] as DataSource
cell.textLabel?.text = item[indexPath.row].itenName

【讨论】:

以上是关于没有内容的表格视图单元格的主要内容,如果未能解决你的问题,请参考以下文章

如何设置表格视图单元格之间的空间并删除背景内容视图的白色

是否在表格视图单元格上选择没有响应

如何根据集合视图中的单元格选择来控制表格视图的内容?

如何更新表格视图单元格?

静态单元格的内容没有出现 UITableView

如何在表格视图行中有多个单元格?