Reputation: 49
I have a table populated by an Entity (tempPrice) of Coredata and a CollectionView populated by another Entity (Participants). The items of collectionView will be selected if the participant of collection is = to participant of tempPrice.
I need to add to this table object when I select an item in a collectionView. Here's my code:
func selectedPartecipants() {
for row in 0..<tableView.numberOfRowsInSection(0) {
let indexPath = NSIndexPath(forRow: row, inSection: 0)
let price = tempPrice[indexPath.row].partecipants?.date!
for item in 0..<collection.numberOfItemsInSection(0) {
let myPath = NSIndexPath(forRow: item, inSection: 0)
let partecipant = partecipantsAtEvent[myPath.row].date!
if price == partecipant {
collection.selectItemAtIndexPath(myPath,
animated: false,
scrollPosition: UICollectionViewScrollPosition.None)
}
}
}
How can I achieve this goal?
Upvotes: 1
Views: 428
Reputation: 4461
I think you need to take another approach.
1) Create an array.
2) Add selected items to array.
3) Once done, populate your tableview from the array.
Much easier. Do you know how to do this ?
If not, here is some code that should help:
var selectedArray = [Price]()
// Here put CODE that checks for selected and add items to array.
func selectedPartecipants() {
let price = tempPrice[indexPath.row].partecipants?.date!
for item in 0..<collection.numberOfItemsInSection(0) {
let myPath = NSIndexPath(forRow: item, inSection: 0)
let partecipant = partecipantsAtEvent[myPath.row].date!
if price == partecipant {
selectedArray.append(partecipant)
}
}
}
//
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("SelectedCell") as? MovieCell{
let selected = selectedArray[indexPath.row]
cell.configureCell(selected)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectedArray.count
}
Finally, to conclude:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedPartecipants()
tableView.reloadData()
}
Upvotes: 2