alikhan
alikhan

Reputation: 67

UITableViewCell delegate indexPath

Good time of day, I'm new in iOS development. I'm developing project where i have tableViewCell and button,progressBar on it. When i tap button indexPath of this cell is passed through delegate to viewController and then with another method i'm downloading some data and show it's progress in progressBar. So when i tap to one cell and then to another, progress in first cell stops and continues in second, could anyone help? Thanks in advance ) Here is delegate methods in viewController:

func didTouchButtonAt(_ indexPath: IndexPath) {
    songs[indexPath.row].isTapped = true
    let selectedSong = self.songs[indexPath.row] as Song
    DownloadManager.shared.delegate = self
    self.indexQueue.append(indexPath)
    self.selectedIndexPath = indexPath
    DownloadManager.shared.download(url: selectedSong.url , title: selectedSong.title)
}
func downloadProgress(_ progress: Progress) {        
    if (progress.completedUnitCount) < progress.totalUnitCount {
            selectedIndexPath = indexQueue.first
    }
    else if(!indexQueue.isEmpty){
        indexQueue.removeFirst()
    }
    print(progress.fractionCompleted)
    print(progress.completedUnitCount, progress.totalUnitCount)
    print(indexQueue.count)
    var cell: ViewControllerTableViewCell?
    cell = self.tableView.cellForRow(at: self.selectedIndexPath!) as? ViewControllerTableViewCell
    if cell != nil {
        cell?.progress = Float(progress.fractionCompleted)
    }
}

this is cell:

@IBAction func downloadButtonTouched(sender: Any){
    self.delegate?.didTouchButtonAt(self.indexPath!)
    self.progressBar.isHidden = false
}

As @RakshithNandish mentioned, I'v used list of indexPathes and when i tap to button, list adds indexPath. So, before passing progress to cell i check if progress is completed: if not, pass progress to first element of queue, otherwise just delete first element from queue, works fine.

Upvotes: 3

Views: 340

Answers (1)

laxman khanal
laxman khanal

Reputation: 998

You can create a model that may be an array which will hold the indexpath of tapped button's cell i.e. append indexpath to the array whenever button is tapped and remove it whenever you want. Later on while returning cells in cellForRowAtIndexPath you check if the array contains indexPath for which you are returning cell.

class DemoCell: UITableViewCell {
   @IBOutlet button: UIButton!
}

class DemoTableViewController: UITableViewController {

  var buttonTappedIndexPaths: [IndexPath] = []

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:       DemoCell.className, for: indexPath) as! DemoCell 
    if buttonTappedIndexPaths.contains(indexPath) {
      //show progress view spinning or whatever you want
    } else {
      //don't show progress view
    }

  }
}

Upvotes: 1

Related Questions