Xcode 7.3 UITableViewController 单元格无法正确显示

问题描述 投票:0回答:1

我尝试显示一个带有一个标签和一个添加项目按钮的简单单元格。我无法让它正确显示。

这是结果:

没有

add item
按钮,也没有正确的行。我的 coredata 中只有两项。

这是我的代码:

   override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        
        let data = items[indexPath.row] as Item
        
        cell.textLabel?.text = data.body
        
        return cell
    }

存在哪些问题?如何正确显示

add item
按钮、正确的行数以及自定义单元格的高度?

ios swift xcode uitableview autolayout
1个回答
0
投票

首先让我们将正确的行数返回到表视图。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellContent.count
}

接下来让其输出到原型单元。

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

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = cellContent[indexPath.row]

    return cell
}

还可以调用以下视图控制器,以便它可以正常运行。

视图控制器:UIViewController、UITableViewDelegate、UITableViewDataSource

最后一件事,原型的单元格与代码中的单元格具有相同的标识符。在我的代码中,我将确保原型单元格中的标识符是“Cell”,因为我在 UITableViewCell(...reuseIdentifier: "Cell") 中调用了它。 View picture to see where to add the identifier in your storyboard

© www.soinside.com 2019 - 2024. All rights reserved.