【问题标题】:How do i dequeue a cell in swift?我如何快速出列单元格?
【发布时间】:2017-02-25 00:30:31
【问题描述】:

'NSInternalInconsistencyException',原因:'无法使具有标识符 eventCell 的单元格出列 - 必须为标识符注册一个 nib 或类或连接情节提要中的原型单元格'

这是代码

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {// this sets the title and subtitle to the Title and Location in the given EventPlan
    let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell ", for: indexPath) as UITableViewCell

    cell.textLabel?.text = eventsArray[indexPath.row].title
    cell.detailTextLabel?.text = eventsArray[indexPath.row].location

    return cell        
}


// here's my code for eventplan
class EventPlan : NSObject {
    var title:String
    var location:String

    init(tits: String, locs: String){
        self.title = tits
        self.location = locs
    }       
}

let event = EventPlan(tits: "Wise BDAY", locs: "UVA-Wise")

我知道我应该在我的主故事板上将单元格标识符标记为 eventCell,但我仍然收到错误消息。我连接不正确吗?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    通常您要做的是在情节提要中设置原型单元格,并在情节提要中设置重用标识符。如果你这样做,你不需要任何代码。

    如果您在 nibfile 中定义单元格,请参阅上面 Sandeep 和 Naveed 的答案。 (请注意,您需要在两个答案中编辑“nibName”参数才能使用您的笔尖名称。

    【讨论】:

    • @KunjPatel 如果这解决了您的问题,您应该接受答案。
    【解决方案2】:

    您必须在表格视图中注册单元格。我通常在viewDidLoad 进行。

    // Swift 2
    tableView.registerNib(UINib(nibName: "NIB_NAME", bundle: nil), forCellReuseIdentifier: "eventCell")
    
    // Swift 3
    tableView.register(UINib(nibName: "NIB_NAME", bundle: nil), forCellReuseIdentifier: "eventCell")
    

    【讨论】:

    • 看到我这样做了,但我仍然收到相同的消息
    • @kunj-patel : 你的 xib 名称是 eventCell.xib 吗?在 UINib(nibName: "eventCell", bundle: nil) 你应该指定 xib name not reusable identifier string
    • override func viewDidLoad(){ let nib = UINib(nibName: "eventCell", bundle: nil) self.tableView.register(nib, forCellReuseIdentifier: "eventCell") }
    • 是的,main 中的名称是 eventCell,它是在 custom 下的那个
    • 您的单元格是在自己的nib 中,还是在tableViewController 中的原型单元格?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多