tableView numberOfRowsInSection

时间:2011-06-22 19:25:52

标签: iphone xcode tableview

我在xcode中有这个错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:

我使用此代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    int num_rows = 0;

    switch (section) {
        case 0:{
            num_rows = [messageReceive count];
        }
            break;
        case 1:{
            num_rows = [messageSend count];
        }
            break;


    return num_rows;    
}

但我尝试了很多代码,但同样的错误。我使用UITableView从脚本php获取带有Json的消息。在“messageReceive”中,有时我有0或1或许多消息。对于messageSend也一样。

thx

这是我的cellForRowAtIndexPath

// Configure the cell.


    NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
    NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];

    switch ( indexPath.section )
    {
        case 0: 
            if (pseudoLabel.text =[dictMessSend  objectForKey:@"sender"] )

    {cell.detailTextLabel.text = [dictMessSend  objectForKey:@"receiver"];
    cell.text= [dictMessSend  objectForKey:@"message"];
            }
            break;


        case 1:             
            if (pseudoLabel.text =[dictMessReceive  objectForKey:@"receiver"] ) 
            {cell.detailTextLabel.text = [dictMessReceive  objectForKey:@"sender"];
        cell.text= [dictMessReceive  objectForKey:@"message"];

                }

            else {
                //cell.text = @"No message";
            }

            break;

    }   


    return cell;

2 个答案:

答案 0 :(得分:2)

提出的例外情况在哪里?除非您实现了自定义计数方法,否则您发布的代码中的任何位置都不会引发该异常。所以我将假设在tableView:cellForRowAtIndexPath:中引发了异常。在该方法(或任何其他类似方法)内部,请确保使用相同的逻辑。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:{
            //messageReceive logic
        }
        break;
        case 1:{
            //messageSend logic
        }
        break;
    }
}

您要做的下一件事是确保messageReceive和messageSend不可变(您的控制台输出显示它是可变的)。如果它们是属性,则它们应该是NSArray并设置为不保留复制(例如@property(nonatomic, copy) NSArray *messageReceive;,否则代码应该看起来像messageReceive = [sourceArray copy];

<强>更新

您提供的更新显示问题出在以下一行

NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];

您无法访问具有相同索引的两个阵列,将其移至switch语句

NSDictionary * dictMessReceive = nil;
NSDictionary * dictMessSend = nil;

switch ( indexPath.section )
{
    case 0: 
        dictMessageReceive = [self.messageReceive objectAtIndex:indexPath.row];
        //...
        break;
    case 1:
        dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
        //...
        break;
}

答案 1 :(得分:0)

发布你的cellForRowAtIndexPath方法,那就是你出错的地方,你可能在某些部分使用了错误的数组......所以对于第0部分,你使用收到的消息(可能是5)和你正在读取的cellForRowAtIndexPath发送的消息数小于5,所以你得到了越界异常..