2017-02-16 89 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

相关问题