简体   繁体   中英

IOS - UICollectionView

I have the following code which works out which section/data index to pass to a destination controller based on the clicked cell.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {


    self.selectedItIndex = indexPath.row; // define NSInteger selectedItemIndex property in your interface
    self.selectedSection = indexPath.section;



}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"sightSeg"]) {

    NSString *s = self.sectionData[_selectedSection];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"siteSection like %@", s];
    NSArray *filteredArr = [self.sightsArray filteredArrayUsingPredicate:pred];
    NSLog(@"Value of hello = %@", filteredArr);
    sightsObject *rightS = filteredArr[_selectedItIndex];
    _finNo = _selectedItIndex +=1;
    NSLog(@"balls = %@",rightS );
     SightViewController *destController = (SightViewController *)segue.destinationViewController;
    destController.viewTit = rightS.siteTitle;
}

}

The code works fine first try (as in the correct data is passed relevant to the cell/section clicked) - the issue I have is that if you return to the parent controller and select another cell - the same information is passed again - if you repeat the process selecting a 3rd cell - the data for the second cell clicked is passed - so its as though its a step out of sync!?

Can anyone offer any advice? - Is there a way to only run the sgue code once the values have been set in the didselectpath method?

The sender parameter in prepareForSegue will be the collection view cell you've tapped. Use the following code to derive your index at the point of sending:

NSIndexPath *selectedIndexPath = [self.collectionView indexPathForCell:sender];

You don't need the code in didSelectItem...

尝试在prepareForSegue:内调用[collectionView indexPathsForSelectedItems]以获取索引路径,而不是将其保存在collectionView:didSelectItemAtIndexPath:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM