【问题标题】:tableView numberOfRowsInSectiontableView numberOfRowsInSection
【发布时间】:2011-06-22 19:25:51
【问题描述】:

我在 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 从带有 Json 的脚本 php 中获取消息。在“messageReceive”中,有时我有 0 条或 1 条或多条消息。 messageSend 也是如此。

谢谢

这是我的 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;

【问题讨论】:

  • 贴出 cellForRowAtIndexPath 函数的代码。异常很可能来自那里。
  • 你的 tableView:cellForRowAtIndexPath: 方法是什么样子的
  • "tableView numberOfRowsInSection" 不是一个好的问题标题

标签: iphone xcode tableview


【解决方案1】:

在哪里引发了异常?除非您实现了自定义计数方法,否则在您发布的代码中的任何地方都不会引发该异常。所以我会假设在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;
}

【讨论】:

  • 最后一个问题:如何在字符串中设置我的 [messageReceive count]?在 numberOfRowsInSection 中,我可以在我的 NSLOG 中看到我的 messageReceive 计数中有多少行: if([messageReceive count]!=0){ NSLog(@"message: %d",[messageReceive count] );
  • NSString *mrcount = [NSString stringWithFormat:@"%d", [messageReceive count]];
【解决方案2】:

发布您的 cellForRowAtIndexPath 方法,这就是您出错的地方,您可能在某些部分使用了错误的数组...因此,对于第 0 部分,您使用收到的消息(可能是 5),而在 cellForRowAtIndexPath 中,您正在从messages sent array 少于 5,所以你得到了越界异常..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多