2017-07-18 78 views
0

我正在创建一个统计信息页面,显示使用某个项目的次数。如果该行的数组为空,则隐藏单独的行

每行有一个itemTitleitemCount。 并非每个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 
} 

见附件中的例子

enter image description here

感谢

回答

1

你应该实现在你的代码override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat并返回高度为0,其在数据源空数组行。否则返回正常高度。

+1

谢谢瑜珈。请注意,你的代码不是swift 3.请修改为:覆盖func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath) - > CGFloat因此,未来的用户将受益 –

+0

哦,是的。感谢您指点。完成 – Yogi