【问题标题】:swift uitableview repeats first cellswift uitableview 重复第一个单元格
【发布时间】:2020-09-14 14:22:57
【问题描述】:

我是 swift 和 iOS 的完整初学者,我正在尝试编写一些代码,这些代码将获取一些 json 并将其放入对象数组中,然后使用该数组来填充 tableview。
问题是当我尝试填充 tableview 时,它只打印第一个,所以它看起来像这样:

这里是代码

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "comicCell", for: indexPath) as! ComicCell

    cell.title.text = comics?[indexPath.row].title
    cell.dateOnSale.text = comics?[indexPath.row].dateOnSale


    return cell
}

我试图在一个简单的 for 循环中打印对象数组,它工作正常,这让我相信我的问题出在上面的函数中

【问题讨论】:

  • 您似乎混淆了numberOfSectionsnumberOfRowsInSection。前者必须返回 1,后者必须返回 comics.count。并且不要将数据源数组声明为可选。
  • @vadian 非常感谢!现在可以使用了

标签: ios swift uitableview swift3


【解决方案1】:

cellForRowAt 方法似乎还可以,我认为您在使用 tableViewnumberOfSectionsnumberOfRowsInSection 方法时遇到了问题,前者必须返回1 并且后者必须在您的情况下返回 comics.count

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多