在表格单元格中处理SWIFT中的索引超出范围

时间:2019-11-15 00:35:04

标签: swift

语言:Swift,REALM,使用表视图单元格显示用户条目。

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

这是我的问题-当您在堆栈视图中创建新的图像视图(如您在代码中看到的那样),以显示列表中的第二个图像时,我得到了错误:索引路径已超出范围。另外,我无法解包journalAspects.inPictures [0] .realmToThumbNailImage(),因为我收到一条错误消息,指出它不是可选的。

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

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

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

这是我想在表格视图单元格中看到的内容

  • 仅包含文本和日期的第一个单元格
  • 带有文本,日期和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.

1 个答案:

答案 0 :(得分:2)

您是否检查过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