首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UITableView中插入多行

如何在UITableView中插入多行
EN

Stack Overflow用户
提问于 2017-08-03 17:32:16
回答 1查看 1.4K关注 0票数 0

我试着用这个答案Cant insert rows into UITableView - Swift 3向表视图插入行,它工作得很好,但有问题,他们显示,但跳过一些数据。

基本上我的问题是我有2D数组var students : [[Student]]?,我想插入一个有4行的部分。

在表视图中显示插入行的正确方式是什么。

到目前为止,我所尝试的

代码语言:javascript
复制
 func numberOfSections(in tableView: UITableView) -> Int {
        return students?.count ?? 0
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return students?[section].count ?? 0
    }

并插入:

代码语言:javascript
复制
 //scrolling down time calling this method
    func oldestState(){
        self.students?.append(getdata()) //Update students  property
        self.newsFeedTableView.beginUpdates()
        let indexPaths = (0..<(getdata().count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
        print(indexPaths)
        self.newsFeedTableView.insertSections([(self.students?.count)! - 1], with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.endUpdates()   
    }

    func getdata() -> [Student]{
        var _students = [Student]()
        for i in  itemNumber..<(itemNumber + 4) {
            let student = Student()
            student.name = "\(i)"
             print("adding student roll number : \(student.name)")
            _students.append(student)
        }
        itemNumber  +=  4
        return _students
    }

check full code

这是输出结果。如果您看到日志,您将看到在表视图中跳过第8-11行

EN

回答 1

Stack Overflow用户

发布于 2017-08-03 18:59:48

我希望这个问题是因为getdata()方法被调用了两次,因此itemNumber值增加了4,两次

试试这个:

代码语言:javascript
复制
let data = getdata()
self.students?.append(data) //Update students  property
self.newsFeedTableView.beginUpdates()
let indexPaths = (0..<(data.count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45480244

复制
相关文章

相似问题

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