2012-05-02 116 views
1

当行大于1时,我有一个uitableview崩溃。这完全没有意义,因为它们是数组中的两个对象。它适用于对象一,但不是两个。检查出:当行数大于1时,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; 
} 

错误:线程1 sigabort当我打电话[self.tableview重载数据];

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

它在哪里崩溃?什么是崩溃报告? – jrturton

+0

你在你的文章中说过,当你重新加载tableView时它崩溃了。首先它是[self.tableView reloadData]而不是[self.tableview重载数据]。另外,你可以粘贴你调用reloadData方法的地方。 – azamsharp

回答

1

使用 return [pro.matches count]

,而不是在您行方法的数量在tableView:numberOfRowsInSection

返回1,因为

+0

仍不能正常工作时 – michaela

2

你的应用程序崩溃,你只返回1.您必须返回[pro.matches count]

+0

我这样做,它崩溃太多 – michaela

+0

一些变化需要做的。看到answr – Saad

2

在你的if([pro.matche s count]> 0)您访问的语句objectAtIndex:1 - 如果匹配数组包含1个对象,则其索引为0,访问索引1会导致应用程序崩溃。

+0

无论哪种方式崩溃... – michaela

1

这里返回数组的大小:

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

取出if发言,并与刚刚取代它:

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

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

+0

是好,但它崩溃 – michaela

+0

然后是另一个问题。 –

+0

不......真的吗?笑只是开玩笑 – michaela

1

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

  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 
+0

为什么是[pro.matches计数1],而不仅仅是[pro.matches对象的指数:指数path.row? – michaela

+0

你能告诉我们错误报告时,它会因未捕获的异常 'NSRangeException',原因崩溃 – sandy

+0

***终止应用程序: '*** - [__ NSArrayI objectAtIndex:]:指数1超出范围[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) 终止叫做抛出异常 – michaela

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

如果仍然崩溃,然后在这里发表您的所有代码。