首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell委托indexPath

UITableViewCell委托indexPath
EN

Stack Overflow用户
提问于 2017-09-13 10:43:57
回答 1查看 335关注 0票数 3

一天中的美好时光,我是iOS开发方面的新手。我正在开发项目,其中有tableViewCell和按钮,progressBar在上面。当我点击这个单元格的indexPath按钮时,它通过委托传递给viewController,然后用另一种方法下载一些数据并显示它在progressBar中的进展情况。所以,当我点击一个细胞,然后点击另一个细胞,第一个细胞的进展停止,第二个细胞继续前进,有人能帮上忙吗?)下面是viewController中的委托方法:

代码语言:javascript
复制
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)
    }
}

这是牢房:

代码语言:javascript
复制
@IBAction func downloadButtonTouched(sender: Any){
    self.delegate?.didTouchButtonAt(self.indexPath!)
    self.progressBar.isHidden = false
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-13 11:12:43

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

代码语言:javascript
复制
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
    }

  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46195433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档