如何在更改时更新tableview单元格

时间:2016-12-10 16:31:05

标签: swift uitableview parse-platform parse-server

当我向上和向下滚动时,tableview单元格已设置超时,因此会影响滞后。

我想仅在数据更新时更新。

Post.swift(模型)

import Foundation
import Parse

class Post: PFObject, PFSubclassing {

    @NSManaged var postBy: PFUser?

    @NSManaged var postUser: String?
    @NSManaged var postUserImg: PFFile?



    @NSManaged var postText: String?
    @NSManaged var postImg: PFFile?
    @NSManaged var sell: NSNumber?

    @NSManaged var commentPointer: PFObject?
    @NSManaged var commentBy: String?
    @NSManaged var comment: String?

    @NSManaged var liked: NSArray?
    @NSManaged var likeCount: NSNumber?

    @NSManaged var mention: NSArray?
    @NSManaged var hashtag: NSArray?


    static func parseClassName() -> String {
        return "Post"
    }



    override class func query() -> PFQuery<PFObject>? {

        let query = PFQuery(className: Post.parseClassName())


        return query
    }

}

extension Post: FeedCellSupport {


    var username:String?{


        return postUser
    }

    var userImg:PFFile?{



        return postUserImg
    }

    var commentText:String?{

        guard let commentTxt = comment else {
            return ""
        }


        return commentTxt


    }

TableView.swift

protocol FeedCellSupport {
    var postDate: String? { get }
    var postId: String? { get }
    var postObj: PFObject? { get }
    var img: PFFile? { get }
    var username:String?{ get }
    var userImg:PFFile?{ get }

    var commentText:String?{ get }
    var commentFrom:String?{ get }

    var postText: String? { get }
    var likes: Int? { get }
    var isLiked: Bool? { get }
    var isSell: Bool? { get }

    var hashtags: NSArray? { get }
    var mentions: NSArray? { get }

}


func fetchObjects() {

        let postQuery = Post.query()?
                        .whereKeyExists("objectId")
                        .includeKeys(["postBy", "commentPointer", "commentPointer.commentBy", "commentBy"])
                        .order(byDescending: "createdAt")
                        as! PFQuery<Post>

        postQuery.limit = self.page
        postQuery.cachePolicy = .networkElseCache

        postQuery.findObjectsInBackground { (object:[Post]?, error:Error?) in

            if error == nil {

                self.results = object!

            }else {
                print(error?.localizedDescription as Any)
            }

            self.tableView.reloadData()
            self.refresher.endRefreshing()
        }


    }


 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ShopDetailCell", for: indexPath) as! ShopDetailCell

        if let object = results[indexPath.row] as? FeedCellSupport{




            cell.postID = object.postObj!
            cell.userNameLabel.text = object.username!
            cell.userNameLabel.sizeToFit()

            cell.descriptionLabel.text = object.postText!
            cell.descriptionLabel.sizeToFit()

            cell.commentByLabel.text = object.commentFrom!
            cell.commentByLabel.sizeToFit()

            cell.commentLabel.text = object.commentText!
            cell.commentLabel.sizeToFit()
 cell.delegate = self

        }
        return cell
    }

如何确保数据仅在更改时才更新?

1 个答案:

答案 0 :(得分:0)

您的应用功能是什么?您的问题与适合完成任务的方式相关,即:更新应用内容。

所以关于业务逻辑:什么时候应该更新信息?

您的问题的解决方案是使用用户切换事件来刷新内容或其他可能的解决方案,例如应用程序代表

除了这些答案外,内容无法自动更新tableview

<强>更新

我认为这行代码滞后:

if let object = results[indexPath.row] as? FeedCellSupport{
    //other code
}

因为这些大括号内的代码正在处理数据设置。您可以尝试模拟本地数据而不是此results数组来说明这是否是问题。