滚动更改 Table - Swift 中动态单元格的顺序 2.3
Scrolling changes the order of the Dynamic Cells in a Table - Swift 2.3
我有一个 Table View Controller 用作 Review Order 屏幕,它基本上有一些具有静态内容的单元格(例如 headers)和其他具有动态内容的单元格(例如列表在以前的视图中选择的服务)。我遇到的问题是,如果我向上滚动到视图的底部,然后再次向上滚动以查看顶部,我注意到动态单元格的顺序发生了变化。
例如:
当视图首次呈现时,我按以下顺序看到服务列表:
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
我有一个 Table View Controller 用作 Review Order 屏幕,它基本上有一些具有静态内容的单元格(例如 headers)和其他具有动态内容的单元格(例如列表在以前的视图中选择的服务)。我遇到的问题是,如果我向上滚动到视图的底部,然后再次向上滚动以查看顶部,我注意到动态单元格的顺序发生了变化。
例如:
当视图首次呈现时,我按以下顺序看到服务列表:
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