IOS - UICollectionView - 通过segue将数据传递给子视图

时间:2014-05-13 13:15:05

标签: ios objective-c

我正在努力通过UICollectionView将数据传递到子视图 - 基本上我想在子视图中填充多个UITextField,并根据点击的项目填充相关数据 - 这是我的观点的截图 -

enter image description here

这是一个剪辑的数组数据,以显示它是如何放在一起的 -

 sightsObject *sight13 = [[sightsObject alloc] initWithsiteTitle:@"ssd"   siteSection:@"views" siteType:@"" siteSubTitle:@"" siteDescription:@"" siteMapUrl:@"docks" sitePic:@"further"];

数组当前是静态的 - 所以编译如下 -

 self.sightsArray = [NSArray arrayWithObjects:sight1, sight2, sight3, sight4, sight5, sight6, sight7, sight8,sight9,sight10, sight11, sight12,sight13, nil];

 self.sectionData = [NSArray arrayWithObjects:@"beatles", @"docks",@"unique",@"museums", @"Football",@"Tours",@"Views",@"Further Afield", nil];

uiCollectionView的填充如下 -

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

_selectedItemIndex = indexPath.row;


CSCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
                                                        forIndexPath:indexPath]
NSString *s = self.sectionData[indexPath.section];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"siteSection like %@", s];
NSArray *filteredArr = [self.sightsArray filteredArrayUsingPredicate:pred];
NSLog(@"Value of hello = %@", filteredArr);
sightsObject *rightS = filteredArr[indexPath.row];
cell.textLabel.text =rightS.siteTitle;
cell.textBrief.text = rightS.siteSubTitle;
cell.imageIco.image = [UIImage imageNamed:rightS.sitePic];

return cell; 

}

我已将选定的索引路径存储在此方法中 -

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


   self.selectedItemIndex = indexPath.row; //stored as NSInteger

}

最后我尝试传递数据如下 -

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


    NSArray* selectedItem = [_sightsArray objectAtIndex:self.selectedItemIndex ];

    SightViewController *destController = [segue destinationViewController];
    destController.viewTit = selectedItem.siteTitle ;




}

但是我收到一条错误,指出在NSArray上找不到属性siteTitle ..

我哪里错了?

2 个答案:

答案 0 :(得分:1)

您可以使用

实现相同目标
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"FirstView"]) {
        NSString* selectedItem = [_sightsArray objectAtIndex:self.selectedItemIndex];
        SightViewController *destController = (SightViewController *)segue.destinationViewController;
        destController.selString = selectedItem;
    }
}

在desController.h文件中将字符串变量声明为

@property(nonatomic, copy) NSString *selString;

答案 1 :(得分:1)

您的_sightArray包含sightsObject类型的对象。尝试:

sightsObject *selectedItem = [_sightsArray objectAtIndex:self.selectedItemIndex ];