Ios 应用程序在重新加载数据时崩溃
*多亏了Paulw11和sjeohp的评论,我才明白了这一点。我使用文本量来更改单元格高度,当我更改为在静态文本上使用数组时,没有在该方法中更正objectAtIndex*强> 我有一个聊天应用程序,我正试图使用tableView来显示聊天互动。我正在将“源”和内容保存到一个数组中,并试图让tableview在发送或接收消息时重新加载,但每次重新加载数据调用时都会崩溃 在ViewDidLoad中:Ios 应用程序在重新加载数据时崩溃,ios,objective-c,uitableview,Ios,Objective C,Uitableview,*多亏了Paulw11和sjeohp的评论,我才明白了这一点。我使用文本量来更改单元格高度,当我更改为在静态文本上使用数组时,没有在该方法中更正objectAtIndex* 我有一个聊天应用程序,我正试图使用tableView来显示聊天互动。我正在将“源”和内容保存到一个数组中,并试图让tableview在发送或接收消息时重新加载,但每次重新加载数据调用时都会崩溃 在ViewDidLoad中: chatLog = [[NSMutableArray alloc]init]; 然后是其他方法: -
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 CellForRowAtIndex路径:
-(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;
}
}
长度不是NSArray上的方法。使用[array count]您的出列可重用单元代码失败。您需要检查它是否为nil,然后是init cell。并检查numberOfRowsInSection代码以返回聊天记录。count多亏了@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中的异常断点功能异常消息是什么,它在哪一行崩溃?-[\uu NSArrayI length]:发送到实例0xbeb3770的未识别选择器***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[\uu NSArrayI length]:发送到实例0xbeb3770的无法识别的选择器'我猜您的代码中的其他地方,可能是numberOfRowsInSection,您正在调用[array length]。您需要使用[array count]。此外,当它崩溃时,它崩溃的那一行应该以绿色突出显示。我认为@sjeohp是正确的,但要了解解决崩溃的一般信息,请阅读此内容-特别是,在本例中,设置一个异常断点。在本例中,他们使用的是dequeueReusableCellWithIdentifier:forIndexPath:只要正确注册了nib或类,此方法将始终返回有效的单元格。您只需使用dequeueReusableCellWithIdentifier检查nil: