UITableViewCell委托indexPath

时间:2017-09-13 10:43:58

标签: ios swift uitableview

一天中的好时光,我是iOS开发的新手。我正在开发项目,我有tableViewCell和button,progressBar就可以了。当我点击按钮时,这个单元格的indexPath通过委托传递给viewController,然后用另一种方法传递给我 下载一些数据并在progressBar中显示它的进度。因此,当我点击一个单元格然后再切换到另一个单元格时,第一个单元格的进度停止并继续进提前致谢 ) 这是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)
    }
}

这是单元格:

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

正如@RakshithNandish所提到的,我使用了indexPathes列表,当我点击按钮时,list会添加indexPath。因此,在将进度传递给单元格之前,检查进度是否完成:如果没有,则将进度传递给队列的第一个元素,否则只需从队列中删除第一个元素,工作正常。

1 个答案:

答案 0 :(得分:0)

您可以创建一个模型,该模型可以是一个数组,它将保存点击按钮单元格的索引路径,即每当按下按钮时将索引路径追加到数组中,并随时将其删除。稍后在cellForRowAtIndexPath中返回单元格时,检查数组是否包含要返回单元格的indexPath。

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
    }

  }
}
相关问题