首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当加载带有错误“无效更新:无效节数”的表视图时,应用程序崩溃。

当加载带有错误“无效更新:无效节数”的表视图时,应用程序崩溃。
EN

Stack Overflow用户
提问于 2017-05-04 05:56:24
回答 2查看 761关注 0票数 2

App崩溃并显示以下错误:

无效更新:无效的节数。更新(6)后表视图中包含的节数必须等于更新(3)之前表视图中包含的节数,加上或减去插入或删除的节数(0插入,0删除)。请帮帮忙

我的代码:

代码语言:javascript
运行
复制
func numberOfSections(in tableView: UITableView) -> Int {

    return newsArray.count

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

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


    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as? NewsTableViewCell


    cell?.contentView.backgroundColor = UIColor.clear

    let whiteRoundedView : UIView = UIView(frame: CGRect(x: 10, y: 4, width: self.view.frame.size.width - 20, height: 410))

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.cornerRadius = 2.0
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
    whiteRoundedView.layer.shadowOpacity = 0.2

    cell?.contentView.addSubview(whiteRoundedView)
    cell?.contentView.sendSubview(toBack: whiteRoundedView)

    let newsdata = newsArray[indexPath.section]

    let date = NSDate(timeIntervalSince1970: TimeInterval(newsdata.meta))
    let dayTimePeriodFormatter = DateFormatter()
    dayTimePeriodFormatter.dateFormat = "MMM dd YYYY "

    let dateString = dayTimePeriodFormatter.string(from: date as Date)


    print(dateString)

    if let newsImg = self.newsImageCache.object(forKey: indexPath.section as AnyObject){
        cell?.imageOut.image = newsImg as? UIImage
    } else {
        loadImageFromWeb(uri: newsdata.photo, cache: self.newsImageCache, indexpath: indexPath)

    }
    cell?.titleLabel.text = newsdata.title
    cell?.descLabel.text = newsdata.body


    return cell!
}




 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.section + 3 == self.newsArray.count {

        loadDatafromUrl()
    }
}



func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20
}

// Make the background color show through
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clear
    return headerView
}



func loadDatafromUrl(){
    let uri = "http://localhost/unani-info/admin/json/news.php"
    print(uri)
    if let url = URL(string: uri){

        let config = URLSessionConfiguration.default
        config.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        let session = URLSession(configuration: config)

        let task = session.dataTask(with: url, completionHandler: {

            (rawData,response,error) in

            if error != nil {
                print("Couldnt load data")
                print(error?.localizedDescription as Any)

            } else {
                if let data = rawData {
                    do{
                        if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [AnyObject]{

                            for index in 0...json.count-1 {

                                if let datas = json[index] as? [String: AnyObject] {

                                    let newsObj = News()
                                    newsObj.title = datas["title"] as! String
                                    newsObj.body = datas["body"] as! String
                                    let photo = datas["photo"] as! String
                                    let photourl = "http://localhost/unani-info/admin/uploads/" + photo
                                    newsObj.photo = photourl
                                    newsObj.meta = datas["meta"] as! Int

                                    self.newsArray.append(newsObj)


                                }

                            }
                            DispatchQueue.main.async {
                                self.tableView.reloadData()
                            }

                        }
                    }catch{
                        print("error")
                    }
                }
            }
        })
        task.resume()
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-04 06:05:05

你能试试吗:

代码语言:javascript
运行
复制
DispatchQueue.main.async {
    for index in 0...json.count-1 {
        if let datas = json[index] as? [String: AnyObject] {
            let newsObj = News()
            newsObj.title = datas["title"] as! String
            newsObj.body = datas["body"] as! String
            let photo = datas["photo"] as! String
            let photourl = "http://localhost/unani-info/admin/uploads/" + photo
            newsObj.photo = photourl
            newsObj.meta = datas["meta"] as! Int
            self.newsArray.append(newsObj)
        }
    }
    self.tableView.reloadData()
}

而不是:

代码语言:javascript
运行
复制
for index in 0...json.count-1 {
    if let datas = json[index] as? [String: AnyObject] {
        let newsObj = News()
        newsObj.title = datas["title"] as! String
        newsObj.body = datas["body"] as! String
        let photo = datas["photo"] as! String
        let photourl = "http://localhost/unani-info/admin/uploads/" + photo
        newsObj.photo = photourl
        newsObj.meta = datas["meta"] as! Int
        self.newsArray.append(newsObj)
    }
}
DispatchQueue.main.async {
    self.tableView.reloadData()
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-04 06:29:38

你在倒置numberOfSectionsnumberOfRowsInSection。你应该:

代码语言:javascript
运行
复制
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsArray.count
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43774916

复制
相关文章

相似问题

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