ios - 滚动 UITableView 时的值变化

标签 ios swift

我有一个带有按钮的 UITableView,该按钮根据用户是否“收藏”帖子进行切换。一切正常,除了滚动表格 View 时,按钮会发生变化。这是我的代码:

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]()

表格 View

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!        
}  

最佳答案

在您的 UITableViewCell PostCellImage 子类中,您应该覆盖 prepeareForReuse 函数 - 将单元格转换为默认模式。

swift :

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial state here 
    //set like button to initial state - title, font, color, etc.
}

关于ios - 滚动 UITableView 时的值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402313/

相关文章:

ios - Xcode SpriteKit LevelBuilder

objective-c - 了解 objc_setAssociatedObject 中的 UnsafeRawPointer

ios - 在 UIScrollView 中,具有视觉格式的 NSLayoutConstraints 无法按预期工作?

swift - Swift 中 float 的前导零

ios - 如何将 iPhone 音频路由到蓝牙扬声器

ios - KVO不适用于com.alpha之类的键路径。

ios - 在用帧 0,0,0,0 初始化的 Storyboard 中创建的 ScrollView

ios - 隐式展开可选值并在 null 时使用它的值

ios - 执行 Segue 在 iOS 10 中崩溃

ios - 推送通知 : How can I send push notification to a specific device using Pubnub?