ankur kumawat ankur kumawat - 3 months ago 7
Objective-C Question

How to pass my id click on CollectionViewCell to other ViewController

I have created gallery view in my app. In that add segment controller , first index is table view and second index is collection view. First index (table view) is proper work in my app, But second index (collection view) show array stored pictures but I want to click on collection view cell and move to another view and pass my UID to another view controller. I tried but could not pass my id. Please help. Thank you

Segue method



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"Tableview"]) {
NSIndexPath *indexPath = [self.table_view indexPathForSelectedRow];
Detail_view *destViewController = segue.destinationViewController;
destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
}
else if ([segue.identifier isEqualToString:@"Collectionview"])
{
NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
collection *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
destViewController.uid_value = [id_ary[indexPath.section] objectAtIndex:indexPath.row];
[self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}
}

Answer

I think you need to improve your code rather then doing split arrays better to user NSObject class manage the array with Object Class for your solution use below code

else if ([segue.identifier isEqualToString:@"Collectionview"])
{
   NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
   collection *destViewController = segue.destinationViewController;
   NSIndexPath *indexPath = [indexPaths firstObject]; // you can also use firstObject rather then ObjectAtIndex:0
   destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
   [self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}