[英]UICollectionView embedded in a UITableViewCell ! Cannot present a viewcontroller from the tableview cell ! With a call back
I have a UICollectionView
which is embedded in a UITableViewCell
. 我有一个
UICollectionView
嵌入在UITableViewCell
。 The delegates and datasources of the UICollectionView
are all given in the UITableViewCell
class . UICollectionView
的委托和数据源都在UITableViewCell
类中给出。 I need to present a UIViewController
on the click of an item in the UICollectionView
. 我需要在
UICollectionView
单击某个项目时提供一个UIViewController
。 But as the properties of the collectionview
are written in the UITableViewCell
I can't really present another UIViewController
from that. 但是,由于
collectionview
的属性是在UITableViewCell
中编写的,因此我不能真正从中呈现另一个UIViewController
。 Please help! 请帮忙!
The tableView delegates: 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;
}
The collectionView delegates which is inside the tableview 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];
}
Hi i have fixed that issue !! 嗨,我已经解决了这个问题!
typedef void(^categorycallback)();
Give this code in your tableviewcell header 在您的tableviewcell标头中提供此代码
Viewcontrollertobecalled *getcall = [[Viewcontrollertobecalled alloc] init];
[getcall callthehome];
if(self.callback)
{
self.callback();
}
Add this code in the didselectrow of the tableview in tableviewcell.m 将此代码添加到tableviewcell.m中tableview的didselectrow中
Catch the call back in the Viewcontroller class and give your code to present your viewcontroller . 在Viewcontroller类中捕获该回调,并提供代码以呈现viewcontroller。
cell.callback = ^(){
NavigationViewStore *questionnaireNavController = [self.storyboard instantiateViewControllerWithIdentifier:@"NavigationViewStore"];
[questionnaireNavController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
HomeViewController *qvc = (HomeViewController *)[questionnaireNavController topViewController];
[self presentViewController:questionnaireNavController animated:NO completion:nil];
};
Write Delegate methods for tableviewcell then set this delegate to view controller then pass the collectionview item selection message to view controller through the delegate methods you have implemented on the viewcontroller 为tableviewcell编写Delegate方法,然后将此委托设置为视图控制器,然后通过您在viewcontroller上实现的委托方法将collectionview项目选择消息传递给视图控制器
declare the below protocol methods on your subclassed tableViewcell as a delegate method 在子类tableViewcell上将以下协议方法声明为委托方法
-(void)tableViewcell:(id)sender didSelectCollectionItemAtIndexPath:(NSIndexpath*)path;
with the help of the above method you can pass the selected indexpath to the viewController 借助上述方法,您可以将选定的indexpath传递给viewController
In your CategoryTableViewCell
implementation, the method didSelectItemAtIndexPath
contains this line of code: 在您的
CategoryTableViewCell
实现中,方法didSelectItemAtIndexPath
包含以下代码行:
[self presentViewController:monitorMenuViewController animated:NO completion:nil];
Here, self
refers to your CategoryTableViewCell
, which is not a subclass of UIViewController
, therefore you can't really present anything on it. 在这里,
self
指的是CategoryTableViewCell
,它不是UIViewController
的子类,因此您无法在其上显示任何内容。 What you can do is, however, you can access the UITableView
that contains your CategoryTableViewCell
, and then access the UIViewController
(or UITableViewController
) holding it. 但是,您可以执行的操作是,访问包含
CategoryTableViewCell
的UITableView
,然后访问保存它的UIViewController
(或UITableViewController
)。
Something of this sort should work: 这种事情应该起作用:
UITableView *tableView = self.superview.superview;
UIViewController *viewController = tableView.dataSource;
[viewController presentViewController:monitorMenuViewController animated:NO completion:nil];
And please note that Apple can change the view hierarchy at anytimes, so in the upcoming versions of iOS, self.superview.superview
might not give you the UITableView
containing your cell. 并且请注意,Apple可以随时更改视图层次结构,因此在即将发布的iOS版本中,
self.superview.superview
可能不会为您提供包含单元格的UITableView
。 So this is a messy way of doing this, but it shouldn't cause any problem for the current version of iOS. 因此,这是一种麻烦的方式,但是对于当前版本的iOS,这不会造成任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.