首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滚动更改表中动态单元格的顺序- Swift 2.3

滚动更改表中动态单元格的顺序- Swift 2.3
EN

Stack Overflow用户
提问于 2017-02-16 15:39:43
回答 1查看 270关注 0票数 0

我有一个,它充当Review屏幕,它基本上有一些带有静态内容的单元格(例如标头)和其他具有动态内容的单元格(例如在以前的视图上选择的服务列表)。我遇到的问题是,如果我向上滚动到视图的底部,然后再向上滚动看到顶部,我注意到动态单元格的顺序发生了变化。

例如,

当视图第一次呈现时,我按以下顺序看到服务列表:1)美甲 2)修脚 3) 30分钟桑拿

但是,如果我再次向下滚动和向上滚动,我会看到以下情况:1) 30分钟桑拿 2)修脚 3)美甲

下面是呈现每个单元格的代码:

代码语言:javascript
复制
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
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-16 15:45:17

您的问题是self.index的使用。它的值似乎取决于调用cellForRow的顺序。那根本行不通。

去掉self.index并根据indexPath的值建立索引。

代码语言:javascript
复制
} 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42278374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档