我有一個表視圖控制器,用作Review Order屏幕,它基本上包含一些帶靜態內容的單元格(如標題)和其他帶有動態內容的單元格如在先前視圖上選擇的服務列表)。我遇到的問題是,如果我向上滾動以查看視圖的底部,然後再次向上滾動以查看頂部,我注意到動態單元格的順序發生了變化。滾動更改表中動態單元格的順序 - Swift 2.3
例如:
當視圖第一渲染我看到在下面的順序的服務的列表: 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
}
}
將tableview拆分成不同的部分可能會有所幫助,而不需要在它們之間留有空格。這樣你就沒有手動索引管理的脆弱性。 – PeejWeej