我正在努力通过 UICollectionView
将数据传递给 subview - 基本上我想在 subview 中使用基于项目的相关数据填充几个 UITextField
单击 - 这是我 View 的屏幕截图 -
这里是数组数据的片段,展示了它是如何组合在一起的——
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..
我哪里错了?
最佳答案
你可以用
- (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;
关于IOS - UICollectionView - 通过 segue 将数据传递给 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632332/