Swift:如何在不重新加载数据的情况下重新加载 UITableViewCell 中的行高 [英] Swift: How to reload row height in UITableViewCell without reloading data

查看:40
本文介绍了Swift:如何在不重新加载数据的情况下重新加载 UITableViewCell 中的行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例,我只需要重新加载 UITableViewCell 的高度.

但是如果我调用函数

tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)

它重新加载单元格的高度和数据.我怎样才能在 Swift 中控制单元格的高度?

这是我的 cellForRow 块:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {如果 indexPath.row == 0 {let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as!自定义TableViewCell1cell.heading.text = headerText返回单元格}否则如果 indexPath.row == 1 {let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as!自定义TableViewCell2ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) incell.mainImage.image = 图像})返回单元格}否则如果 indexPath.row == 2 {let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as!自定义TableViewCell3//cell.aurthorImage.image = UIImage(named : "obama")ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) incell.aurthorImage.image = 图像})cell.aurthorImage.tag = aurthorIDcell.aurthorImage.layer.cornerRadius = cell.aurthorImage.frame.height/2cell.aurthorImage.clipsToBounds = truecell.aurthorImage.userInteractionEnabled = truecell.aurthorImage.addGestureRecognizer(aurthorImageTapRecignizer)cell.aurthorName.text = self.authorNamecell.time.text = self.timeself.followButton = cell.followButton返回单元格}否则如果 indexPath.row == 3 {let cell = tableView.dequeueReusableCellWithIdentifier("cell4", forIndexPath: indexPath) as!自定义TableViewCell4让 htmlHeight = contentHeights[indexPath.row]cell.webElement.tag = indexPath.rowcell.webElement.delegate = selfcell.webElement.loadHTMLString(HTMLContent, baseURL: nil)cell.webElement.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)返回单元格}否则如果 indexPath.row == 4 {let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as!自定义TableViewCell1cell.heading.text = "相关帖子"返回单元格}否则如果 indexPath.row == 5{let cell = tableView.dequeueReusableCellWithIdentifier("cell6", forIndexPath: indexPath) as!自定义TableViewCell6返回单元格}否则如果 indexPath.row == 6 {let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as!自定义TableViewCell1cell.heading.text = "评论"返回单元格}否则如果 indexPath.row == 7 {let cell = tableView.dequeueReusableCellWithIdentifier("cell5", forIndexPath: indexPath) as!自定义TableViewCell5让 htmlHeight = contentHeights[indexPath.row]self.commentSection = cell.commentsViewself.commentSection.tag = indexPath.rowself.commentSection.delegate = self让 url = NSURL(字符串:commentsURL)让 requestObj = NSURLRequest(URL: url!)self.commentSection.loadRequest(requestObj)self.commentSection.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)commentSectionDidNotLoad = false返回单元格}别的 {let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as!自定义TableViewCell1cell.heading.text = headerText返回单元格}

解决方案

您可以使用此代码更新单元格的高度,而无需重新加载其数据:

tableView.beginUpdates()tableView.endUpdates()

<块引用>

您也可以使用此方法,然后使用 endUpdates 方法来为行高的变化设置动画,而无需重新加载单元格.

UITableView类参考

I have a case in which I have to reload only height of a UITableViewCell.

but if I call the function

tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)

it reloads the height as well as the data of the cell. How can i just control the height of cell in Swift?

This is my cellForRow block :

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
    cell.heading.text = headerText
        return cell
    }

    else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! CustomTableViewCell2
        ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in
            cell.mainImage.image = image
        })
        return cell
    }
    else if indexPath.row == 2 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as! CustomTableViewCell3
        //cell.aurthorImage.image = UIImage(named : "obama")
        ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in
            cell.aurthorImage.image = image
        })
        cell.aurthorImage.tag = aurthorID
        cell.aurthorImage.layer.cornerRadius = cell.aurthorImage.frame.height/2
        cell.aurthorImage.clipsToBounds = true
        cell.aurthorImage.userInteractionEnabled = true
        cell.aurthorImage.addGestureRecognizer(aurthorImageTapRecignizer)
        cell.aurthorName.text = self.authorName
        cell.time.text = self.time
        self.followButton = cell.followButton
        return cell
    }

    else if indexPath.row == 3 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell4", forIndexPath: indexPath) as! CustomTableViewCell4
        let htmlHeight = contentHeights[indexPath.row]

        cell.webElement.tag = indexPath.row
        cell.webElement.delegate = self
        cell.webElement.loadHTMLString(HTMLContent, baseURL: nil)
        cell.webElement.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)

        return cell
    }
    else if indexPath.row == 4 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
        cell.heading.text = "Related Posts"
        return cell
    }
    else if indexPath.row == 5{
        let cell = tableView.dequeueReusableCellWithIdentifier("cell6", forIndexPath: indexPath) as! CustomTableViewCell6
        return cell

    }
    else if indexPath.row == 6 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
        cell.heading.text = "Comments"
        return cell
    }
    else if indexPath.row == 7 {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell5", forIndexPath: indexPath) as! CustomTableViewCell5


        let htmlHeight = contentHeights[indexPath.row]
        self.commentSection = cell.commentsView
        self.commentSection.tag = indexPath.row
        self.commentSection.delegate = self
        let url = NSURL(string: commentsURL)
        let requestObj = NSURLRequest(URL: url! )
        self.commentSection.loadRequest(requestObj)
        self.commentSection.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
            commentSectionDidNotLoad = false

            return cell
    }
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
        cell.heading.text = headerText
        return cell
    }

解决方案

You can use this code to update the cell's height without reloading their data:

tableView.beginUpdates()
tableView.endUpdates()

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

UITableView Class Reference

这篇关于Swift:如何在不重新加载数据的情况下重新加载 UITableViewCell 中的行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆