当行数 >1 时 UITableView 崩溃 [英] UITableView crashes when number of rows >1

查看:39
本文介绍了当行数 >1 时 UITableView 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uitableview 当行大于一时崩溃.这完全没有意义,因为它们是数组中的两个对象.它适用于对象一,但不适用于对象二.看看:

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

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

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

推荐答案

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

In your if([pro.matches count] > 0) statement you access objectAtIndex:1 - if the matches array contains 1 object, it has the index 0, accessing index 1 will crash your app.

这篇关于当行数 >1 时 UITableView 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆