多个复选框选择并单独打印选中的行标题

时间:2017-07-06 09:14:05

标签: ios swift xcode uitableview

我有一个带有显示按钮,标题的表格视图。现在我需要在单元格上检查/取消选中带有图像的单元格。最后我需要打印的图像单元格标题是什么。如何做?

我的代码:

var selectedIndexPathArray = Array<NSIndexPath>()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BlockedTableViewCell

        let pending = allnames?[indexPath.row]

        cell.NameLabel.text = pending?.name


        return cell
    }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedIndexPathArray.append(indexPath as NSIndexPath)
        tableView.reloadData()
    }

我的单元格复选框图片名称:'checked.png' 'unchecked.png'

1 个答案:

答案 0 :(得分:0)

更新图片

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BlockedTableViewCell

            let pending = allnames?[indexPath.row]

            cell.NameLabel.text = pending?.name
           if selectedIndexPathArray.contains(indexPath) {
             cell.checkboxBtn.setImage( UIImage(named:"checked.png"), for: .normal)          
           } else {
             cell.checkboxBtn.setImage( UIImage(named:"unchecked.png"), for: .normal)           
           }


            return cell
        }

打印标题:

func printSelectedItems(){

   for indexPath in selectedIndexPathArray {
      let item = allnames?[indexPath.row]
      print(item.name)
   }
}