這是接收消息的ChatViewController.m
,然後顯示在tableView
中。 現在當消息的發送者改變時,我想給用戶選擇切換到新消息(alert view
)。如果用戶點擊開關,如何顯示消息?如何用新數據刷新我的UIViewController?
這是如何接收和顯示消息。
-(void)recvdMsg:(NSDictionary *)msg
{
NSString *currentUser = [msg objectForKey:@"sender"];
NSString *match = @"@";
NSString *preAt ;
NSScanner *scanner = [NSScanner scannerWithString:currentUser];
[scanner scanUpToString:match intoString:&preAt];
if (self.name == preAt) //same sender
{
NSMutableDictionary *newMsg=[[NSMutableDictionary alloc]init];
NSString *m = [msg objectForKey:@"msg"];
[newMsg setObject:m forKey:@"message"];
[newMsg setObject:converID forKey:@"conversationID"];
[newMsg setObject:@"1" forKey:@"FromTo"];
NSDate *today=[NSDate date];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
NSString *dt=[dateFormat stringFromDate:today];
[newMsg setObject:dt forKey:@"timeStamp"];
[self AddMessage:newMsg]; // adding to database.
NSBubbleData *heyBubble = [NSBubbleData dataWithText:m date:[NSDate date] type:BubbleTypeSomeoneElse];
[bubbleData addObject:heyBubble];
[bbltblView reloadData];
[bbltblView scrollBubbleViewToBottomAnimated:YES];
}
else //message from new sender
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"New Message from " message:preAt delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Switch", nil];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) // cancel button clicked
{
return;
}
else {
// switch button is clicked....How to go from here.....how would i reload data?
}
}
使用'[實現代碼如下reloadData];'。更新您的數據源後 –
如果您想要顯示來自新用戶的消息,然後在警報切換代碼中刪除bubbleData中的所有對象並僅添加新用戶的消息(如果希望來自bubbleData中的所有用戶的數據,但只是想要顯示消息,然後在關鍵「發件人」的bubbleData上應用謂詞,並使用過濾的數組在tableview中顯示數據,不要忘記爲tableview調用reloadData。 –
請幫助一些示例代碼。 – icodes