【问题标题】:UITableView crashes when number of rows >1UITableView 在行数 >1 时崩溃
【发布时间】:2012-05-02 15:09:46
【问题描述】:

我有一个 uitableview 在行大于一时崩溃。这根本没有意义,因为它们是数组中的两个对象。它非常适用于对象一,但不适用于对象二。看看吧:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"rigth not %i", [pro.matches count]);

    return 1;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //NSLog(@"%i", indexPath.row);
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // configure your cell here...
    if ([pro.matches count] >0) {
        cell.textLabel.text = [pro.matches objectAtIndex:1];
    }
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}

错误:当我调用 [self.tableview 重新加载数据] 时,线程 1 sigabort 开启;

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x13dc022 0x156dcd6 0x13c8644 0x44cea5 0x2591a8 0x204688 0x206862 0xb466d 0xb4167 0x2a42 0x9a5f 0xa2755e 0x96463e 0x95d1e7 0x95ceea 0x9ed0ad 0x3da2330 0x3da4509 0x1313803 0x1312d84 0x1312c9b 0x12c57d8 0x12c588a 0x26626 0x20ed 0x2055)
terminate called throwing an exception

【问题讨论】:

  • 它在哪里崩溃?什么是崩溃报告?
  • 您在帖子中说过,当您重新加载 tableView 时它会崩溃。首先是 [self.tableView reloadData] 而不是 [self.tableview reload data]。另外,你能粘贴你调用 reloadData 方法的地方吗?

标签: iphone objective-c uitableview nsarray


【解决方案1】:

您的应用崩溃是因为tableView:numberOfRowsInSection

你只返回1。你必须返回[pro.matches count]

【讨论】:

  • 当我这样做时它也会崩溃
  • 还需要做一些更改。看答案
【解决方案2】:

在您的 if([pro.matches count] > 0) 语句中,您访问 objectAtIndex:1 - 如果匹配数组包含 1 个对象,则它的索引为 0 ,访问索引 1 会使您的应用崩溃。

【讨论】:

    【解决方案3】:

    在这里返回数组大小:

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

    删除if 语句并将其替换为:

    cell.textLabel.text = [pro.matches objectAtIndex:indexPath.row];
    

    indexPath.row 是当前行索引,从零开始。

    【讨论】:

    • 那么还有一个问题。
    【解决方案4】:

    使用 return [pro.matches count]

    而不是在您的行数方法中返回 1

    【讨论】:

      【解决方案5】:

      您是否检查过数组中的字符串是否已正确创建和初始化?我认为您需要检查几个地方:

      1. 确保你的数组是好的,当你引用它们时,数组和数组中的对象都没有被释放。

      2. 在您的 -numberOfRowsInSection 中,它应该是 返回 [pro.matches 计数]

      3. 在您的 -cellForRowAtIndexPath 中,它应该是 cell.textLabel.text = [pro.matches objectAtIndex:indexPath.row];

      我写了一个演示程序,试试看,你会明白的。我在 viewController.h 中创建了一个单视图应用程序:

      #import <UIKit/UIKit.h>
      
      @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
      {
          NSArray *list;
      }
      
      @property(nonatomic,retain) NSArray *list;
      @end
      

      在 viewController.m 中,

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      @synthesize list;
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      
          NSArray *tempArray = [[NSArray alloc] initWithObjects:@"a",@"b", nil];
          self.list = tempArray;
          [tempArray release];
      }
      
      - (void)viewDidUnload
      {
          [super viewDidUnload];
          // Release any retained subviews of the main view.
      }
      
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          return YES;
      }
      
      /********************************************************************************
       ******************** UITableViewDataSource Protocol Methods ********************
       ********************************************************************************/
      
      #pragma mark - UITableViewDataSource Protocol Functions
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          //NSLog(@"%i", indexPath.row);
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }
          // configure your cell here...
          if ([list count] >0)
          {
              cell.textLabel.text = [list objectAtIndex:indexPath.row];
          }
      
          [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
          return cell;
      
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          return list.count;
      }
      
      /********************************************************************************
       ******************** UITableViewDelegate Protocol Methods **********************
       ********************************************************************************/
      
      #pragma mark - UITableViewDelegate Protocol Functions
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {}
      @end
      

      【讨论】:

      • 为什么是 [pro.matches count-1] 而不仅仅是 [pro.matches object at index: index path.row?
      • *** 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** 第一次抛出调用堆栈:(0x13dc022 0x156dcd6 0x13c8644 0x44cea5 0x2591a8 0x204688 0x206862 0xb466d 0xb4167 0x2a42 0x9a5f 0xa2755e 0x96463e 0x95d1e7 0x95ceea 0x9ed0ad 0x3da2330 0x3da4509 0x1313803 0x1312d84 0x1312c9b 0x12c57d8 0x12c588a 0x26626 0x20ed 0x2055)终止称为抛出异常跨度>
      • 感谢迈克尔!当数组中有两个对象时,您可以在 nslog 中检查您的数组中是否有两个对象...因为您没有放置我要问的所有代码?
      • 抱歉我弄错了,不是 [pro.matches count-1] 而是 [pro.matches object at index: index path.row]。试试这个演示,不要忘记在你的界面构建器中,将 uitableview 的数据源和委托设置为文件的所有者。
      【解决方案6】:

      你有想过这个吗?我遇到了同样的问题,最后我用这里发布的解决方案解决了这个问题:https://stackoverflow.com/a/8096988/810360

      简而言之,我只是将我的表格从静态设置为动态:

      【讨论】:

        【解决方案7】:
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            NSLog(@"rigth not %i", [pro.matches count]);
        
            return  [pro.matches count];
        }
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {    
            static NSString *CellIdentifier = @"Cell";
        
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            //NSLog(@"%i", indexPath.row);
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            // configure your cell here...
            if ([pro.matches count] >0) {
                cell.textLabel.text = [pro.matches objectAtIndex:indexpath.row];
            }
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            return cell;
        }
        

        如果它仍然崩溃,请在此处发布您的所有代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-11
          • 1970-01-01
          • 2013-01-09
          相关资源
          最近更新 更多