2017-08-31 23 views
0

一天的好时光,在我的应用程序中,我有一个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)") 
      } 
     } 
    } 

} 
+0

你有你的按钮状态保存在数据源。 – Jaydeep

回答

0

这是因为当你滚动表视图单元格将被重用。将视图设置为单元格prepareForReuse中的初始状态。就像这样:

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

你的答案几乎有帮助,但是当我在这里设置按钮isSelected = false时,我选中的按钮也不会被选中,对我来说这个错误,你怎么看? – alikhan

+0

@Oklahoma你应该恢复表视图数据源的状态,主要是在tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell' – Lumialxk

+0

正确的方式使用'UITableView'是内部管理数据,所以所有选择应该存储在您的数据模型中,而不是按钮属性。 – Legoless

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() 
    } 

} 
相关问题