In my code I have a table view that loads the name of users using my app. When a user selects a row it will appear a checkmark. What I want to do is save an array that contains all the selected rows (the array will contain the names). I found some information, but I still learning iOS programming and I dont know Obj-c.
This is what I have done so far:
var selectedMembers = [String]?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.names?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UserCell") as! UITableViewCell
cell.textLabel!.text = names![indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
selectedMembers = Array()
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark
{
cell.accessoryType = .None
self.selectedMembers?.remove(indexPath)
}
else
{
cell.accessoryType = .Checkmark
self.selectedMembers?.append(indexPath)
}
}
}