如何解决 TableView 数据重叠问题? 我搜索了一些对象 C 版本的答案,但它似乎不适用于 Swift。我试图获取 cell==nil,但它给了我一个错误
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.cellForRowAtIndexPath(indexPath) as! PatientTableViewCell
if (cell){
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
if(cell != nil){
var subviews = cell.contentView.subviews
subviews.removeAll()
}
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
折腾了两天,终于明白了。解决这个问题的关键是找到哪个小区被恢复了。我现在正在做的是给单元格一个 var 标识符。
class PatientTableViewCell: UITableViewCell {
@IBOutlet weak var followupBtn: UIButton!
@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var titleType: UILabel!
@IBOutlet weak var titleID: UILabel!
@IBOutlet weak var patientTypelbl: UILabel!
@IBOutlet weak var patientIDlbl: UILabel!
**var identifier = false**
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
var cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
if(cell.identifier == true){
cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
//config cell
cell.identifier = true //important
我希望它可以帮助别人。 :)
最佳答案
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell: PatientTableViewCell! = tableView.dequeueReusableCellWithIdentifier("patientCell") as? PatientTableViewCell
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
关于Swift,防止 uitableviewcell 重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358857/