【问题标题】:UICollectionView embedded in a UITableViewCell ! Cannot present a viewcontroller from the tableview cell ! With a call backUICollectionView 嵌入在 UITableViewCell 中!无法从 tableview 单元中显示 viewcontroller !回电
【发布时间】: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


【解决方案1】:

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

typedef void(^categorycallback)();

在你的 tableviewcell 标题中给出这个代码

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

【讨论】:

    【解决方案2】:

    在您的CategoryTableViewCell 实现中,方法didSelectItemAtIndexPath 包含这行代码:

    [self presentViewController:monitorMenuViewController animated:NO completion:nil];
    

    这里,self 指的是您的CategoryTableViewCell,它不是UIViewController 的子类,因此您不能在其上真正呈现任何内容。但是,您可以做的是访问包含您的CategoryTableViewCellUITableView,然后访问持有它的UIViewController(或UITableViewController)。

    这种东西应该可以工作:

    UITableView *tableView = self.superview.superview;
    UIViewController *viewController = tableView.dataSource;
    [viewController presentViewController:monitorMenuViewController animated:NO completion:nil];
    

    请注意,Apple 可以随时更改视图层次结构,因此在即将发布的 iOS 版本中,self.superview.superview 可能不会为您提供包含您的单元格的UITableView。所以这是一种混乱的做法,但对于当前版本的 iOS 应该不会造成任何问题。

    【讨论】:

      【解决方案3】:

      为 tableviewcell 编写委托方法,然后将此委托设置为视图控制器,然后通过您在视图控制器上实现的委托方法将集合视图项目选择消息传递给视图控制器

      将您的子类 tableViewcell 上的以下协议方法声明为委托方法

      -(void)tableViewcell:(id)sender didSelectCollectionItemAtIndexPath:(NSIndexpath*)path;
      

      借助上述方法,您可以将选定的 indexpath 传递给 viewController

      【讨论】:

      • 如何将collectionview项目选择消息传递给viewcontroller??我尝试在 collectionview 的 didselect 上的 tableview 方法中放置断点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多