R.AlAli R.AlAli - 1 month ago 6
Swift Question

Swift loading more data repeats the already existing cell

I'm using UITableview to load data from web service.
When viewDidLoad 10 cell are loaded, when user scroll down more data is loaded like Facebook application.

The problem is that on load more 10 extra cells should be loaded (numberOfRowInSection should be equal to 20) but instead the first 10 cells are repeated and than the news cell appear (i.e numberOfRowInSection is equal to 30)

example of what is happening

viewDidLoad:

A

B

C

when load more:

A

B

C

A

B

C

D

E

F

what I'm expecting is :

viewDidLoad:

A

B

C

when load more:

A

B

C

D

E

F

I'm getting data from web service in this code:

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)
}


Here I'm code having the UITableview functions:

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()
})
}

}
}


note that i'm using local data

Thanks in advance

Answer

Looks like your server returns you all the data.

Before appending the new data

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

clean the newsGrouped array.

Comments