Reputation: 87
I'm working in a UITableView which contains 5 rows ("a" to "e"), and when I click on one of the rows, it shows me in the next UITableView the letter I chose as a Header (with the prepareForSegue function). Actually, until this point it works perfectly.. BUT when I try to add more "headers" in the 2nd tableView after clicking in more than one row from the first UITableView, it just show me the last option (for example, if I click first in "a", then "d" and then "c"... the second UITableView just show me the row "c" as a header, not the other 2 rows).
How can I choose multiple rows and send those options to a second UITableView through a prepareForSegue function?
My line of codes for the first view are these:
let array = ["a", "b", "c", "d", "e"]
@IBOutlet var initialTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueA" {
if let destination = segue.destinationViewController as? Content {
if let selectedRow = initialTableView.indexPathForSelectedRow?.row {
destination.title = array[selectedRow]
}
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = array[indexPath.row]
return cell
}
The second view contains these lines:
var contentArray:[String] = []
@IBOutlet var contentTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
contentArray.append(title!)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return contentArray.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellContent = tableView.dequeueReusableCellWithIdentifier("cellContent", forIndexPath: indexPath) as UITableViewCell
cellContent.textLabel!.text = "Second test"
return cellContent
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return contentArray[section]
}
Thanks!
Upvotes: 0
Views: 1388
Reputation: 17710
Just enable multiple selection on the tableview, and use the indexPathsForSelectedRows variant to get the full list. You can then either pass the whole list from your data source + the indexes, or map the indexes to the actual values.
Upvotes: 0
Reputation: 15512
Joe.
For solving of you're issue.You should enabled multiple selection by :
tableView.allowsMultipleSelection = YES;
Than you should remember items you selected in first viewContoller. For convenience try to use:
var indexPathsForSelectedRows: [NSIndexPath]? { get }
And than send this information to the another viewController
by segue.
Example:
let array = ["a", "b", "c", "d", "e"]
@IBOutlet var initialTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
initialTableView.allowsMultipleSelection = YES
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueA" {
if let destination = segue.destinationViewController as? Content {
if let selectedRows = initialTableView.indexPathsForSelectedRows {
//hear you have option to send items that are selected to another controller or send only indexPath.
}
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = array[indexPath.row]
return cell
}
Upvotes: 1
Reputation: 469
You have to store the selected rows yourself in a list or array
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//check if self._selectedIndices (your array) contains indexPath.row
//if yes then remove it from array
cell.accessoryType = UITableViewCellAccessoryType.None // remove check mark if you want to use it
//if no then add it to array
cell.accessoryType = UITableViewCellAccessoryType.Checkmark // add check mark if you want to use it
}
then in your prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// use self._selectedIndices to get the selected row indices
// self._dataSource[index] for the actual data
}
Upvotes: 0