Swift:如何在不重新加载数据的情况下重新加载UITableViewCell中的行高

Tal*_*han 12 uitableview swift

我有一个案例,我必须重新加载一个高度UITableViewCell.

但如果我调用该函数

tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
Run Code Online (Sandbox Code Playgroud)

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

这是我的cellForRow块:

    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
    }
Run Code Online (Sandbox Code Playgroud)

Dio*_*nes 33

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

tableView.beginUpdates()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)

您还可以使用此方法,然后使用endUpdates方法为行高度的更改设置动画,而无需重新加载单元格.

UITableView类参考

  • 我应该在哪里以及如何使用它? (2认同)

Mor*_*iya 8

开始更新tableview,然后结束它而不更改任何内容.

tableView.beginUpdates()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)

这应该使tableview设置新的高度

你可以通过实现(a)heightForRowAtIndexPath逻辑设置高度或(b)自动布局和自动tableview行高度来调节高度

一个.

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
     if [your condition, row == 5 in your comment] {
          return 100
     } else {
         return 40
     }
}
Run Code Online (Sandbox Code Playgroud)

每当你想改变高度时,你只需要调用这两行

tableView.beginUpdates()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)

B.

在viewDidLoad中

tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

然后你可以只显示设置单元格高度的布局约束,并在需要时访问它以设置高度

cell.heightConstraint.constant = 100
tableView.beginUpdates()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)