2017-06-03 88 views
0

我有一个固定的空间量,我将拥有一个UITableView。我不想让UITableView滚动,所以我想计算不用滚动的单元格的数量。单元格大小不一,并使用UITableViewAutomaticDimension来确定大小。什么是最好的方式去做这件事?计算适合固定空间的UITableViewCells的数量

回答

1

您可以使用此方法并在字典中保留单元格高度,然后您可以在字典值上运行cicle并计算所有保存的值,并根据值执行所需操作,让我们看一些示例代码,

首先定义字典,让您的价值观

var automaticDimensionsHeightRows : [Int:CGFloat] = [:] 

我定义这个var automaticDimensionsHeightRows变量,因为我有2种类型的细胞的人有固定的高度和其他人UITableViewAutomaticDimension高度

然后用这种方法,我们得到的,并保持价值

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if((self.tableView.delegate?.tableView!(self.tableView, heightForRowAt: indexPath))! == UITableViewAutomaticDimension) 
    { 
     self.automaticDimensionsHeightRows[indexPath.row] = cell.frame.size.height 
    } 
} 

最后,你可以在你的字典中的值运行cicle,并检查你的价值观或做任何你需要做的

希望这有助于

0

如果你不想的tableView滚动刚刚获得的tableView 可见细胞让arrayCell = tableView.indexPathsForVisibleItems() 或 让arrayCells = tableView.visibleCells()

一旦你看到可见的细胞,你可以发现细胞的数量是可见的