2017-02-16 109 views
0

我有一個表視圖控制器,用作Review Order屏幕,它基本上包含一些帶靜態內容的單元格(如標題)和其他帶有動態內容的單元格如在先前視圖上選擇的服務列表)。我遇到的問題是,如果我向上滾動以查看視圖的底部,然後再次向上滾動以查看頂部,我注意到動態單元格的順序發生了變化。滾動更改表中動態單元格的順序 - Swift 2.3

例如:

當視圖第一渲染我看到在下面的順序的服務的列表: 1)修指甲 2)修腳 3)30分鐘桑拿浴 enter image description here

但是,如果我再次向下滾動,我會看到以下內容: 1)30分鐘桑拿 2)修腳 3)修指甲 enter image description here

下面是呈現每個單元代碼:

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 
     } 
    } 
+1

將tableview拆分成不同的部分可能會有所幫助,而不需要在它們之間留有空格。這樣你就沒有手動索引管理的脆弱性。 – PeejWeej

回答

2

你的問題是使用的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 
+0

謝謝!我看到我把它變得比原來更復雜。那些深夜,tsk。 – Jace