【问题标题】:How to link each UITableViewCell with another view如何将每个 UITableViewCell 与另一个视图链接
【发布时间】:2013-09-28 08:12:46
【问题描述】:

我是 iOS 编程新手,我想做一件简单的事情。我看到了几个关于我的问题的主题,但我不明白为什么我的代码不起作用......

  • 我创建了一个名为 details.xib 的空视图
  • 我创建了一个 Objective-C 类,所以我有两个空文件 details.hdetails.m
  • 在名为 ViewController.xib 的主视图中,我有一个 tableView
  • ViewController.m 中,我在顶部添加了:#import "details.h"
  • ViewController.m 中,我像这样修改了 didSelectRowAtIndexPath 方法:

    details *det = [[details alloc] init];
    [self.navigationController pushViewController:det animated:YES];
    

我得到了这个通知:不兼容的指针类型将'details *__strong'发送到'UIViewController *'类型的参数

对不起,如果我的英语不好,我是法国人...谢谢你的帮助!

【问题讨论】:

  • details 是 UIView 还是 UIViewController?
  • det 用作属性。

标签: iphone ios uitableview view cell


【解决方案1】:

检查您是否在文件所有者中正确地植根于 details.xib 和类。

参考这张图片:

并确保您的详细视图控制器是 UIViewController 的子类,如果不是,请创建一个视图控制器作为 UIViewController 子类(在创建视图控制器时您可以自己选择。参考下图)

【讨论】:

  • 你确定detail view是UIViewcontroller的子类吗?
  • 你的 details.h @interface details : UIViewController 和这行一模一样?
  • 我用向导创建了一个名为 details2ViewController 的新类,这是肯定的。但它没有任何改变......
【解决方案2】:
 NSMutableArray *MenuArray=[[NSMutableArray alloc] init];

 //Populate your array dynamically. Here I have populate my array with a custom object
 [MenuArray addObject:[[GlobalMethod alloc] initWithMenuName:@"Home" WithNibNumber:1]];
 [MenuArray addObject:[[GlobalMethod alloc] initWithMenuName:@"Profile" WithNibNumber:2]];
 [MenuArray addObject:[[GlobalMethod alloc] initWithMenuName:@"Friends" WithNibNumber:3]];
 [MenuArray addObject:[[GlobalMethod alloc] initWithMenuName:@"Photos" WithNibNumber:4]];

 // And so on.....


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [MenuArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   GlobalMethod *Method=[MenuArray objectAtIndex:[indexPath row]];
   UITableViewCell *cell;
   [cell setTag:[Method NibNumber]];
   return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
   if([cell tag]==1)
   {
      //push to viewController1
   }
   else if([cell tag]==2)
   {
      //push to  viewController12
   }
   else
   {
      //and go on
   }
}

【讨论】:

  • 我不能这样做,因为我的 TableView 是动态填充的。
  • 检查我更新的答案。我希望你能明白你必须做什么
  • 你能告诉我你的代码吗...我的意思是你试图做什么?
【解决方案3】:

编译器告诉你:“我期待这个 presentViewController:animated: 方法中有一些 UIViewController 子类,但它不是”

detail 确实应该是 detailViewController,子类化 UIViewController

编辑

转到你的detail类头文件(detail.h),确保它继承自UIViewController

@interface detail : UIViewController

//properties & methods declarations...

@end

【讨论】:

  • 我明白你在说什么,但我该怎么做呢?抱歉,我对 iOS 还不放心……
  • 那么也许你应该花更多时间做教程,然后再冲上 stackoverflow...我编辑了我的答案以使其更明确:)
  • 所以您不应该收到警告不兼容的指针类型将“详细信息 *__strong”发送到“UIViewController *”类型的参数,对吗?
  • 那么这里[self.navigationController pushViewController:det animated:YES];发生了一些事情,请确保您的ViewController确实嵌入在UINavigationController中,如果没有,您可以在调试器中看到它:self.navigationController将为nil。
  • 确实,self.navigationController 的值为空。所以我必须在我的 details2ViewController.xib 中添加一个 UINavigationController ?
猜你喜欢
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2014-12-20
相关资源
最近更新 更多