滚动后表格视图单元格混合(swift)
tableview cells mix after scrolling (swift)
我创建了一个 table 视图,用一些数据填充单元格,效果很好。
问题是,当我滚动 table 时,所有的单元格都混在一起了,你知道阻止这种情况的方法吗?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let celda = tabla_reportes.dequeueReusableCellWithIdentifier("celda", forIndexPath: indexPath) as! celdaReportes
switch indexPath.section {
case 0:
celda.datos = tipos
celda.back = UIColor.lightGrayColor()
case 1:
celda.index = indexPath.row
celda.datos = fillArrayWithData(datos[indexPath.row] as! NSDictionary)
celda.back = UIColor.clearColor()
default:
break
}
celda.delegate = self
return celda
}
因为单元格被重新用于性能。例如,当您向上滚动表格视图并且 1 号单元格消失时,10 号单元格实际上是您的 1 号单元格。无论您在 1 号单元格中设置什么,并且值未更新,都保留在 10 号单元格中,依此类推...如果它没有值,请尝试在单元格中放置一个空白值。
您应该更新单元格的字段。也许你在这里 UITableViewCell reuse good practice 可以帮助你
我创建了一个 table 视图,用一些数据填充单元格,效果很好。
问题是,当我滚动 table 时,所有的单元格都混在一起了,你知道阻止这种情况的方法吗?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let celda = tabla_reportes.dequeueReusableCellWithIdentifier("celda", forIndexPath: indexPath) as! celdaReportes
switch indexPath.section {
case 0:
celda.datos = tipos
celda.back = UIColor.lightGrayColor()
case 1:
celda.index = indexPath.row
celda.datos = fillArrayWithData(datos[indexPath.row] as! NSDictionary)
celda.back = UIColor.clearColor()
default:
break
}
celda.delegate = self
return celda
}
因为单元格被重新用于性能。例如,当您向上滚动表格视图并且 1 号单元格消失时,10 号单元格实际上是您的 1 号单元格。无论您在 1 号单元格中设置什么,并且值未更新,都保留在 10 号单元格中,依此类推...如果它没有值,请尝试在单元格中放置一个空白值。
您应该更新单元格的字段。也许你在这里 UITableViewCell reuse good practice 可以帮助你