Swift加载更多数据会重复已有的单元格

时间:2016-10-20 08:43:42

标签: ios swift facebook web-services uitableview

我使用UITableview从Web服务加载数据。 当加载viewDidLoad 10单元格时,当用户向下滚动时,会加载更多数据,如Facebook应用程序。

问题是在加载时应加载10个额外的单元格(numberOfRowInSection应该等于20),而是重复前10个单元格而不是新闻单元格出现(即numberOfRowInSection等于30)

正在发生的事情的例子

viewDidLoad中:

A

C

加载更多时:

A

C

A

C

d

电子

˚F

我期待的是:

viewDidLoad中:

A

C

加载更多时:

A

C

d

电子

˚F

我在此代码中从网络服务获取数据:

                    if let infoDic = result.valueForKey("Info") as? NSDictionary {
                    globalVariables.gcurrentRun = (infoDic["CurrentRun"] as! String)
                        print("currentRun: \(globalVariables.gcurrentRun)")
                    globalVariables.gnextRun = (infoDic["NextRun"] as? String)!
                    if globalVariables.gcurrentRun == "1" {
                       sourceForImage = "news_icn"
                    }
                    else if globalVariables.gcurrentRun == "2" {
                       sourceForImage = "facebook_icon"
                    }
                    else if globalVariables.gcurrentRun == "3" {
                       sourceForImage = "twitter_icn"
                    }
                        print("nextRun: \(globalVariables.gnextRun)")
                    currentRunForLoadMore = globalVariables.gnextRun
                    if "\(infoDic["NoData"])" == 1 {
                    globalVariables.gnoData = "noMoreData"
                        print("noData: \(globalVariables.gnoData)")
                    } else {
                        globalVariables.gnoData = "moreData"
                        print("noData: \(globalVariables.gnoData)")
                    }
                    globalVariables.gpreviousRun = (infoDic["PreviousRun"] as? String)!
                        print("previousRun: \(globalVariables.gpreviousRun)")
                    currentRunForRefresh = globalVariables.gpreviousRun
                    globalVariables.gremark = (infoDic["Remark"] as? String)!
                        print("remark: \(globalVariables.gremark)")
                }

                if let mediaArray = result.valueForKey("mediaList") as? NSArray {
                    for media in mediaArray {
                        if globalVariables.gcurrentRun == "1" {
                    channel = ((media as? NSDictionary)!.valueForKey("Channel") as? String)!
                    print("channel: \(channel)")
                        }
                    globalVariables.gContentOfNews = ((media as? NSDictionary)!.valueForKey("Content") as? String)!
                    print("content: \(globalVariables.gContentOfNews)")
                        if globalVariables.gcurrentRun != "1" {
                            if let memberDic = (media.valueForKey("member") as? NSDictionary){
                               let memberFName = memberDic.valueForKey("Member_FName") as! String
                                channel = memberFName
                            }
                    print("fromName: \(channel)")
                        }
                    globalVariables.gIdFromWeb = Int((media.valueForKey("ID") as? String)!)!
                    globalVariables.gPublishD = ((media as? NSDictionary)!.valueForKey("PublishD") as? String)!
                    print("publishD: \(globalVariables.gPublishD)")
                    globalVariables.gPublishT = ((media as? NSDictionary)!.valueForKey("PublishT") as? String)!
                    print("publishTime: \(globalVariables.gPublishT)")

                        newsGrouped.append(NewsGrouped(channelOfNews: channel, publishedDate: globalVariables.gPublishD, publishedTime: globalVariables.gPublishT, contentOfNews: globalVariables.gContentOfNews, remarkSourceOfNews: "\(globalVariables.gTypeId)", newsIdFromWeb: globalVariables.gIdFromWeb, noDataToShow: globalVariables.gnoData))
                    do {
                        let bosId = try TeamDataHelper.insert(
                            NewsFeed(
                                newsId: 0,
                                typeId: Int(globalVariables.gcurrentRun)!,
                                idFromWeb: globalVariables.gIdFromWeb,
                                newInsertFlag: 1,
                                channel: channel,
                                fromName: globalVariables.gFromName,
                                contentOfNews: globalVariables.gContentOfNews,
                                publishD: globalVariables.gPublishD,
                                publishT: globalVariables.gPublishT
                            ))
                        print(bosId)
                    }
                }
                    completion(newsGrouped: newsGrouped)
            }

这里我是具有UITableview功能的代码:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    print("numberOfRows: \(newsGrouped.count)")
    return self.newsGrouped.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    loadActivityIndicator.stopAnimating()
   let newsCell: HPNewsWithImageTableViewCell = newsTableView.dequeueReusableCellWithIdentifier("hpNewsWithImageTableViewCell", forIndexPath: indexPath) as! HPNewsWithImageTableViewCell

        newsCell.idLbl.text = "\(newsGrouped[indexPath.section].newsIdFromWeb)"
        print("newsIdFromWeb: \(newsGrouped[indexPath.section].newsIdFromWeb)")
        newsCell.newsDate.text = newsGrouped[indexPath.section].publishedDate
        newsCell.userNameLbl.text = newsGrouped[indexPath.section].channelOfNews
        print("globalVariables.gChannel: \(newsGrouped[indexPath.section].channelOfNews)")

        newsCell.imageOfSource.image = UIImage(named: "\(newsGrouped[indexPath.section].sourceImage)")
        print("imageOfSource: \(newsGrouped[indexPath.section].sourceImage)")
        newsCell.newsTime.text = newsGrouped[indexPath.section].publishedTime

        print(newsGrouped[indexPath.section].publishedTime)
        newsCell.newsText.text = newsGrouped[indexPath.section].contentOfNews
        print(newsGrouped[indexPath.section].contentOfNews)
            newsCell.selectionStyle = .None
    do {
    try TeamDataHelper.update()
        }
    catch {
    }
    return newsCell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 170
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("newsDetailsSegue", sender: self)
}

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

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 5
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    if offsetY > contentHeight - scrollView.frame.size.height {
        loadMoreIndicator.hidden = false
        loadMoreIndicator.startAnimating()
        CallForNewsAPICalls.GetNewsFeeds(actionDown, CurrentRun: currentRunForLoadMore) { (newsGrouped) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                for news in newsGrouped {
                    print("currentRunForLoadMore\(currentRunForLoadMore)")
                    self.newsGrouped.append(news)
                }
                self.newsTableView.reloadData()
            })
        }

    }
}

请注意我使用的是本地数据

提前致谢

3 个答案:

答案 0 :(得分:0)

看起来您的服务器会返回所有数据。

在追加新数据之前

newsGrouped.append(NewsGrouped(channelOfNews: channel, publishedDate: globalVariables.gPublishD, publishedTime: globalVariables.gPublishT, contentOfNews: globalVariables.gContentOfNews, remarkSourceOfNews: "\(globalVariables.gTypeId)", newsIdFromWeb: globalVariables.gIdFromWeb, noDataToShow: globalVariables.gnoData))

清理newsGrouped数组。

答案 1 :(得分:0)

为了确保您只在最后一行加载,您可以使用willDisplayCell。检查当前行是否等于总计数或数组,然后开始加载新数据

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == self.yourArray.count - 1 {
        // fetch your data here
    }
}

答案 2 :(得分:0)

您只需将以前的数据存储在一个数组l.e中。比如 storeArray ,当你加载其他内容时,追加一个到storeArray并使用 sotreArray 到tableview ...