如何在 tableview 单元格中按顺序放置 3 个不同的图像?

How to put 3 different image with order in tableview cell?

我在表格视图单元格中有一个图像视图,并且有 3 个不同的图像。

我正在尝试做什么:

我的手机应该是这样的:

img1
img2
img3
img1
img2
img3
img1
.
.

。 我该怎么做这个订单?抱歉我的英语不好:)

这是我的 cellforrowat

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let jlist = self.joinAllAllowedInnoList, !jlist.isEmpty {
            let cell = tableView.dequeueReusableCell(withIdentifier: "JoinCell", for: indexPath) as! JoinCell

            //--

            cell.delegate = self
            cell.indexPath = indexPath



            //--
            if(indexPath.row % 2 == 0) {
                cell.thubnailImageView.image = UIImage(named:"thumb1")

            }
            else {
                cell.thubnailImageView.image = UIImage(named:"thumb2")
            }

            cell.participationEndDate.text = jlist[indexPath.row].joinEnd
            cell.titleLabel.text = jlist[indexPath.row].shortDesc

            return cell
        }
        else {
            let cell = UITableViewCell()
            cell.selectionStyle = .none
            cell.backgroundColor = .clear
            cell.textLabel?.textAlignment = .center
            cell.textLabel?.textColor = UIColor.black
            cell.textLabel?.text = "nodataavaiable".localized()
            return cell
        }
    }

正确答案是:

let row = indexPath.row + 1
if row % 3 == 0 {
    // Img3
}
if (row + 1) % 3 == 0 {
    // Img2
}
if (row + 2) % 3 == 0 {
    // Img1
}

这是一个简单的逻辑。首先,将你的三张图片放在一个数组中:

let images = [UIImage(named:"thumb1"), UIImage(named:"thumb2"), UIImage(named:"thumb3")]

将其设为视图控制器的 属性。

现在 cellForRowAt 你只需要做:

cell.thubnailImageView.image = images[indexPath.row % 3]

就是这样。


如果你不需要数组,你可以做一些简单的事情:

let imgNum = indexPath.row % 3
if imgNum == 0 {
    // use Img1
} else if imgNum == 1 {
    // use Img2
} else {
    // use Img3
}