在 UITableView 中隐藏空行并根据非空行更改 UITableView 的高度,可以通过以下步骤实现:
具体实现方法如下:
在 UITableView 的数据源中,可以使用以下代码来动态调整 UITableView 的高度:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
其中,dataSource 是一个数组,包含了 UITableView 的数据源。
在 UITableView 的代理方法中,可以使用以下代码来动态调整 UITableViewCell 的高度:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if dataSource[indexPath.row].isEmpty {
return 0
} else {
return 44
}
}
其中,如果 dataSourceindexPath.row 为空字符串,则返回 0,否则返回 44。
在 UITableView 的代理方法中,可以使用以下代码来动态调整 UITableViewCell 的显示和隐藏:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if dataSource[indexPath.row].isEmpty {
cell.isHidden = true
} else {
cell.isHidden = false
}
}
其中,如果 dataSourceindexPath.row 为空字符串,则隐藏该 UITableViewCell,否则显示该 UITableViewCell。
最终,可以通过以上代码实现在 UITableView 中隐藏空行并根据非空行更改 UITableView 的高度。
领取专属 10元无门槛券
手把手带您无忧上云