Reputation: 4085
I have UI as shown in attached image. It has two components mainly UICollectionView and UITableView.
What I am trying to achieve here when user selects any cell from UITableView, I would like to update Top CollectionViewCell.
What I have done so far.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(![self.selectedIndexPath isEqual:indexPath]){
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndexPath = indexPath;
NSIndexPath *collectionIndex = [NSIndexPath indexPathForItem:0 inSection:self.currentPage];
//TODO:Find Correct Cell
UICollectionViewCell *cellToUpdate = [self.collectionView cellForItemAtIndexPath:collectionIndex];
for (UIView *subView in [cellToUpdate.contentView subviews]) {
if ([subView isKindOfClass:[CustomView class]]) {
CustomView *infoView = (CustomView *)subView;
[infoView updateInfoWithIndex:indexPath.row];
}
}
[self.collectionView reloadData];
}
I think I am doing something wrong here in following line. Not sure what ?
NSIndexPath *collectionIndex = [NSIndexPath indexPathForItem:0 inSection:self.currentPage];
Upvotes: 1
Views: 2282
Reputation: 344
It depends on your implementation, how you have grouped your cells into sections but let's assume you have one section and each element in your array corresponds to a row in your table.
If you wanted to reload a cell corresponding to an array element at index x
all you need to do is write
[collectionView performBatchUpdates:^{
collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:x section:0]];}];
Conversely, if you have the cell but want to find its indexpath you can always call [collectionView indexPathForCell:myCell]
Upvotes: 0
Reputation: 2394
try this code, I make demo for you and its working 100%
@IBOutlet var tblview: UITableView!
let arrm :NSMutableArray = NSMutableArray()
@IBOutlet var collectionview: UICollectionView!
var arrayofdata = [
["image":"2.jpg","title":"Himanshu"],
["image":"1.png","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"1.png","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"1.png","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],
["image":"2.jpg","title":"Himanshu"],]
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayofdata.count;
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath)
profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!)
profileimage.layer.masksToBounds=false
if arrm.containsObject("text\(indexPath.row)") {
profileimage.layer.cornerRadius = profileimage.frame.size.height/2
profileimage.layer.masksToBounds=true
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")
cell?.textLabel?.text = "text\(indexPath.row)"
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if arrm.containsObject("text\(indexPath.row)") {
}else
{
arrm.addObject("text\(indexPath.row)")
}
collectionview.reloadData()
}
Upvotes: 1
Reputation: 6038
I can't show you the code for Obj-C, I haven't touched that in forever. But that answer to your problem should be fairly simple. You need to use delegates.
Basically, the idea is the following :
When you tap any cell from the tableview, you fire an event that says "Something was selected!".
From the point of view or your tableview, this is everything there is to know. You can't let the tableview know there is a collectionview above, and certainly not the content of its cells from the outside.
The cell have to update themselves from the inside. This is key.
So what you do is, when you create those cells, they subscribe to that event (means, they listen to it), and when it is fired, they will react accordingly from the inside.
Since there is only one cell visible, and only one cell that should subscribe, its pretty easy to do, you just subscribe in cellforrow and the cell that will react to will always be correct. If not, you'll have to use indexpaths.
Note that I'm not sure if you're supposed to unsubscribe to those events (otherwise all previously created cell might stay in memory, or even still respond to the event) since I don't really remember how this works in obj-c. I'm guessing there should be an event type that accepts only one subscriber, so use that if you can.
If your cell somehow needs data from the outside, you can pass a parameter to the event before firing it, in the cellSelected.
Once you've set all that up, you're gold.
Upvotes: 0
Reputation: 49354
You should use reloadItems(at indexPaths: [IndexPath])
and just pass the index path you are interested in. Then just do the drawing in your UICollectionViewDataSource
method.
Upvotes: 0