简体   繁体   English

Swift 3 UITableViewCell indexPath.row搞砸了

[英]Swift 3 UITableViewCell indexPath.row messing up

I have a TableView to show bunch of movies in. movies is the array of Movie objects. 我有一个TableView中显示一堆的电影。 movies是电影对象的数组。 movieIDs is the array of movie ids. movieIDs是电影ID的数组。 Ids are just strings. id只是字符串。

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

        // editing the cell here.

        cell.movieNameLabel.text = movies[indexPath.row].movieName
        cell.movieYearLabel.text = movies[indexPath.row].year

        // source of all hell here.

        for id in movieIDs {

            if id == movies[indexPath.row].movieID {

                print(id + " is equal to " + movies[indexPath.row].movieID)
                cell.myButton.setImage(/*there is an image here*/), for: .normal)

            }

        }

The for loop in cellForRowAt method: cellForRowAt方法中的for循环:

for id in movieIDs {

        if id == movies[indexPath.row].movieID {

            print(id + " is equal to " + movies[indexPath.row].movieID)
            cell.myButton.setImage(//there is an image here), for: .normal)
        }

    }

I am comparing all the ids in the movieIDs to the id of the movie at the cell, which is movies[indexPath.row].movieID . 我正在将movieIDs中的所有ID与该单元格处的电影ID进行比较,该电影是movie movies[indexPath.row].movieID If it returns true, i replace the image of the button inside the cell. 如果返回true,则替换单元格内按钮的图像。 When i print inside the if statement, it actually doesn't execute but it still replaces button images at random cells. 当我在if语句中打印时,它实际上不会执行,但仍会替换随机单元格上的按钮图像。 And if i scroll too fast up and down, images of the buttons gets replaced in almost all cells, when its only intended to change for cells that ids match. 而且,如果我上下滚动太快,则按钮的图像将在几乎所有单元格中被替换,而只是为了更改ID匹配的单元格而已。

The reason that the cells are stuffed up is because they are reusable cells. 填充单元格的原因是因为它们是可重复使用的单元格。

So for example, if you have cell #1 set up with an image, when you scroll down and that cell #1 goes off the screen and becomes cell #10 (for instance), it is still showing the image. 因此,例如,如果您为单元格1设置了图像,则向下滚动时该单元格1离开屏幕并变为单元格10(例如),它仍在显示图像。

The solution is you have to remove the image that was previously set by checking if it doesn't match the movieID , set the image to nil . 解决方案是您必须通过检查先前设置的图像是否与movieID不匹配来删除该图像,并将其设置为nil

You don't have to do the for loop here, instead you use the contains for array. 您不必在这里进行for循环,而可以使用contains for数组。 So replace this code: 因此,替换此代码:

for id in movieIDs {

    if id == movies[indexPath.row].movieID {

        print(id + " is equal to " + movies[indexPath.row].movieID)
        cell.myButton.setImage(//there is an image here), for: .normal)
    }

}

with this: 有了这个:

if movieIDs.contains(movies[indexPath.row].movieID) {
    cell.myButton.setImage(//there is an image here), for: .normal)
}
else{
    cell.myButton.setImage(nil)
}

You have to set nil if no id matches: 如果没有id匹配,则必须设置nil

var matched = false
for id in movieIDs {

    if id == movies[indexPath.row].movieID {

        print(id + " is equal to " + movies[indexPath.row].movieID)
        cell.myButton.setImage(//there is an image here), for: .normal)
        matched = true
    }

}

if !matched {
    cell.myButton.setImage(nil)
}

For a better solution, you should create a function to get the image: 为了获得更好的解决方案,您应该创建一个函数来获取图像:

if let image = getMovieImageByID(movies[indexPath.row].movieID) {
    cell.myButton.setImage(image), for: .normal)
} else {
    cell.myButton.setImage(nil), for: .normal)
}

func getMovieImageByID(movieID: String) -> UIImage? {
    for id in movieIDs {
        if id == movieID {
            // return the image for the respective movieID
        }
    }

    return nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM