Table View Controller Duplicate Itself in iOS Swift

UITableViewCell is repeating after every 5th cell (Before you mark as duplicate kindly read the whole quest )

Remember - the cells are being reused.

You hide the cell, but you never explicitly unhide the cell

When you come to row 6, you are re-using the cell that was at row 0, and isHidden = true

All you need to do is extend your check, and hide the rows that you need to be hidden, and explicitly show the cells that you need to see. If you also have a moving banner that you add - you will also need to check to see if it's been loaded, and remove it if not required. Remember - it may not be row 6 - that's just how it works out with the current screensize

If you do have significant differences between the cells you want to use, you might be better using two different classes - and then you don't have to think about hiding labels

class demoTableCell: DemoTableCellNormalRow {
@IBOutlet var name : UILabel!
@IBOutlet var area : UILabel!
}

class demoTableCell: DemoTableCellFirstRow {
@IBOutlet var area : UILabel!
@IBOutlet var movingBannerView : LCBannerView!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"

if row == 0 {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellFirstRow

cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String

// populate the bannerview which already exists, or create a new one

return cell
} else {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellNormalRow

cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String

return cell
}
}

UITableView Custom Cell is duplicating UIButton on scroll

Because cells are reusable, content stays. So your old buttons are still in your stack view and you're adding next buttons every time.

To fix this, before you add new buttons to UIStackView remove old buttons

extension ButtonCell {

fileprivate func anchorSubViews() {
...

for case let button as UIButton in buttonGroupStackView.subviews {
button.removeFromSuperview()
}

for (index, b) in buttons {
...
buttonGroupStackView.addArrangedSubview(btn)
}
...
}
}

tableview duplicate cell went scrolling down

..ReusableCell means what it says: Cells are reused. All views which are added to the cell will remain.

You are responsible to keep the cell in a defined state.

I'd recommend to create a custom cell in Interface Builder with all desired UI elements and IBOutlets.



Related Topics



Leave a reply



Submit