我有一个,它充当Review屏幕,它基本上有一些带有静态内容的单元格(例如标头)和其他具有动态内容的单元格(例如在以前的视图上选择的服务列表)。我遇到的问题是,如果我向上滚动到视图的底部,然后再向上滚动看到顶部,我注意到动态单元格的顺序发生了变化。
例如,:
当视图第一次呈现时,我按以下顺序看到服务列表: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
}
}发布于 2017-02-16 15:45:17
您的问题是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 serviceCellhttps://stackoverflow.com/questions/42278374
复制相似问题