UITableViewCell 的动态维度可以,但是如果为空如何隐藏它们?

Dynamic Dimension of UITableViewCell ok, but how hide them if empty?

我有一个 UITableView,其中包含 3 个原型单元格(例如第一个单元格:图像,第二个单元格:描述,3. 链接,...)。

如果后端的数据为空,我想隐藏它们(例如,如果没有图像,隐藏第一个单元格)。为此,我以这种方式覆盖了 heightForRowAtIndexPath 函数:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch (indexPath.row) {

    case 0:
        if event?.photo_urls.count == 0{
            return 0
        }
        else{
            return 80.0
        }
    case 1:
        if event?.description == ""{
            return 0
        }
        else{
            return 90.0
        }
    default:
        return 100.0
    }
}

并通过

隐藏单元格
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    switch (indexPath.row) {

    case 0:
        let cell = tableView.dequeueReusableCellWithIdentifier("PhotoCell", forIndexPath: indexPath) as! UITableViewCell

        if event?.photo_urls.count != 0 {
            // ...
        }
        else{
            cell.hidden = true
        }

        return cell

    case 1:
        let cell = tableView.dequeueReusableCellWithIdentifier("DesCell", forIndexPath: indexPath) as! UITableViewCell
       if event?.description != "" {
            // ...
        }
        else{
            cell.hidden = true
        }

        return cell

    default:
        let cell = tableView.dequeueReusableCellWithIdentifier("PhotoCell", forIndexPath: indexPath) as! UITableViewCell
        return cell

    }
}

到这里没问题,它工作正常! 现在,问题是我想根据单元格内容(例如描述高度)使单元格动态化。为此,我使用了

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 80.0
 }

如果我评论 heightForRowAtIndexPath 细胞实际上是动态的但我不能再隐藏它们了。

对于如何隐藏空单元格并根据其内容应用自动尺寸,您有什么建议吗?

为此,我建议您先对数组进行排序,然后再在 table 视图中显示它。隐藏单元格不是一个好主意,根据 Apple 的建议也不好。所以除了隐藏单元格你可以做一件事:从数组中删除索引。通过这种方式,您始终可以在 table 中显示数据,并且它会正常运行。所以不要试图隐藏单元格,只需从数组中弹出索引即可。

假设您有动态数据并且想在表视图中显示它,因此您需要创建一个数据数组来显示。

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet
var tableView: UITableView
var items: [String] = ["We", "Heart", "nothing" ,"" ,"imageurl", "", "xyz"]

override func viewDidLoad() {
    super.viewDidLoad()
    reloadTableAfterSorting()
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

func reloadTableAfterSorting(){

for var i = 0; i < self.items.count; i++
   {

    if self.items[i] == ""{
        self.items.removeAtIndex(2)

     }


   }

  self.tableView.reloadData()
 }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel?.text = self.items[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You selected cell #\(indexPath.row)!")
  }
}