无法在UITableViewCell中为UIImageView设置imagesize

时间:2016-10-08 21:15:35

标签: ios swift uitableview uiimageview

我已经设置了图像(如果可用)但由于某种原因我不能将图像的大小设置为图像视图(如果它有图像或至少是剪切预览)我尝试了一些东西沿着this

的路线

但正如你所看到的,图像最终占据整个细胞不确定为什么...... enter image description here

这就是布局应该是什么样子......我试着去填充一个方面...但是最终的结果大致相同......

enter image description here

编辑:添加了代码,我忘了添加......这会有很多帮助吗?

设置tabeview单元格

    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!.contentMode = UIViewContentMode.scaleAspectFill
            cell.imageView!.image = commentList[indexPath.row].imageFile
        }
        else if !commentList[indexPath.row].isFile {
            cell.imageView!.image = nil
            cell.imageView!.removeFromSuperview()

        }
        return cell
    }
}

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

        }

    }


}

1 个答案:

答案 0 :(得分:0)

这里有两件事要做:

  • 将图片视图的clipsToBounds设置为true。否则,无论图像视图有多小,它都会显示图像中之外的部分。

  • 为图像视图提供足够的约束以确定其宽度和高度。否则,它将尝试成为图像的大小。