我有一个带有按钮的UITableView,根据用户是否“收藏”一个帖子来切换。一切都很好,除了滚动表格视图,按钮改变。这是我的代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let feed = self.feed else {
return 0
}
return feed.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if feed!.count > 9 {
if indexPath.row == feed!.count - 1 {
self.loadMorePosts()
}
}
if hasImageAtIndexPath(indexPath) {
return imageCellAtIndexPath(indexPath)
} else {
return basicCellAtIndexPath(indexPath)
}
}
func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
let post = self.feed?[indexPath.row]
if post?.image?.isEmpty == false {
return true
}
return false
}
func imageCellAtIndexPath(indexPath:NSIndexPath) -> PostCellImage {
let cell:PostCellImage = self.tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! PostCellImage
if let post = self.feed?[indexPath.row] {
let likedPost = post.hasFavorited
if likedPost == true {
if let favoriteCount = post.favoriteCount {
let count = String(favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
} else {
if let favoriteCount = post.favoriteCount {
let count = String(favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
}
}
return cell
}
收藏的帖子数组
var favoritedPosts = [Int]()
表格视图
if let likedPost = post.hasFavorited {
if likedPost == true {
self.favoritedPosts.append(indexPath.row)
print(self.favoritedPosts)
}
}
if self.favoritedPosts.contains(indexPath.row) {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
} else {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
答案 0 :(得分:11)
在UITableViewCell
PostCellImage
子类中,您应该覆盖prepeareForReuse
函数 - 将单元格设置为默认模式。
夫特:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here
//set like button to initial state - title, font, color, etc.
}
答案 1 :(得分:5)
这可能是由表视图单元重用引起的。事实证明,当帖子是收藏的帖子时,你的代码设置了likeButton.image,但是当帖子不是收藏的时候,你的代码没有删除图像。所以当第一次将每个单元格加载到tableView中时,每件事都可以正常工作。但是,当滚动tableView时,当具有喜欢的图像集的那些单元格移出屏幕区域时,它们将被重复用于滚动的单元格。因此,如果这种单元格被甚至不被收藏的帖子重用,则图像仍然会在那里。
UITableViewCell有一个prepareForReuse方法,它让你有机会在重用单元格之前重置内容。
答案 2 :(得分:0)
您可以尝试使用prepareForReuse或尝试在likedPost == false
答案 3 :(得分:0)
使用else
子句中的两个注释掉的行完成外观将解决您的问题。
if self.favoritedPosts.contains(indexPath.row) {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
} else {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
// Uncomment these two lines and add proper values for image / color to resolve your problem
// cell.likeButton.setImage(UIImage(named: "not-liked-yet"), forState: .Normal)
// cell.likeButton.setTitleColor(UIColorFromRGB("A67832"), forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
希望这有帮助。