【发布时间】:2016-11-25 04:46:50
【问题描述】:
我正在将 UIViews 加载到我的 tableview 单元格内容视图中,但是当我滚动浏览我的 tableview 时,只有一个单元格加载了它的视图。它为每个单元格加载正确的视图,但它只加载一个,然后随着新单元格从底部出现而消失。所有数据都通过函数 makeTableViewRowView(填充 uiview 的字符串数组)在本地加载。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: expenseCell, for: indexPath)
tableViewRow = makeTableViewRowView(indexPath: indexPath)
tableViewRow.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addSubview(tableViewRow)
cell.selectionStyle = .none
let margins = cell.contentView.layoutMarginsGuide
tableViewRow.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
tableViewRow.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
tableViewRow.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
tableViewRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
return cell
}
【问题讨论】:
-
什么是
tableViewRow? -
@Mr.Bista 这只是一个持有 UIView 的属性导致 makeTableViewRowView 返回一个 UIView
-
因为
tableViewRow只初始化一次,所以它只出现在最后一个单元格中 -
尝试使用自定义单元格,而不是在
cellForRowAt中添加子视图 -
@HamzaAnsari 是的。做到了。
标签: ios swift uitableview uiview