当行数> 1时,UITableView崩溃

时间:2012-05-02 15:09:47

标签: iphone objective-c uitableview nsarray

我有一个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 reload data]时,线程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

7 个答案:

答案 0 :(得分:2)

您的应用在tableView:numberOfRowsInSection

中崩溃了

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

使用 return [pro.matches count]

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

答案 3 :(得分:1)

返回数组大小:

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

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

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

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

答案 4 :(得分:1)

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

  1. 确保您的数组正常,并且在引用它们时数组和对象内的对象都没有被释放。

  2. 你的-numberOfRowsInSection中的
  3. 应该是 return [pro.matches count]

  4. 你的-cellForRowAtIndexPath中的
  5. 应该是 cell.textLabel.text = [pro.matches objectAtIndex:indexPath.row];

  6. 我写了一个演示程序,试试看,你会得到它。我在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
    

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

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

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

UITableView Properties