【问题标题】:Unable to set imagesize for UIImageView in UITableViewCell无法在 UITableViewCell 中为 UIImageView 设置图像大小
【发布时间】:2016-10-08 21:15:35
【问题描述】:

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

但是正如你所看到的,图像最终占据了整个单元格,不知道为什么......

这是布局应该是什么样子...我尝试进行方面填充...但最终结果大致相同...

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

设置 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
    }
}

解析查询:

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

        }

    }


}

【问题讨论】:

    标签: ios swift uitableview uiimageview


    【解决方案1】:

    这里有两件事要做:

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

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

    【讨论】:

    • 我希望图像适合某个方面,因此它会填充图像但不会完整显示图像。
    • Aspect fit 完整显示图像(“fit”)。
    • 哦,有没有办法让图像适合 imageview 呢?抱歉,其中一些似乎有点令人困惑。我是否必须每次调整图像大小以使其符合图像视图的标准?
    • 有没有办法调整图像大小,使其适合图像视图?
    • 你有没有看我的回答??我已经解释了该怎么做。试试吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多