2016-11-25 50 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。哈姆扎的回答爲我工作。 –

相關問題