繁体   English   中英

iOS-UICollectionView

[英]IOS - UICollectionView

我有以下代码,该代码根据单击的单元格确定将哪个节/数据索引传递给目标控制器。

- (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;
}

}

该代码可以很好地进行首次尝试(因为传递了与单击的单元格/节有关的正确数据)-我遇到的问题是,如果您返回到父控制器并选择另一个单元格,则再次传递相同的信息-如果您重复选择第三个单元格的过程-单击的第二个单元格的数据已传递-这样就好像是不同步!

谁能提供任何建议? -是否只有在didselectpath方法中设置了值后才可以运行sgue代码的方法?

prepareForSeguesender参数将是您点击的集合视图单元格。 使用以下代码在发送时派生索引:

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

您不需要didSelectItem...的代码didSelectItem...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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