【问题标题】:UITableViewCells disappearingUITableViewCells 消失
【发布时间】:2017-06-25 15:35:23
【问题描述】:

所有 UITableCell 在 UITableView 中滚动和触摸时都会消失。 一切都以编程方式完成,我使用标准的 UITableViewCell 类。

我在某处读到包含我的单元格数据的数组可能被清空,因此视图无法在滚动时加载数据。但是,我不知道如何验证这一点。

这是我的 TableView 委托和数据源:

class LessonTable: NSObject, UITableViewDataSource, UITableViewDelegate {
    private var lessons = [LessonItem]()
    public var backgroundColor = UIColor(white: 0.97, alpha: 1)

    func addLessons(_ lessons: [LessonItem], tableView: UITableView){ 
        tableView.beginUpdates()

        //Construct array of indexPaths
        var indexPaths = [IndexPath]()
        for i in 0...lessons.count {
            indexPaths.append(IndexPath(row: lessons.count - 1 + i, section: 0))
        }

        //Append all lessons to Lessons array
        self.lessons.append(contentsOf: lessons)

        //Insert rows
        tableView.insertRows(at: indexPaths, with: .automatic)

        tableView.endUpdates()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lessons.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath)
        cell.backgroundColor = backgroundColor

        let row = indexPath.row
        cell.textLabel?.text = lessons[row].Title
        cell.detailTextLabel?.text = lessons[row].Start.Formatted() + " - " + lessons[row].Stop.Formatted()

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let row = indexPath.row
        print(lessons[row].Title)
    }
}

在我的 ViewController 的 ViewDidLoad 方法中,我正在执行以下操作:

let upcomingLessonsTableView = UITableView(frame: CGRect(x: 0, y: 100, width: (card.frame.size.width), height: 90))
    let upcomingLessonsTableViewData = LessonTable()
    upcomingLessonsTableView.dataSource = upcomingLessonsTableViewData
    upcomingLessonsTableView.delegate = upcomingLessonsTableViewData
    upcomingLessonsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "lessonCell")
    upcomingLessonsTableView.backgroundColor = upcomingLessonsTableViewData.BackgroundColor
    card.addSubview(upcomingLessonsTableView)

还有

upcomingLessonsTableViewData.addLessons(upcomingEvents, tableView: upcomingLessonsTableView)

upcomingEvents 是一个数组。

新发现。 当设置一个持续打印出我的课程数组内容的计时器时,单元格不会消失。为什么?没有线索。

【问题讨论】:

  • 你什么时候打电话给 AddLessons?
  • 我在 ViewController 的 ViewDidLoad 方法中这样做。

标签: ios swift uitableview


【解决方案1】:

将以下变量设为全局变量。 让即将到来的LessonsTableViewData;

【讨论】:

  • 变量的范围。分享更多代码,以便我更好地解释。
  • 我知道范围发生了变化,但这与UITableView有什么关系?如果不是因为函数运行完成后ViewDidLoad里面的局部变量被移除了?
  • 根据代码,局部变量将被释放,应用程序应该崩溃。可以分享您的 swift 文件以获取更多信息。我不能用那些行代码说什么。
  • 我想,如果你连续滚动所有记录都会消失。
【解决方案2】:

在您的 cellForRow 方法中添加此 as! LessonCell 您的 let cell 行 应该是这样的:

let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath) as! LessonCell

【讨论】:

  • LessonCell 类不存在。我使用的是标准的 UITableViewCell,因此额外的演员不会有任何区别。对吗?
  • 你有单元格的 xib 文件吗?还有那个xib的课程?或不? @AxelBoberg
  • 我没有 xib。但是,我只是为单元格创建了一个类,因为我必须进行一些调整,在您的代码中命名为 LessonCell。
  • 参见上面的解决方案。我必须将 TableViewData 变量(Data/Delegate 类的实例)设为全局。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2016-08-08
  • 2016-04-02
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
相关资源
最近更新 更多