parsequery为所有uitableviewcells设置单个图像

时间:2016-10-08 04:27:04

标签: ios swift uitableview

我试图将图像填充到一个单元格中,但出于某种原因,当它不应该加载到其他单元格中时。我不确定是什么原因造成的。我不确定,但是当我快速向下滚动它会加载 在它不应该这样的所有图像视图中:

enter image description here

是否有这样的原因?
我也可能会在图像视图消失后尝试隐藏它,但是现在我将关注图像加载它不应该在哪里。

Parsequery:

func getLocalComments(point:PFGeoPoint){
    var temp = [CommentDetails]()
    DispatchQueue.global(qos: .background).async {
        let qComments = PFQuery(className: "UserCommentary")
        let qDrinks = PFQuery(className: "venueDrinks")
        if self.type == "House Parties"{
            //Query the bars
            qDrinks.whereKey("venueName", equalTo: self.id)
            qDrinks.whereKey("venueID", equalTo: self.venueName)
            qDrinks.addDescendingOrder("createdAt")

            qComments.whereKey("venueID", equalTo: self.id)
            qComments.whereKey("venueName", equalTo: self.venueName)

            do{

                let qReply1 = try qDrinks.findObjects()
                if qReply1.count>0{
                    let comment:CommentDetails = CommentDetails.init(comm: "Test", ven: self.venueName, venId: self.id, owner: PFUser.current()!.username!, vote: 0)
                    var i = 0
                    while i < 2 {
                        let item = qReply1[i]
                        print(item)
                        comment.cellType = 2
                        i+=1
                    }
                    //temp.append(comment)
                }
                let qReply2 = try qComments.findObjects()
                for item in qReply2{
                    let comment:CommentDetails = CommentDetails.init(comm: "Test", ven: self.venueName, venId: self.id, owner: PFUser.current()!.username!, vote: 0)
                    comment.commentOwner = item.object(forKey: "owner") as! String
                    comment.comment = item.object(forKey: "comment") as! String
                    comment.isFile = item.object(forKey: "image") as! Bool

                    if comment.isFile {
                        let dataPhoto:PFFile = item.object(forKey: "imgFile") as! PFFile
                        let imageData:NSData = try dataPhoto.getData()
                        let image:UIImage = UIImage(data:imageData as Data)!
                        comment.imageFile = image
                        comment.cellType = 2
                    }
                    temp.append(comment)
                }
            }
            catch{
                print(error)
            }
        }

        DispatchQueue.main.async {
            print(temp)
            self.commentList.removeAll()
            self.commentList = temp
            self.commentTableView.reloadData()

        }

    }


}

表格视图:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
    var cell:commentTableViewCell
    if commentList[indexPath.row].cellType == 1{
        //drink
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "drink",for: indexPath) as! commentTableViewCell
        cell.drinkName.text = commentList[indexPath.row].commentOwner
        cell.drinkPrice.text = commentList[indexPath.row].comment
        return cell
    }
    else{
        //comment
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "comment",for: indexPath) as! commentTableViewCell
        cell.ownerTitle.text = commentList[indexPath.row].commentOwner
        cell.commentContent.text = commentList[indexPath.row].comment
        if commentList[indexPath.row].isFile{
            cell.imageView!.image = commentList[indexPath.row].imageFile
        }
        return cell
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return commentList.count
}

更新

我添加了一个隐藏元素,如下所示:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
    var cell:commentTableViewCell
    if commentList[indexPath.row].cellType == 1{
        //drink
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "drink",for: indexPath) as! commentTableViewCell
        cell.drinkName.text = commentList[indexPath.row].commentOwner
        cell.drinkPrice.text = commentList[indexPath.row].comment
        return cell
    }
    else{
        //comment
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "comment",for: indexPath) as! commentTableViewCell
        cell.ownerTitle.text = commentList[indexPath.row].commentOwner
        cell.commentContent.text = commentList[indexPath.row].comment
        if commentList[indexPath.row].isFile{
            cell.imageView!.image = commentList[indexPath.row].imageFile
        }
        else{

            cell.imageView!.isHidden = true
        }
        return cell
    }
}

现在所有图像都消失了:

enter image description here

2 个答案:

答案 0 :(得分:1)

cell.imageView!.image = nil索引中使用if commentList[indexPath.row].cellType == 1。试试这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell{
    var cell:commentTableViewCell
    if commentList[indexPath.row].cellType == 1{
        //drink
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "drink",for: indexPath) as! commentTableViewCell
        cell.drinkName.text = commentList[indexPath.row].commentOwner
        cell.drinkPrice.text = commentList[indexPath.row].comment
          cell.imageView!.image = nil
        return cell
    }
    else{
        //comment
        cell = self.commentTableView.dequeueReusableCell(withIdentifier: "comment",for: indexPath) as! commentTableViewCell
        cell.ownerTitle.text = commentList[indexPath.row].commentOwner
        cell.commentContent.text = commentList[indexPath.row].comment
        if commentList[indexPath.row].isFile{
            cell.imageView!.image = commentList[indexPath.row].imageFile
        }
        return cell
    }
}

答案 1 :(得分:0)

我还没有找到具有更大版本的可靠解决方案,但我愿意做出这个答案并让某人选择它。所以,不要只是隐藏它,我会完全删除imageView,以便它甚至不考虑加载。它看起来像这样:

/ # ps -fe
PID   USER     TIME   COMMAND
    1 root       0:00 sh
    8 root       0:00 sleep 900
    9 root       0:00 sleep 900
   11 root       0:00 ps -fe

现在如果我能找到正确调整图像大小的方法