一天中的美好时光,我是iOS开发方面的新手。我正在开发项目,其中有tableViewCell和按钮,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。因此,在将进度传递给单元格之前,我检查是否完成了进度:如果没有,将进度传递给队列的第一个元素,否则只需从队列中删除第一个元素,就可以了。
发布于 2017-09-13 11:12:43
您可以创建一个可能是数组的模型,该数组将保存已点击按钮单元格的索引路径,即,每当按钮被点击时,将索引路径追加到数组中,并随时删除它。稍后,在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
}
}
}https://stackoverflow.com/questions/46195433
复制相似问题