gpt4 book ai didi

swift - 在tableview单元格中处理SWIFT中的索引越界

转载 作者:行者123 更新时间:2023-12-04 11:00:05 26 4
gpt4 key购买 nike

语言:Swift、REALM、使用 tableview 单元来显示用户条目。

我有一个问题,我无法弄清楚如何在表格 View 单元格中显示图像,其中每个单元格都有不同数量的图像进入,并且当用户保存其条目时,图像本身是可选的。

这是我的问题 - 当我在堆栈 View 中创建一个新的 ImageView 时,如您在代码中看到的那样,要显示列表中的第二个图像,我收到索引路径越界的错误。另外,我无法解开 journalAspects.inPictures[0].realmToThumbNailImage() 因为我收到一个错误,说它不是可选的。

我如何解决在不同单元格中显示不同数量的图像而不使应用程序崩溃的问题。

我的应用中有 3 个用户条目

  • 第一个有文字、日期和没有图像
  • 第二个有文字、日期和 1 张图片
  • 第三个有文字、日期和 2 张图片

  • 这是我想在表格 View 单元格中看到的内容
  • 第一个只有文本和日期的单元格
  • 带有文本、日期和 1 个图像的第二个单元格
  • 第三个带有文本、日期和 2 个图像的单元格

  • 非常感谢,非常感谢您的意见。

    这是我的代码:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let displayCell = journalAspectTableViewDispay.dequeueReusableCell(withIdentifier: "firstAddCell", for: indexPath) as! FirstAddTableViewCell

    if let journalAspects = RealmEntries?[indexPath.row] {

    //MARK: Text display

    displayCell.journalTextDisplayLabel.text = journalAspects.realmText

    let pictureImageView = UIImageView()

    pictureImageView.heightAnchor.constraint(equalToConstant: 70).isActive = true

    pictureImageView.widthAnchor.constraint(equalToConstant: 70).isActive = truedisplayCell.stackViewForImageShow.addArrangedSubview(pictureImageView)

    let secondpictureImageView = UIImageView()

    secondpictureImageView.heightAnchor.constraint(equalToConstant: 70).isActive = true

    secondpictureImageView.widthAnchor.constraint(equalToConstant: 70).isActive = true

    displayCell.stackViewForImageShow.addArrangedSubview(secondpictureImageView)

    if journalAspects.inPictures.count == 0 {
    return displayCell
    } else {
    let imagesComingOut = journalAspects.inPictures[0].realmToThumbNailImage()

    secondpictureImageView.image = imagesComingOut

    let secondimagesComingOut = journalAspects.inPictures[1].realmToThumbNailImage() -- App crashes

    pictureImageView.image = secondimagesComingOut -- App crashes
    }
    }
    return displayCell
    }







    Hi, Thank you for your feedback, It is working, But when I implement it, I am having a strange problem. As I add new entries, the number of rows in the section won't change, and weird things happen. At first as I run and compile the app from the Xcode, everything seems right. Entries with one image has one image, entries with no image has no image and entries with 2 has 2. But as I scroll up and down, suddenly entries with no image gets populated with some image. Even though, I have added a new entry, the number of rows will still return the same. For instance if I have 10 entries at the start by running the compiler on the Xcode, then I add the 11th entry, as I scroll, the table view adds the 11th entry as 10th and knocks out the first entry. No matter how many I add, they just knock one down and add another at the top making the number of rows I see as a constant number since it is compiled. If you have any idea as to why that is happening, Please let me know Thank you again.

    最佳答案

    你检查过inPictures实际上包含 2 个图像?您检查是否大于 0,但不大于 1。这可能是 out of range 的唯一原因。该行中的错误。更安全的方法是:

    switch journalAspects.inPictures.count {
    case 2:
    secondpictureImageView.image = journalAspects.inPictures[0].realmToThumbNailImage()
    pictureImageView.image = journalAspects.inPictures[1].realmToThumbNailImage()
    case 1:
    pictureImageView.image = journalAspects.inPictures[1].realmToThumbNailImage()
    // or maybe the other one - hard to tell as you've mixed up sequencing
    default: break
    }
    return displayCell

    关于swift - 在tableview单元格中处理SWIFT中的索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868648/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com