【问题标题】:Calling heightForRowAtIndexPath manually手动调用 heightForRowAtIndexPath
【发布时间】:2015-09-08 13:34:18
【问题描述】:

在我的表格视图中,表格被加载,然后图像被加载,当图像被加载时,我想从我配置单元格的 ProductTableViewCell.swift 文件中再次调用此函数。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath:  NSIndexPath) -> CGFloat {
 return cell.frame.size.height
}

我该怎么做?

编辑:

import UIKit

class ProductTableViewCell: UITableViewCell {

//var appleview: AppleProductsTableViewController!
@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productTitleLabel: UILabel!
@IBOutlet weak var productDescriptionLabel: UILabel!
let screenSize: CGRect = UIScreen.mainScreen().bounds



func configureCellWith(product: Product){
    productImageView.image = product.image
    productDescriptionLabel.text = product.description
    productTitleLabel.text = product.title

    var urlstring = product.imageURL

    ImageLoader.sharedLoader.imageForUrl(urlstring as String, completionHandler:{(image: UIImage?, url: String) in
 //self.productImageView.frame.size = orgframe.frame.size
        self.productImageView.image = image!

        dispatch_async(dispatch_get_main_queue(), {
            self.productImageView.frame.size = (image?.size)!

            while self.productImageView.frame.size.height > 0.4 * self.screenSize.height {
                self.productImageView.frame.size.width = self.productImageView.frame.size.width * 0.9
                self.productImageView.frame.size.height = self.productImageView.frame.size.height * 0.9
                println("kleiner 2")
            }
            while self.productImageView.frame.size.width > 0.6 * self.screenSize.width{
                self.productImageView.frame.size.width = self.productImageView.frame.size.width * 0.9
                self.productImageView.frame.size.height = self.productImageView.frame.size.height * 0.9
                println("kleinrer")
            }
            self.productTitleLabel.center.y = self.productImageView.center.y + self.productImageView.frame.size.height/2 + 15
            self.productDescriptionLabel.center.y = self.productTitleLabel.center.y + 28

            NSNotificationCenter.defaultCenter().postNotificationName("renew", object: nil)
        })


    })


}


}

【问题讨论】:

    标签: xcode swift uitableview tableview


    【解决方案1】:

    只需使用:

    tableView.reloadData()
    

    所以

    heightForRowAtIndexPath
    

    将再次调用,您的所有单元格都会获得“新”计算的高度。更好的主意(在大多数情况下)是使用 Autolayout 自动计算所需的空间。

    编辑:如果你想从任何类重新加载你的表,你可以使用通知。

    所以在你的 TableView 中:

     func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTable:",name:"renew", object: nil)
    }
    
        func reloadTable(notification: NSNotification){
        //load data here
        self.tableView.reloadData()
    }
    

    这个在你的牢房里。

    NSNotificationCenter.defaultCenter().postNotificationName("renew", object: nil)
    

    【讨论】:

    • 但我有 2 个类,1 个用于 tableview,1 个用于 tableviewcell。我需要从 tableviewcell 类中调用它,但我不能。因为 tableviewcell 没有名为 tableview 的成员,如果我使用 TableViewController.tableView.reloadData() 我得到一个 lldb 错误。我知道我很笨,解决起来很简单,但不知道怎么解决
    • 您可以使用 NSNotification 从任何类(添加通知的位置)更新您的表格 - 我将编辑我的答案。
    • 谢谢它的工作原理,但它卡在一个循环中,有没有办法在没有覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} 函数开始的情况下更新行高叫什么?
    • 不,那总是一起调用(但是为什么不返回相同的单元格?)
    • 我确实返回了相同的单元格,但它陷入了循环。看看我的 productTableViewcell(见编辑)
    【解决方案2】:

    有一种“偷偷摸摸”的方式来做到这一点。

    如果你使用:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    不会重新加载任何单元格,但会触发 heightForRowAt 并且单元格会自行调整大小。

    感谢 Hamish Rickerby (blog)

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多