【发布时间】:2015-10-22 20:23:10
【问题描述】:
我有一个 UICollectionView,它嵌入在 UITableViewCell 中。 UICollectionView 的委托和数据源都在 UITableViewCell 类中给出。我需要在单击UICollectionView 中的项目时显示UIViewController。但是由于collectionview 的属性是写在UITableViewCell 中的,所以我不能从中真正呈现另一个UIViewController。请帮忙!
tableView 代表:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return books.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = @"CategoryTableViewCell";
CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
dict = [[NSDictionary alloc]init];
dict = books[indexPath.row];
NSString *categoryID = [dict[@"category"] stringValue];
cell.booksArray = dict[@"books"];
cell.categoryNameLabel.text = [self getCategoryNameForCategory:categoryID];
[cell.collectionView reloadData];
return cell;
}
tableview 单元内的 collectionView 代表。
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _booksArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"bookCollectionViewCell";
BookCollectionViewCell *cell = (BookCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
dict = [[NSDictionary alloc]init];
dict = _booksArray[indexPath.item];
NSString *imgString = [NSString stringWithFormat:@"http://test.valmeeki.com/images/front_covers/%@.jpg",dict[@"id"]];
NSUserDefaults *prefs = [[NSUserDefaults alloc]init];
[prefs setObject:imgString forKey:@"bookcoverimg"];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imgString]
placeholderImage:[UIImage imageNamed:@"nik_bg_01.png"]];
cell.costLabel.text = [NSString stringWithFormat:@"Rs %@",[dict[@"price"] stringValue]];
cell.titleLabel.text = dict[@"title"];
cell.authoLabel.text = dict[@"authorName"];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults *prefs = [[NSUserDefaults alloc] init];
[prefs setObject:dict[@"title"] forKey:@"booktitle"];
[prefs setObject:dict[@"authorName"] forKey:@"authorname"];
[prefs setObject:dict[@"numDownloads"] forKey:@"bookdownloads"];
[prefs setObject:dict[@"rating"] forKey:@"bookratings"];
[prefs setObject:dict[@"price"] forKey:@"bookprice"];
[prefs setObject:dict[@"synopsis"] forKey:@"booksynopsis"];
[prefs setObject:dict[@"id"] forKey:@"bookid"];
NSLog(@"%@",dict[@"title"]);
SideMenuCategories *monitorMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SideMenuCategories"];
[self presentViewController:monitorMenuViewController animated:NO completion:nil];
}
【问题讨论】:
-
你能提供一些代码吗? “我不能展示另一个视图控制器”并不能解释太多。
-
当我尝试从 tableviewcell 中显示视图控制器时,它显示错误故事板属性不是 tableviewcell 的对象
-
在您提供为实现此目的而编写的代码之前,没有人可以对您的问题发表任何意见。
-
嗨 halileohalilei 我已经编辑了我的问题,请帮助我!
标签: ios iphone xcode uitableview uicollectionview