gpt4 book ai didi

ios - UICollectionView 嵌入在 UITableViewCell 中!无法从 tableview 单元格中呈现 viewcontroller!回电

转载 作者:行者123 更新时间:2023-11-29 12:17:07 34 4
gpt4 key购买 nike

我有一个嵌入在 UITableViewCell 中的 UICollectionViewUICollectionView 的委托(delegate)和数据源都在 UITableViewCell 类中给出。我需要在单击 UICollectionView 中的项目时显示一个 UIViewController。但是由于 collectionview 的属性是在 UITableViewCell 中编写的,所以我无法真正从中呈现另一个 UIViewController。请帮忙!

tableView 委托(delegate):

- (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 委托(delegate)。

- (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];


}

最佳答案

您好,我已经解决了这个问题!!

typedef void(^categorycallback)();

在您的 tableviewcell header 中提供此代码

 Viewcontrollertobecalled *getcall = [[Viewcontrollertobecalled alloc] init];
[getcall callthehome];
if(self.callback)
{
self.callback();

}

在tableviewcell.m中tableview的didselectrow中添加这段代码

在 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];
};

关于ios - UICollectionView 嵌入在 UITableViewCell 中!无法从 tableview 单元格中呈现 viewcontroller!回电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741369/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com