在iOS 5中,TableViewController不滚动而不调用didSelectRowAtIndexPath

时间:2012-02-10 04:17:15

标签: ios5 uitableview

我使用XCode 4.2版本升级到iOS 5。 自从我升级后,我无法创建正确的workign表视图控制器。

1)表视图显示在设备上,但是当我尝试向上滚动时,它崩溃而没有任何错误消息

2)当我尝试选择一个单元格时,不会调用didSelectRowAtIndexPath。

NOTE: the same code was workign for me before upgrating to iOS5.

这是TableViewController.m文件的代码

 #import "CTableViewController.h"
 @implementation CTableViewController

 @synthesize arrNames;

-(id)initWithStyle:(UITableViewStyle)style
 {
    self = [super initWithStyle:style];
    if (self) {
       NSLog(@"CTableViewController::initWithStyle");
     // Custom initialization
}
return self;
}

-(void)viewDidLoad
{
   [super viewDidLoad];

   arrNames = [[NSMutableArray alloc]initWithObjects:@"Peter Frank Brauer",@"Robert Schneider",@"Winfried Walter Kem",@"Eugen Germen Bachle",@"Clara Weib",@"Heinz Guther Winkler",@"Roland Bendel",@"Swen Aschemeyer",@"Patrick Bodingmeier",nil];
 }

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
    // Return the number of sections.
 return 1;
 }

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     // Return the number of rows in the section.
    return [self.arrNames count];
 }

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
cell.textLabel.text = [self.arrNames objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:16];
NSLog(@"CTableViewController::cellForRowAtIndexPath");
return cell;
}

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"called didSelectRow method");

}

@end

谢谢,请帮忙 粟色

4 个答案:

答案 0 :(得分:0)

如果它没有任何错误消息而崩溃,可能是内存管理问题,而且很可能是因为代码的其他部分,或者甚至是因为错误格式化的nslog。

编译器似乎没有注意到一些显而易见的事情,例如为int设置一个分数值,这会立即使应用程序崩溃但没有任何有意义的错误消息。

尝试在代码中查找平凡的东西,而不仅仅是在tableView实现中。

此外,您是否有任何特定原因使用alloc,init(非自动释放)而不是arrayWithObjects填充您的arrNames?

另外:运行静态分析仪!它可能有所帮助。按住Run按钮,将弹出Analyze按钮。

答案 1 :(得分:0)

我将您的源代码复制并粘贴到一个新项目中。我突然发现了一些事情。

您正在继承UITableViewController。尝试只是继承UIViewController,并删除你的initWithStyle方法。

确保在nib中,tableview委托和数据源都附加到File的所有者。

如果您仍然遇到崩溃,那么此代码不是您的问题。

答案 2 :(得分:0)

如果首先崩溃,如何选择行?

顺便说一句,代码似乎不是问题。 重新启动Xcode后尝试此操作:

  1. 转到管理器
  2. 选择项目标签
  3. 点击您打开的项目并删除派生数据。
  4. 然后等待重新编制索引。 (您可以看到顶部条形码和具有新索引目标的代码)。

    就像通常的Clean All Target and Build。

    在类似情况下,它适用于我。 希望它有所帮助。

答案 3 :(得分:-1)

在我的情况下,它是在本地声明的控制器实例,它丢失了委托,所以我在标题中声明了实例,一切正常。