如何用新数据刷新我的UIViewController?

时间:2013-12-11 06:28:17

标签: ios objective-c uitableview nsdictionary uialertview

这是一个ChatViewController.m接收消息,然后显示在tableView中。 现在,当消息发送者发生变化时,我想让用户选择切换到新消息(alert view)。如果用户点击开关,如何显示消息?

这是接收和显示消息的方式。

 -(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?
}
}

3 个答案:

答案 0 :(得分:1)

您可以实现alertview委托metnod,如下所示

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) //OK Button
    {
       //ok button code
    }
    else //Switch Button
    {
        //switch button code
        [tableview reloadData];
    }
}

答案 1 :(得分:0)

您可以实施alertView委托

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if(buttonIndex != alertView.cancelButtonIndex) {
         // Switch action, Write your logic to reload you data
    }
  }

答案 2 :(得分:0)

同样的事情,您需要遵循else条件以及您在上述if条件中所做的事情。再次表示您需要refresh array,然后您需要reload tableview填写以下新数据: -

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) //OK Button
    {
       //ok button code
    }
    else //Switch Button
    {
        // when switch button is clicked....write below for reloading the data
   [bubbleData addObject:heyBubble];// here heyBubble will be your new data
     [bbltblView reloadData];
    }
}