2016-12-08 57 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处设置一个断点,你会发现它是零。

相关问题