ios - 尝试为同一索引路径使多个单元格出队,这是 Swift 不允许的

标签 ios swift uitableview

在我的应用程序中,我有一个TabBar导航系统。当应用程序为第一个选项卡加载 View Controller 时,其中的 calendarTableView 正常加载,但是如果我切换到另一个选项卡然后返回到第一个选项卡,我会得到 NSInternalInconsistencyException Attempted to dequeue multiple cells for相同的索引路径 错误。将单元格声明为: let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell 给我错误。现在,我按照其他帖子中的建议解决了它,方法是在 cellForRowAt 方法的单元格声明中不指定 indexPath 并将其声明为:let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell") 作为! CalendarTableViewCell 但这是否意味着我错误地管理了数据源或者出现该错误的原因是什么?不应该总是指定 indexPath 吗?我做了一个测试,因为直到现在我才遇到问题,我将系统日期更改为更早的日期,比如本月 10 日,但我没有收到错误。那么这是否意味着 tableview 试图使太多单元格出队?你能看到发生了什么吗?我真的很感激对此的一些解释。 一如既往,非常感谢您的宝贵时间。 这是我正在使用的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell
//        let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell") as! CalendarTableViewCell
        cell.configureUi()
        let date = datesArray[indexPath.row]
        cell.cellDate = String( describing: date)
        let calendar = Calendar.current
        let components = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        let dayInWeek = dateFormatter.string(from: date)
//        cell.dayLabel.text = "\(String(describing: components.day!))" + " " + "\(dayNamesArray[components.weekday! - 1])"
        cell.dayLabel.text = "\(String(describing: components.day!))" + " " + dayInWeek
        cell.cellWeekday = components.weekday!
        //        print("cell weekday is: \(cell.cellWeekday!)") // prints correct weekday
        cell.cellId = "\(String(format:"%04d", components.year!))" + "\(String(format:"%02d", components.month!))" + "\(String(format:"%02d", components.day!))"
        self.selectedDate = cell.cellId // used for time slots cellId
        //        print("##################### selectedDate in tableview is :\(self.selectedDate) ")
        // highlighting current day cell
        if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
            cell.layer.borderWidth = 4
            if Theme.selectedTheme == 1 {
                if #available(iOS 11.0, *) {
                    cell.layer.borderColor = Theme.calendarCellToday?.cgColor
                } else {
                    // Fallback on earlier versions
                    cell.layer.borderColor = Theme.calendarCellTodayRgb.cgColor
                }

            } else if Theme.selectedTheme == 2 {
                if #available(iOS 11.0, *) {
                    cell.layer.borderColor = Theme.calendarCellToday2?.cgColor
                } else {
                    // Fallback on earlier versions
                    cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
                }
            }
            //            print(" @@@@@@@@@@@@@@@@@@@@@@@@@@   selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
            self.selectedDate = cell.cellId
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
            cell.configureUi()
            // highlighting current day cell
            if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
                cell.layer.borderWidth = 4
                if Theme.selectedTheme == 1 {
                    if #available(iOS 11.0, *) {
                        cell.layer.borderColor = Theme.calendarCellToday?.cgColor
                    } else {
                        // Fallback on earlier versions
                        cell.layer.borderColor = Theme.calendarCellTodayRgb.cgColor
                    }
                } else if Theme.selectedTheme == 2 {
                    if #available(iOS 11.0, *) {
                        cell.layer.borderColor = Theme.calendarCellToday2?.cgColor
                    } else {
                        // Fallback on earlier versions
                        cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
                    }
                }
                //            print(" @@@@@@@@@@@@@@@@@@@@@@@@@@   selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")

                self.selectedDate = cell.cellId
            }
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
            cell.layer.borderWidth = 4
            if Theme.selectedTheme == 1 {
                if #available(iOS 11.0, *) {
                    cell.layer.borderColor = Theme.firstTintColor?.cgColor
                } else {
                    // Fallback on earlier versions
                    cell.layer.borderColor = Theme.firstTintColorRgb.cgColor
                }

            } else if Theme.selectedTheme == 2 {
                if #available(iOS 11.0, *) {

                    cell.layer.borderColor = Theme.firstTintColor2?.cgColor
                } else {
                    // Fallback on earlier versions
                    cell.layer.borderColor = Theme.firstTintColorRgb2.cgColor
                }
            }
            self.updateTimeSlots(selectedCell: cell)
        }
    }

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let cell: CalendarTableViewCell = tableView(self.calendarTableview, cellForRowAt: IndexPath.init(row: self.actualDay - 1, section: 0)) as! CalendarTableViewCell
        self.updateTimeSlots(selectedCell: cell)

    }

最佳答案

很可能真正的原因在 scrollViewDidEndScrollingAnimation 中。

从不调用数据源方法tableView(_:cellForRowAt:) (不像 cellForRow(at: )你自己。它由框架专门调用。

并且强烈建议您不要在 cellForRow 之外操作单元格。修改模型并重新加载行。

关于ios - 尝试为同一索引路径使多个单元格出队,这是 Swift 不允许的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321942/

相关文章:

ios - swift 2.0 UIWebview 只显示白页

objective-c - Objective-C : UITable Method "didSelectRowAtIndexPath" does not work if label is added to cell

php - Apple 推送通知中的表情符号支持

iphone - 选择 WiFi 网络

objective-c - 我如何回到 SpriteKit 中的旧场景?

iOS Storyboard - 导航 Controller 不工作

ios - 使用swift 4将字典传递到 TableView 中

swift - 未获取 "requires explicit use of ' self' 以使捕获语义显式”Xcode 中的错误,构建在应该失败时成功

ios - 如何检测 UITableView 上的触摸,到目前为止似乎没有任何效果?

swift - UITableview 中的收藏夹按钮 (Swift 4)