ios - 将多个自定义 TableViewCell Xib 索引到 TableView 中

标签 ios swift xcode uitableview indexpath

我正在创建一个 TableView,它从顶部的图像、大约 5 个已解析的 json 单元格、另一个图像、大约 3 个已解析的 json 单元格和另一个图像开始。

我正在使用 3 个自定义 Nib 。每一件都是独一无二的。

我在“let item = page?.collapsibles[indexForSecondSetTableViewCells]”行上收到“索引超出范围”错误

这是我的代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let currentIndex = indexPath.row - 1
    let numberofCarousels = self.page?.carousels.count
    let indexAfterCarousels = numberofCarousels ?? 00 + 1
    let indexForSecondSetTableViewCells = indexAfterCarousels + 1

    if indexPath.row == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "pipesOne", for: indexPath) as! Pipes1TableViewCell

        cell.pipesOne.image = UIImage(named: "WhatsNew1")

        return cell
    }

    if indexPath.row == indexAfterCarousels {

        let cell = tableView.dequeueReusableCell(withIdentifier: "pipesOne", for: indexPath) as! Pipes1TableViewCell

        cell.pipesOne.image = UIImage(named: "WhatsNew2")

        return cell
    }

    if indexPath.row == indexForSecondSetTableViewCells {

        let cell = tableView.dequeueReusableCell(withIdentifier: "collapsibleCell", for: indexPath) as! CollapsiblesTableViewCell

        let item = page?.collabsible[indexForSecondSetTableViewCells]

        cell.collabsibleTitle.text = item?.title
        cell.collabsibleDescription.text = item?.content

        return cell
    }

    let cell = tableView.dequeueReusableCell(withIdentifier: "newsTableViewCell", for: indexPath) as! NewsTableViewCell


    let item = page?.carousels[currentIndex]

    cell.newsTitle.text = item?.title
    cell.newsText.text = item?.caption
    cell.newsImage.kf.setImage(with: page?.carousels[currentIndex].imageURL)

    return cell

}

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (page?.carousels.count ?? 0) + 1 + (page?.collapsibles.count ?? 0)
}

更新:我简化了代码以使用相同的 json 对象两次,以便每个项目准确地映射到它应该转到的单元格,但我仍然在第 7 行收到“ fatal error :索引超出范围”错误让 item = page?.collapsible[indexPath.row]

新代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "pipesOne", for: indexPath) as! Pipes1TableViewCell

        cell.pipesOne.image = UIImage(named: "WhatsNew1")

        return cell
    }

    if indexPath.row == 1 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "newsTableViewCell", for: indexPath) as! NewsTableViewCell

        let item = page?.carousels[indexPath.row]

        cell.newsTitle.text = item?.title
        cell.newsText.text = item?.caption
        cell.newsImage.kf.setImage(with: page?.carousels[indexPath.row].imageURL)

        return cell
    }

    if indexPath.row == 6 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "pipesOne", for: indexPath) as! Pipes1TableViewCell

        cell.pipesOne.image = UIImage(named: "WhatsNew2")

        return cell
    }

    if indexPath.row == 7 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "collapsibleCell", for: indexPath) as! CollapsiblesTableViewCell

        let item = page?.collapsibles[indexPath.row]

        cell.collabsibleTitle.text = item?.title
        cell.collabsibleDescription.text = item?.content


        return cell
    }
    return UITableViewCell()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 11
}

最佳答案

尝试print("\(indexPath)")

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("\(indexPath)")
}

然后你就会知道在崩溃之前它成功显示了多少行。 然后在cellForRowAt设置条件断点,这样当indexPath是它崩溃的地方时它就会停止。 现在做po (page?.carousels.count ?? 0) + 1 + (page?.collapsibles.count ?? 0) 查看您的表格 View 有多少行。绝对是indexForSecondSetTableViewCells大于行数

关于ios - 将多个自定义 TableViewCell Xib 索引到 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58754547/

相关文章:

ios - 在 Storyboard中,我可以将自定义文本放在 "featured"选项卡栏图标下吗?

ios - iOS 中的字母间距

swift - 稍后如何从配置文件中删除 HealthKit

ios - swift 将 var 从一个 if 语句传递到另一个

Swift:如何从 Assets 中获取图像名称

iphone - 如果方向不是问题的话自动布局?

ios - cell.selected 不起作用

ios - 根据自动布局更改UILabel中的字体大小(快速)

ios - 需要用按钮 Swift 生成的 Int 更改 UIImageView

ios - 如何使导航 Controller 导航栏不可见或至少改变颜色