简体   繁体   中英

UITableView crashes when number of rows >1

I have a uitableview that crashes when row is greater than one. It makes no sense at all since their are two objects in the array. It works perfectly for object one, but not two. Check it out:

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

error: thread 1 sigabort on when i call [self.tableview reload data];

*** 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

Your app crashes because in the tableView:numberOfRowsInSection

you return only 1. You must return [pro.matches count]

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

use return [pro.matches count]

instead of return 1 in your number of rows method

Return the array size here:

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

Remove the if statement and replace it with just:

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

indexPath.row is the current row index, starting at zero.

Have you checked whether the string in your array is created and initialized correctly? I think there are a few places you need to check:

  1. Make sure your array is good and neither the array nor the object inside the array has been deallocated when you reference them.

  2. in your -numberOfRowsInSection, it should be return [pro.matches count]

  3. in your -cellForRowAtIndexPath, it should be cell.textLabel.text = [pro.matches objectAtIndex:indexPath.row];

I wrote a demo program, try it out, you will get it. I created a single view app, in the viewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    NSArray *list;
}

@property(nonatomic,retain) NSArray *list;
@end

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

If it still crashes then post all your code here.

Did you ever figure this out? I was having the same problem, and I ended up solving it with the solution posted here: https://stackoverflow.com/a/8096988/810360

In short, I just set my table from static to dynamic:

UITableView属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM