2016-12-08 60 views
0

我想隱藏我的表格視圖中的特定單元格?隱藏我的表格中的特定單元格

private var yourDataSource = [AccountData.AllTasksDB] //A Json Array originally 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    var rowHeight:CGFloat = 72 

    //AccountData.AllTasksDB[indexPath.row]["importantvariable"] 
    if (self.yourDataSource[indexPath.row]["importantvarible"] == "0") 
    { 
     print("Cell is mine") 
     rowHeight = 0 
    } 
    else 
    { 
     print("Cell is not mine") 
     rowHeight = 72.0 
    } 

    return rowHeight 
} 

誰能告訴我我做錯了什麼?我認爲它是循環遍歷每個單元格並檢查我已經設置的一個值。但它似乎並不想訪問它。

+0

我建議節省您想隱藏,然後重新加載表來確定的rowHeight – Nathaniel

+0

'的tableView細胞(S)的indexPath .cellForRowAtIndexPath(indexPath)''可能會返回nil,因爲單元格已經出隊...... – Nathaniel

+0

示例:'fileprivate var self.hiddenCellIndexes.contains:[IndexPath]'然後在您的tableView中的heightForRow委託:'if self.hiddenCellIndexes.contains indexPath)' – Nathaniel

回答

0

我想這會在回答中更清晰

fileprivate var yourDataSource = [YourObject]() 

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

    if self.yourDataSource[indexPath].isMine == true { 

    return 0 
    } 
    else 
    { 
    return 72.0 //or whatever you like 
    } 
} 
+0

只需確保根據需要追加/刪除索引,然後調用'self.tableView.reloadData()' – Nathaniel

+0

我更新了我的代碼。但它仍然沒有看到單元格的變量(來自TableViewCellMyTasks類的cell.isMine)。我試圖看看是否有一個變量是真的,如果是這樣,將單元格的高度設置爲0(基本上隱藏它) – Taurian

+0

我不建議您訪問單元格的屬性。你應該使用你的dataSource(用來填充單元格,對嗎?)來確定高度。這是否更有意義? – Nathaniel

0

您正在訪問tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)方法中的單元格。 此時單元格還不可見(或出列)。如果單元格不可見,則它將爲零,因此您的錯誤。

試着在let cell = tableView.cellForRowAtIndexPath(indexPath) as! TableViewCellMyTasks處設置一個斷點,你會發現它是零。

相關問題