Reputation: 1514
so here is the thing, i tried to work my way around with no framework as i wanted to unterstand it. What i wanted to do what implement a drag and drop uitableview with multisection and an autoscroll feature. The issue i ran into is that when autoscrolling back and forth some celles (at least their label names) duplicates on other cells and i really don't understand why.
let indexPath = self.tableView.indexPathForRowAtPoint(currentLongPressTouchLocation)
if indexPath != Drag.sourceIndexPath
{
// swap the data between the 2 (internal) arrays
let dataPiece = self.items[Drag.sourceIndexPath.section][Drag.sourceIndexPath.row]
self.items[indexPath!.section].insert(dataPiece, atIndex: indexPath!.row)
self.items[Drag.sourceIndexPath.section].removeAtIndex(Drag.sourceIndexPath.row)
//------=-=-=-=[
// This line errors when it is uncommented. When I comment it out, the error is gone,
// and the cells /do/ reorder.. ¯\_(ツ)_/¯
// swap(¤tList.orderItems[indexPath.row], ¤tList.orderItems[Drag.sourceIndexPath.row])
self.tableView.moveRowAtIndexPath(Drag.sourceIndexPath, toIndexPath: indexPath!)
Drag.sourceIndexPath = indexPath
// error is definitly here imo but cannot find it :/
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomViewCell
cell.tag = indexPath.row
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
cell.label.frame = CGRectMake(20, 0, screenWidth - 20, 44)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
return cell
}
I forgot the init of the customviewcell my bad :
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
label = UILabel(frame: CGRectMake(20, 0, screenWidth / 2, 44))
labelBis = UILabel(frame: CGRectMake(20, 20, screenWidth - 20, 20))
labelBis.textAlignment = NSTextAlignment.Center
self.contentView.addSubview(label)
self.contentView.addSubview(labelBis)
}
If anybody could help me on this it would be gladly appreciated, thanks for reading.
Upvotes: 1
Views: 1011
Reputation: 38162
Data duplication on table view cells happens due to cell re-use in cellForRowAtIndexPath:
. You might be calling, dequeueReusableCellWithIdentifier
method inside cellForRowAtIndexPath:
which pulls out already created cell from the pool (typically when cell 1 disappears and cell 8 is about to show, cell 1 is re-used for cell 8 instead of creating a new cell).
When this happens it is our responsibility to reset the re-used cell content before setting new content. At times, it may happen that your cell label setting code might be in a If condition which is not executing in current context leading to showing the re-used cell's label text.
Please check, I am sure your case would be falling in this.
Upvotes: 4