phi*_*son 8 ios uistoryboard uicollectionview
我在导航控制器中嵌入了一个UICollectionView控制器.collectionView列出项目,每个单元格应该转到ProjectDetail屏幕.
我根本无法触发segue.如果我只是在导航栏上放下一个按钮并将一个segue连接到细节,它就可以工作.但是从我的CollectionView单元格触发却没有.
这是故事板的样子:http://cl.ly/RfcM 我有一个从CollectionViewCell连接到ProjectDetailViewController的segue
这是我的ProjectDetailViewController中的相关代码:
@interface ProjectCollectionViewController () {
NSArray *feedPhotos;
Projects *projects;
}
@end
@implementation ProjectCollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[FeedViewCell class] forCellWithReuseIdentifier:@"cell"];
[self loadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"selected %d", indexPath.row);
Project *project = [projects getProject:indexPath.row];
NSLog(@"project = %@", project);
}
- (void)loadData {
[self.projectLoader loadFeed:self.username
onSuccess:^(Projects *loadedProjects) {
NSLog(@"view did load on success : projects %@", loadedProjects);
projects = loadedProjects;
[self.collectionView reloadData];
}
onFailure:^(NSError *error) {
[self handleConnectionError:error];
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return projects.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
UIImageView *cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
Project *project = [projects getProject:indexPath.row];
NSString *imageUrl = [project coverPhotoUrl:200 forHeight:200];
NSLog(@"imageurl =>%@", imageUrl);
if (imageUrl) {
[cellImageView setImageWithURL:[NSURL URLWithString:imageUrl]];
}
[cell addSubview:cellImageView];
cell.imageView = cellImageView;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我猜这个问题在于我如何将Cell连接到CollectionView.
任何帮助将不胜感激!
Pul*_*yal 21
您无法直接从故事板中的单元格创建segue,因为集合视图是通过数据源动态填充的.您应该使用collectionView:didSelectItemAtIndexPath:和以编程方式执行segue performSegueWithIdentifier:sender:.像这样的东西:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"MySegueIdentifier" sender:self];
}
Run Code Online (Sandbox Code Playgroud)
其中MySegueIdentifier是故事板中定义的segue的标识符.
| 归档时间: |
|
| 查看次数: |
10587 次 |
| 最近记录: |