2016-11-25 36 views
0

我正在将UIViews加载到我的tableview单元格内容视图中,但当我滚动浏览tableview时,只有一个单元格加载其视图。它为每个单元格加载正确的视图,但它只加载一个,然后随着新单元格从底部出现而消失。所有数据都通过函数makeTableViewRowView(填充uview的字符串数组)在本地加载。当我滚动时,TableView只加载一个单元格的内容

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 
    } 

broken tableView

+1

什么是'tableViewRow'? –

+0

@ Mr.Bista它只是一个拥有UIView原因的属性makeTableViewRowView返回一个UIView –

+0

它的因为'tableViewRow'只初始化一次,这就是为什么它出现在最后一个单元格只有 –

回答

0

使自定义单元格在Interface Builder和deque它。并根据cellForRowAtIndexPath中该单元格的要求进行更改!因为cellForRowAtIndexPath会在你的单元格为deque时被调用,我的意思是单元格将被重用!因此,在cellforrow中创建视图并将其作为子视图添加并不是一个好的解决方案,您可以直接从界面构建器添加并根据需要在cellforrow中对其进行处理!

+0

好的想法,但我没有用这个项目的IB。哈姆扎的回答为我工作。 –

相关问题