Uitableview 如果某行的数组为空,则隐藏该行

Uitableview 如果某行的数组为空,则隐藏该行,uitableview,swift3,Uitableview,Swift3,我正在创建一个统计页面,显示项目的使用次数 每行有一个itemTitle和itemCount。 如果用户不填充数据,并非每一个itemTitle都有数据,但每一行都有itemCount 我想隐藏空行 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell! = tableVi

我正在创建一个统计页面,显示项目的使用次数

每行有一个itemTitle和itemCount。 如果用户不填充数据,并非每一个itemTitle都有数据,但每一行都有itemCount

我想隐藏空行

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

    let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "StatsCell", for: indexPath)

    let itemTitle: UILabel      = cell.viewWithTag(1000) as! UILabel
    let itemCount: UILabel      = cell.viewWithTag(1001) as! UILabel

    itemTitle.text              = self.statsArray[(indexPath as NSIndexPath).row].itemTitle
    itemCount.text              = "\(self.statsArray[(indexPath as NSIndexPath).row].itemCount)"

    if (self.statsArray[(indexPath as NSIndexPath).row].itemTitle).isEmpty {

        tableView.rowHeight = 0

    }

    return cell
}
这是我正在尝试的,但如果这是真的,它将隐藏所有行,而不仅仅是单个空行

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

    let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "StatsCell", for: indexPath)

    let itemTitle: UILabel      = cell.viewWithTag(1000) as! UILabel
    let itemCount: UILabel      = cell.viewWithTag(1001) as! UILabel

    itemTitle.text              = self.statsArray[(indexPath as NSIndexPath).row].itemTitle
    itemCount.text              = "\(self.statsArray[(indexPath as NSIndexPath).row].itemCount)"

    if (self.statsArray[(indexPath as NSIndexPath).row].itemTitle).isEmpty {

        tableView.rowHeight = 0

    }

    return cell
}
参见所附示例


谢谢

您应该在代码中实现override func tableview tableview:UITableView,heightForRowAtIndexPath:nsindepath->CGFloat,并将数据源中具有空数组的行的高度返回为0。否则返回正常高度。

谢谢Yogi。请注意,您的代码不是swift 3。请修改为:override func tableView\utableview:UITableView,heightforrow在indepath:indepath->CGFloat,以便将来的用户受益