2017-07-18 48 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