0

Within UITableView, I have a custom cell with download button. Once Download button is tapped, I update the tag with indexPath.row and in download function progress view is displayed in that cell. The problem is when user scrolls and cell becomes invisible, that specific progress view starts showing in a different cell.

This is my cellForRowAtIndexPath function:

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

    var  cell:CellWithDownload! = tableView.dequeueReusableCellWithIdentifier("CellWithDownload") as CellWithDownload
    var title = rowDataAll[indexPath.row].valueForKey("BayanTitle") as String
    cell.TitleLabel.text = title
    cell.AuthorLabel.text = rowDataAll[indexPath.row].valueForKey("Artist") as? String
    var countOfLikesString = 
    cell.downloadButton.tag = indexPath.row
    cell.downloadButton.addTarget(self, action: Selector("downloadAudio:"), forControlEvents: UIControlEvents.TouchUpInside)

    // If cell becomes visible again, then star        

    if let isDownloading = rowDataAll[indexPath.row].valueForKey("isDownloading") {

        if (isDownloading as NSString == "true") {
            cell.showDownloading()
            cell.progressView.progress = getDownloadObjectWithURL(url).progress
        } else if (isDownloading as NSString == "completed") {                
            cell.hideDownloading()                
        }
    }        
    return cell
}

Please advice.

Kampai
  • 22,848
  • 21
  • 95
  • 95
  • What are you doing in `downloadAudio:`? – Ashraf Tawfeeq Feb 10 '15 at 09:51
  • In my download Audio, I am using: `var indexSelectedTableView:NSInteger = sender.tag rowDataAll[indexSelectedTableView].setValue("true", forKey: "isDownloading") var cell:CellWithDownload = self.tableView?.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as CellWithDownload cell.showDownloading()` – Asif Masroor Feb 10 '15 at 10:00

1 Answers1

1

Most likely, the issue is that you are not re-initializing the cells once reused. When you dequeue a new cell it could be reusing one of the cells which just scrolled off screen. If that had the progress bar showing and you never initialize dequeued cells to have no progress bar, then as soon as you reuse one which had a progress bar, it will appear.

So I think you just need to make sure you initialize the cells after you dequeue one.

In your cellForRowAtIndexPath I think you need an else statement added to deal with a cell which is not downloading and set no progress bar:

if let isDownloading = rowDataAll[indexPath.row].valueForKey("isDownloading"){
   <Your existing code seems to setup the progress bar here>
}
else{
   <Set it to have no pogress bar in here.>
   cell.hideDownloading()
}
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • See updated answer. Basically don't assume the cell is brand new after dequeueReusableCellForIndexPath. You need to initialize everything. – Rory McKinnel Feb 10 '15 at 10:09