如何只用按钮选择抽头单元格?

时间:2017-08-31 09:25:09

标签: swift tableview

一天中的好时光,在我的应用程序中我有一个tableview和自定义单元格,在单元格中有标签,按钮和progressBar,这样当我点击按钮下载继续和progressBar显示进度,但当我向下滚动时我意识到选择了其他单元格并显示进度,当我再次向上滚动时,我所选单元格的进度停止。你可以帮忙,任何反馈意见赞赏)

这是我的TableViewController:

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return titles.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
        cell.pesnya.text = titles[indexPath.row]
        cell.pevets.text = artists[indexPath.row]
        cell.url = urls[indexPath.row]
    return (cell)
}

@IBAction func buttonPressed(_ sender: AnyObject) {
    (sender as! UIButton).isSelected = !(sender as! UIButton).isSelected
    if (sender as! UIButton).isSelected {
        if let indexPath = tableView.indexPath(for: sender.superview!?.superview as! UITableViewCell) {
            DownloadManager.shared.download(url: urls[indexPath.row], title: titles[indexPath.row])
        }
    } else {
        //            (sender as! UIButton).setTitle("Удалить", for: UIControlState.normal)
        if let indexPath = tableView.indexPath(for: sender.superview!?.superview as! UITableViewCell) {
            let name = "\(titles[indexPath.row]).mp3"
            let name2 = name.replacingOccurrences(of: " ", with: "")
            let filePathURL = URL(string:"string")
            do {
                try FileManager.default.removeItem(at: filePathURL!)
            } catch {
                print("Could not delete file: \(error)")
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

这是因为滚动时会重复使用表格视图单元格。将视图设置为单元格prepareForReuse中的初始状态。像这样:

class ACell: UITableViewCell {
    override func prepareForReuse() {
        view.hidden = true
    }
}

答案 1 :(得分:0)

试试这个: -

override func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int
{
return titles.count
}

 override func tableView(_ tableView: UITableView, cellForRowAt 
 indexPath: IndexPath) -> UITableViewCell
 {
 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: 
 indexPath) as! ViewControllerTableViewCell
cell.pesnya.text = titles[indexPath.row]
 cell.pevets.text = artists[indexPath.row]
cell.url = urls[indexPath.row]
// Create IBOutlet for button
cell.btnPressed.tag = indexPath.row
cell.btnPressed.addTarget(self, action: #selector(self.buttonPressed(sender:)), for: .touchUpInside)

if  cell.btnPressed.isSelected {
    DownloadManager.shared.download(url: urls[indexPath.row], title: titles[indexPath.row])
}else {
    let name = "\(titles[indexPath.row]).mp3"
    let name2 = name.replacingOccurrences(of: " ", with: "")
    let filePathURL = URL(string:"string")
    do {
        try FileManager.default.removeItem(at: filePathURL!)
    } catch {
        print("Could not delete file: \(error)")
    }

}

return (cell)
}

@IBAction func buttonPressed(_ sender: UIButton) {
let indexPath =  IndexPath(row: sender.tag, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? 
  ViewControllerTableViewCell {
    cell.btnPressed.isSelected = !cell.btnPressed.isSelected
     tableview.reloadData()
  }

}