我有一个TableView来显示一堆电影。movies
是Movie对象的数组。 movieIDs
是电影ID的数组。 Ids只是字符串。Swift 3 UITableViewCell indexPath.row搞砸了
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)
}
}
的在cellForRowAt方法循环:
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)
}
}
我在小区,这是movies[indexPath.row].movieID
在movieIDs
所有的ID比较影片的ID。如果它返回true,我替换单元格内的按钮的图像。当我在if语句内部打印时,它实际上不会执行,但它仍然替换随机单元格中的按钮图像。如果我上下滚动速度太快,几乎所有单元格中的按钮图像都会被替换,当它仅用于更改id匹配的单元格时。