Reputation: 105
* I got it figured out thanks to Paulw11 and sjeohp comments. I was using the amount of text to change the cell height and didnt correct the objectAtIndex in that method when i changed to using the array over static text. *
I have a chat app that im trying to use a tableView to show the chat interaction. Im saving the "source" and the contents into an array and trying to get the tableview to reload that anytime a message is sent or received but it crashes everytime on the reloadData call.
In the ViewDidLoad:
chatLog = [[NSMutableArray alloc]init];
and then other methods:
-(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]);
}
}
}
This is the 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;
}
}
Upvotes: 0
Views: 179
Reputation: 105
I got it figured out thanks to @Paulw11 and @sjeohp comments. I was using the amount of text to change the cell height and didnt correct the objectAtIndex in that method when i changed to using the array over static text. *
in another method I had:
NSAttributedString *aString = [NSAttributedString alloc] initWithString:[chatLog objectAtIndex:indexPath.row];
and it needed to be:
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[[chatLog objectAtIndex:indexPath.row]objectAtIndex:1]];
Thanks to @Paulw11 and @sjeohp for helping me figure out the exception breakpoint feature in Xcode
Upvotes: 0
Reputation: 5148
Your dequeueReusableCell code is fail. you need check if it nil then init cell. And check about numberOfRowsInSection code to return chatLog.count
Upvotes: -1