应用程序在reloadData上崩溃

时间:2014-05-22 01:36:17

标签: ios objective-c uitableview

*感谢Paulw11和sjeohp评论,我明白了。当我改为在静态文本上使用数组时,我正在使用文本量来更改单元格高度并且没有更正该方法中的objectAtIndex。 *

我有一个聊天应用程序,我试图使用tableView来显示聊天交互。我将“源”和内容保存到一个数组中,并试图让tableview重新加载,无论何时发送或接收消息,但它每次都在reloadData调用时崩溃。

在ViewDidLoad中:

chatLog = [[NSMutableArray alloc]init];

然后是其他方法:

-(IBAction)sendMessagePressed:(UIBarButtonItem *)sender {

    NSLog(@"Send Pressed");

    // if the message bar isnt blank send the message
    if ( ![messageInputBar.text isEqualToString:@""] ) {

        NSString* text = [NSString stringWithFormat:@"You Wrote:\n%@", messageInputBar.text];

        NSString* source = @"self";

        NSArray* sent = [[NSArray alloc]initWithObjects:source, text, nil];

        messageInputBar.text = @"";

        [self CloseMessageKeys];



        [chatLog addObject:sent];


        // this is where it crashes. this code was working with static text before
        // i implemented the mutable array
        [chatTable reloadData]; 


        NSData* messageData = [messageInputBar.text dataUsingEncoding:NSUTF8StringEncoding];

        NSArray* connectedPeers = _app_Delegate.mpc_Handler.mpc_session.connectedPeers;

        NSError* error;

        [_app_Delegate.mpc_Handler.mpc_session sendData:messageData toPeers:connectedPeers withMode:MCSessionSendDataReliable error:&error];

        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        }


    }

}

这是TableView cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ( [[[chatLog objectAtIndex:indexPath.row]objectAtIndex:0]isEqualToString:@"self"]) {


        OutgoingTableViewCell* cell = [chatTable dequeueReusableCellWithIdentifier:@"outgoingCell" forIndexPath:indexPath];

        NSString* text = [[chatLog objectAtIndex:indexPath.row]objectAtIndex:1];

        cell.txtLabel.text = text;

        [[cell txtLabel] setNumberOfLines:0]; // unlimited number of lines

        [[cell txtLabel] setLineBreakMode: NSLineBreakByWordWrapping];

        [[cell txtLabel] setFont:[UIFont systemFontOfSize: 14.0]];

        cell.backgroundColor = [UIColor greenColor];

        [self showTheBottom];

        return cell;

    } else {

        IncomingTableViewCell* cell = [chatTable dequeueReusableCellWithIdentifier:@"incomingCell" forIndexPath:indexPath];

        NSString* text = [[chatLog objectAtIndex:indexPath.row]objectAtIndex:1];

        cell.txtLabel.text = text;

        [[cell txtLabel] setNumberOfLines:0]; // unlimited number of lines

        [[cell txtLabel] setLineBreakMode: NSLineBreakByWordWrapping];

        [[cell txtLabel] setFont:[UIFont systemFontOfSize: 14.0]];

        cell.backgroundColor = [UIColor blueColor];

        [self showTheBottom];

        return cell;
    }

}

3 个答案:

答案 0 :(得分:1)

长度不是NSArray上的方法。使用[数组计数]

答案 1 :(得分:0)

感谢@ Paulw11和@sjeohp发表评论。当我改为在静态文本上使用数组时,我正在使用文本量来更改单元格高度并且没有更正该方法中的objectAtIndex。 *

我用另一种方法:

 NSAttributedString *aString = [NSAttributedString alloc] initWithString:[chatLog objectAtIndex:indexPath.row];

它必须是:

 NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[[chatLog objectAtIndex:indexPath.row]objectAtIndex:1]];

感谢@ Paulw11和@sjeohp帮助我找出Xcode中的异常断点功能

答案 2 :(得分:-1)

您的dequeueReusableCell代码失败。你需要检查它是否为初始单元格。并检查numberOfRowsInSection代码以返回chatLog.count