我已经设法创建了单元格,并让它们检查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.
}
}
的可能的复制[如何获得的数值的选择一个字符串中的UITableView的单元格](http://stackoverflow.com/questions/16492849/how-to-get-the-values-of-selected-cells-of-a-uitableview-in-one-string) – seggy
@seggy我检查了,但不幸的是我刚刚开始学习迅速3,我是新编程,所以我不明白Obj-c。 –
您可以将目标C转换为Swift https://objectivec2swift.com – seggy