ios - UITableViewCell 委托(delegate) indexPath

标签 ios swift uitableview

今天真好,我是 iOS 开发的新手。我正在开发项目,其中有 tableViewCell 和按钮,progressBar。当我点击这个单元格的按钮 indexPath 通过委托(delegate)传递给 viewController 然后用另一种方法我是 下载一些数据并在 progressBar 中显示它的进度。因此,当我点击一个单元格然后点击另一个单元格时,第一个单元格的进度停止并在第二个单元格继续,有人可以帮忙吗?提前致谢 ) 这是 viewController 中的委托(delegate)方法:

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 列表,当我点击按钮时,列表添加了 indexPath。因此,在将进度传递给单元格之前,我检查进度是否已完成:如果没有,则将进度传递给队列的第一个元素,否则只需从队列中删除第一个元素,工作正常。

最佳答案

您可以创建一个模型,它可能是一个数组,它将保存点击按钮的单元格的索引路径,即只要点击按钮就将索引路径附加到数组,并在需要时将其删除。稍后在 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
    }

  }
}

关于ios - UITableViewCell 委托(delegate) indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46195433/

相关文章:

iOS:将行添加到 TableView

ios - json数据自定义http请求

ios - 带循环的 NSOperationQueue 和带委托(delegate)的类

iphone - ios - 如何在现有类(class)中分配新类(class)?

ios - UITableView willDisplayCell 方法的不当行为

swift - 类型 'AVCapturePhotoOutput' 的值没有成员 'outputSettings'

ios - 如何将属于 UITableView 中模型数组对象的数组中的数据显示到 tableView?

ios - 不允许 MSAL 为目标 9.0 安装 cocoa pod

ios:将新密码添加到 iCloud 钥匙串(keychain)

ios - 获取重新排序的 UITableViewCells 的顺序