2017-03-06 102 views
0

我已经设法创建了单元格,并让它们检查select和deselect。如何获取单元格的字符串名称,以及如何在单击按钮时保留它们的选中或取消选中状态。比如说,如果我只想点击选中的单元格并将它们保存到数据库中,并且下次打开表格视图时,它们应该被选中/取消选择。如何从tableview中检查单元格?

import UIKit 


class CellsViewController: UITableViewController { 


    // Creating cells with code 

    let activities = ["Jumping", "History", "Reading", "Football", "Nightlife", "Hiking", "Spa"] 

    // Function to figure out how many rows we need depending on the lenght of the list 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return activities.count 
    } 

    // returns the cell name and puts it in the table view controller 
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = activities[indexPath.row] 
     return cell 
    } 
    // checks and unchecks the given cell based on user click 
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{ 
      tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none 
     }else{ 
      tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark 


     } 
    } 



    @IBAction func saveButton(_ sender: Any) { 
     // Retrieve the checked cells and do something 
     // With them once the button is clicked 

    } 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



} 
+0

的可能的复制[如何获得的数值的选择一个字符串中的UITableView的单元格](http://stackoverflow.com/questions/16492849/how-to-get-the-values-of-selected-cells-of-a-uitableview-in-one-string) – seggy

+0

@seggy我检查了,但不幸的是我刚刚开始学习迅速3,我是新编程,所以我不明白Obj-c。 –

+0

您可以将目标C转换为Swift https://objectivec2swift.com – seggy

回答

0

//喜,给标签该按钮,通过使用标签u需要你的表数据源的阵列中maintatin选择的数据

let activities = [["name": "Jumping", "isSelected": "NO"], ....] 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = activities[indexPath.row]["name"] 
    cell.btnSelect.tag = indexPath.row 
    if activities[indexPath.row]["name"] as String == "Yes" { 
     cell.btnSelect.isSelected = true 
    } else { 
     cell.btnSelect.isSelected = false 
    } 
    return cell 
} 

    @IBAction func saveButton(_ sender: Any) { 
    // Retrieve the checked cells and do something 
    // With them once the button is clicked 
    saveButton.isSelected = !saveButton.isSelected 
    let dicdetails = activities[sender.tag] 
    if saveButton.isSelected { 
     dicdetails["isSelected"] = "YES" 
    } else { 
    dicdetails["isSelected"] = "No" 
    } 
    activities[sender.tag] = dicdetails 
} 
+0

这可以工作,但它一次只返回一个单元格,我想选择多个单元格并让它们都是“是”或“否”。 –

+0

这也总是只改变最后点击的单元格,我正在寻找可能将它们添加到数组中并使用它们后缀。 –

+0

我刚刚给你举例,你需要按照你的逻辑来实现 –