我有一个 TableView Controller ,用作审查订单屏幕,它基本上有一些单元格具有静态内容(例如标题)和其他单元格具有动态内容(例如在以前的 View 中选择的服务列表)。我遇到的问题是,如果我向上滚动到 View 的底部,然后再次向上滚动以查看顶部,我注意到动态单元格的顺序发生了变化。
例如:
当 View 首次呈现时,我按以下顺序看到服务列表: 1) 美甲 2) 修脚 3) 30 分钟桑拿
但是,如果我再次向下和向上滚动,我会看到以下内容: 1) 30 分钟桑拿 2) 修脚 3) 美甲
下面是渲染每个单元格的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.row == 0) {
//static header cell
self.index = 0
let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell", forIndexPath: indexPath)
return headerCell
} else if (indexPath.row == 1) {
//static instruction cell
self.index = 0
let instructionCell = tableView.dequeueReusableCellWithIdentifier("instructionCell", forIndexPath: indexPath)
return instructionCell
} else if (indexPath.row == 2) {
//static services section header cell
self.index = 0
let servicesSectionHeaderCell = tableView.dequeueReusableCellWithIdentifier("servicesSectionHeaderCell", forIndexPath: indexPath)
return servicesSectionHeaderCell
} else if ((indexPath.row <= (self.services.count + 2)) && (indexPath.row > 2) && !self.services.isEmpty) {
//dynamic service cells
let serviceCell = tableView.dequeueReusableCellWithIdentifier("serviceCell", forIndexPath: indexPath) as! ServiceCell
serviceCell.serviceLabel.text = self.services[self.index]
self.index += 1
return serviceCell
} else if (indexPath.row == (self.services.count + 3)) {
//static appointment time section header cell
self.index = 0
let appointmentTimeSectionHeaderCell = tableView.dequeueReusableCellWithIdentifier("appointmentTimeSectionHeaderCell", forIndexPath: indexPath)
return appointmentTimeSectionHeaderCell
} else if (indexPath.row == (self.services.count + 4)) {
//static date and time cell
self.index = 0
let dateAndTimeCell = tableView.dequeueReusableCellWithIdentifier("dateAndTimeCell", forIndexPath: indexPath) as! DateAndTimeCell
dateAndTimeCell.dateAndTimeLabel.text = self.orderReview.serviceDate + SINGLE_WHITE_SPACE_STRING_CONSTANT + self.orderReview.serviceTime
return dateAndTimeCell
} else {
//default cell
self.index = 0
let cell = UITableViewCell()
return cell
}
}
最佳答案
您的问题是 self.index
的使用。它的值似乎取决于调用 cellForRow
的顺序。那根本行不通。
去掉 self.index
并使索引基于 indexPath
的值。
} else if ((indexPath.row <= (self.services.count + 2)) && (indexPath.row > 2) && !self.services.isEmpty) {
//dynamic service cells
let serviceCell = tableView.dequeueReusableCellWithIdentifier("serviceCell", forIndexPath: indexPath) as! ServiceCell
serviceCell.serviceLabel.text = self.services[indexPath.row - 3]
return serviceCell
关于ios - 滚动更改表中动态单元格的顺序 - Swift 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42278374/