Step 1:
Declare Arrays.
@IBOutlet var sampleTable: UITableView!
var nameArray : NSMutableArray = NSMutableArray()
var finalArray : NSMutableArray = NSMutableArray()
Register TableViewCell in ViewDidLoad, Name is SampleTableViewCell
In ViewDidLoad to add Country names to nameArray.
sampleTable.allowsMultipleSelection = true
sampleTable.allowsMultipleSelectionDuringEditing = true
sampleTable.registerNib(UINib(nibName: "SampleTableViewCell", bundle: nil), forCellReuseIdentifier: "SampleTableViewCell")
nameArray = ["India", "NZ", "England", "AUS", "PAK", "SA", "Bangladesh","Kenya","Srilanka"]
Step2:
Write TableView DataSource and Delegate Methods to Display all countries in one table.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SampleTableViewCell", forIndexPath: indexPath) as! SampleTableViewCell
cell.brandName.text = nameArray[indexPath.row] as? String
if cell.selected
{
cell.selected = false
if cell.accessoryType == UITableViewCellAccessoryType.None
{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
}
return cell
}
//didSelectRow
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
//tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell!.selected
{
cell!.selected = false
if cell!.accessoryType == UITableViewCellAccessoryType.None
{
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
finalArray.addObject((nameArray[indexPath.row] as? String)!)
}
else
{
cell!.accessoryType = UITableViewCellAccessoryType.None
finalArray.removeObject((nameArray[indexPath.row] as? String)!)
}
}
print(finalArray)
}
In finalArray having all the selected rows.