tableview在单击单元格或在ios 5中滚动时崩溃

时间:2012-04-10 07:06:36

标签: iphone objective-c uitableview ios5

我做了一个演示空应用并添加了一个带视图的导航控制器

UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:objFirstViewControllerViewController];
[self.window addSubview:navBar.view];

之后,我在第一个视图控制器上添加了一个表格视图。

UITableView* demotableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
demotableView.delegate = self;
demotableView.dataSource = self;
[self.view addSubview:demotableView];

和表格视图的委托函数和行函数的主单元格就像这样

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  cell.textLabel.text = @"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:@"Copperplate" size:18];
return cell;
}

但是当我滚动我的桌子或点击任何单元格进入下一个视图时,它只会崩溃并分别在点击和滚动时给出这两个错误。

[__NSCFArray tableView:didSelectRowAtIndexPath:]
[__NSCFDictionary tableView:cellForRowAtIndexPath:]

我不明白这个代码一直在使用先前的os正确处理

任何人都可以帮忙吗?

以下是选择行

的代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];

}

并且没有这一行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 15;
}

4 个答案:

答案 0 :(得分:1)

实际上我误用了ARC我为使应用程序成功运行所做的更改实际上由于内存泄漏导致崩溃我在委托中使用本地对象引用了类但是当它尝试添加数据时它被释放了当表的委托和数据源尝试添加当前类中的东西时它被释放并且它从这些消息实例中抛出消息,我被卡住了因为我认为它正在发生,因为我采取了一种空的应用程序但是之后在委托类中添加以下行我解决了问题。

我在.h文件的委托类中做了什么:

 FirstViewControllerViewController *objFirstViewControllerViewController;

@property (strong, nonatomic)  FirstViewControllerViewController *objFirstViewControllerViewController;

然后我的桌子开始表现正常,所有我遇到问题的事情。

答案 1 :(得分:0)

用此替换代码并尝试:

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

  }
  cell.textLabel.text = @"Hello this is sample text";

  cell.textLabel.minimumFontSize = 12;

  cell.textLabel.adjustsFontSizeToFitWidth = TRUE;

  cell.textLabel.font = [UIFont fontWithName:@"Copperplate" size:18];

  return cell;
}

希望这有帮助。

答案 2 :(得分:0)

因为您设置了demotableView.delegate = self; 你必须实现tabelView: didSelectRowAtIndexPath:函数来解决选择(点击)一个单元格时的崩溃。

要解决滚动崩溃问题,您必须实施

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

方法

P.S:在cellForRowAtIndexPath:中,除了cell.textLabel.text之外的所有行都应该在

之内
if(cell == nil){
}

请遵循适当的记事管理规则

答案 3 :(得分:0)

请试试这个......

像这样添加你的表格视图..

 productList = [[UITableView alloc] initWithFrame:CGRectMake(0,102, 320, 267) style:UITableViewStylePlain];
 productList.delegate = self;
 productList.dataSource = self;
 productList.backgroundColor = [UIColor clearColor];
 [self.view addSubview:productList];

并添加这些方法......

  #pragma mark Table view data source

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


 // Customize the number of rows in the table view.
   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return (your row count);
    }


 // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

 // Configure the cell.

         cell.textLabel.text = @"Title";
         cell.detailTextLabel.text = formattedString;
         cell.detailTextLabel.textColor = [UIColor darkGrayColor];

         return cell;

      }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
     [self.navigationController pushViewController:anotherViewController animated:YES];

   }