简体   繁体   English

为什么存在图像时我的tableview单元的大小不正确?

[英]Why are my tableview cells not properly sizing when an image is present?

I'm setting up a "newsfeed" styled tableview and would like for my custom cells to automatically resize based on two factors: 我正在设置“ newsfeed”样式的表格视图,并希望自定义单元格根据两个因素自动调整大小:

1) Whether or not the represented post contains an image - If it doesn't, the cell should behave as there though is no ImageView and size appropriately 1)所代表的帖子是否包含图像-如果不包含图像,则该单元应表现为没有适当的ImageView和大小

2) If the post does contain an image, the ImageView should set its height according to the image height (hence, no height constraint on the ImageView) and the cell should resize accordingly 2)如果帖子中确实包含图像,则ImageView应根据图像高度设置其高度(因此,ImageView上没有高度限制),并且单元格应相应地调整大小

I'm getting extremely erratic behavior from my cells in trying to accomplish this. 在尝试完成此操作时,我的单元格获得了非常不稳定的行为。 Initially, the first cell with an image will populate correctly. 最初,带有图像的第一个单元格将正确填充。 However, as you scroll through, images begin to size incorrectly and others don't appear at all. 但是,当您滚动浏览时,图像的尺寸开始不正确,而其他图像根本没有出现。

My images are being downloaded asynchronously via Google Firebase. 我的图片正在通过Google Firebase异步下载。

I'm using Autolayout in my cell and have top, bottom, leading, and trailing constraints set properly for all subviews. 我在单元格中使用了自动布局,并为所有子视图正确设置了顶部,底部,前导和尾随约束。 The issue seems to be limited only to the ImageView. 该问题似乎仅限于ImageView。 No other elements are affected. 没有其他元素受到影响。

I've tried setting a height constraint on the ImageView and programmatically resizing it depending on whether or not an image is meant to appear. 我尝试在ImageView上设置高度限制,并根据是否要显示图像以编程方式调整其大小。 This does not seem to be helping. 这似乎没有帮助。

Here are my tableview delegate methods: 这是我的tableview委托方法:

// Tableview methods
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mPosts!.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 600
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = newsfeed.dequeueReusableCell(withIdentifier: "newsfeedCell") as! NewsfeedCell

    let post = mPosts![indexPath.row]

    cell.posterNameDisplay.text = post.getPosterName() + " " + (mAccount?.getFamilyName())!

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMMM dd, yyyy @ hh:mm a"
    let dateString = dateFormatter.string(from: post.getTimeStamp())
    cell.timestampDisplay.text = dateString

    cell.postMessageDisplay.text = post.getPostMessage()

    AccountUtils.loadProfilePhoto(ProfileId: post.getPosterId(), ProfilePhoto: cell.profilePhoto)
    cell.profilePhoto.layer.borderWidth = 1.0
    cell.profilePhoto.layer.masksToBounds = false
    cell.profilePhoto.layer.borderColor = UIColor.white.cgColor
    cell.profilePhoto.layer.cornerRadius = cell.profilePhoto.frame.size.width / 2
    cell.profilePhoto.clipsToBounds = true

    PostUtils.loadPostImage(View: cell.postImage, PostId: post.getPostId())

    return cell
}

And here is the utility method I'm using to download and set the image in the ImageView: 这是我用来在ImageView中下载并设置图像的实用程序方法:

public static func loadPostImage(View postImage: UIImageView, PostId postId: String) {
    let storage = Storage.storage()
    let storageRef = storage.reference()
    let photoRef = storageRef.child("photos/" + Auth.auth().currentUser!.uid + postId + ".jpg")

    photoRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
        if error != nil {
            postImage.image = nil
        }

        else {
            let image = UIImage(data: data!)
            postImage.image = image
        }
    }
}

If your cells have more than one unique layout, each unique layout should receive its own reuse identifier (and better it's own subclass). 如果您的单元具有多个唯一的布局,则每个唯一的布局应收到自己的重用标识符(最好是自己的子类)。

Create two cell subclasses - one for text only, the second is for cell with image 创建两个单元格子类-一个仅用于文本,第二个用于具有图像的单元格

Change your func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell to something similar: 将您的func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell更改为类似的内容:

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

    let isCellHasImage: Bool = // understand if it will image cell or just text cell

    if (isCellHasImage) {
        // dequeue and return your image cell
        // return cell
    }

    // dequeue and return your text-only cell
    // ...
    // return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM