Iphone 添加元素时,UITableView会调整大小

Iphone 添加元素时,UITableView会调整大小,iphone,ios,uitableview,storyboard,Iphone,Ios,Uitableview,Storyboard,我正在为iPhone开发IRC客户端,我的UITableView有问题 首先,我调整了tableview的大小,以便在用户键入消息时,工具栏和键盘可以放在屏幕上。看起来像这样: 但问题是,当用户完成键入并想要发送消息(发送到流并添加到tableview)时,tableview的大小会调整,工具栏会放在底部,但键盘会保留,我不知道要退出第一响应者的文本字段,我想保持键盘和工具栏在按下发送按钮前后的位置。看起来像这样: 这是我的一些代码: -调整窗口大小 // Keyboard will sho

我正在为iPhone开发IRC客户端,我的UITableView有问题

首先,我调整了tableview的大小,以便在用户键入消息时,工具栏和键盘可以放在屏幕上。看起来像这样:

但问题是,当用户完成键入并想要发送消息(发送到流并添加到tableview)时,tableview的大小会调整,工具栏会放在底部,但键盘会保留,我不知道要退出第一响应者的文本字段,我想保持键盘和工具栏在按下发送按钮前后的位置。看起来像这样:

这是我的一些代码:

-调整窗口大小

// Keyboard will show
- (void)keyboardWillShow {
    [UIView animateWithDuration:0.25 animations:^{
        [self.tableView setFrame:CGRectMake(0, 0, 320, 177)];
    }];

    [UIView animateWithDuration:0.25 animations:^{
        [self.toolBar setFrame:CGRectMake(0, 177, 320, 43)];
    }];    
}

// Keyboard will hide
- (void)keyboardWillHide {
    [UIView animateWithDuration:0.25 animations:^{
        [self.tableView setFrame:CGRectMake(0, 0, 320, 481)];
    }];

    [UIView animateWithDuration:0.25 animations:^{
        [self.toolBar setFrame:CGRectMake(0, 393, 320, 43)];
    }];    
}
-向tableview添加元素

// Retrieve the sender and the message from the input and add it to the data context
- (void)addMessage:(NSString *) input isSender: (BOOL) sender {
    Message *messageToBeAdded = [[Message alloc] init];
    [messageToBeAdded setIsSender:sender];

    NSArray *arr = [input componentsSeparatedByString:[NSString stringWithFormat:@"PRIVMSG %@ :",[connection channel]]];
    [messageToBeAdded setMessage:[arr objectAtIndex:1]];

    arr = [input componentsSeparatedByString:@"!"];

    if (sender) {
        [messageToBeAdded setSender:[connection nick]];
    } else {
        NSString *tmp = [arr objectAtIndex:0];
        [messageToBeAdded setSender:[tmp substringWithRange:NSMakeRange(1, [tmp length]-1)]];
    }

    [_messageList addObject:messageToBeAdded];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_messageList count]-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
我到处都找过了,我确信这与tableview的添加部分有关。我将感谢任何帮助或指导

谢谢大家!

编辑: 我刚刚意识到这与使用自定义单元格有关。我创建了一个新项目,并尝试一步一步地模拟我所做的事情。在我使用带有UILabel的自定义单元格时,一切都很顺利。UILabel通过插座连接到UITableViewCell类,这就是问题发生的地方

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *myLabel;
@end
#导入
@接口CustomCell:UITableViewCell
@属性(强,非原子)IBUILabel*myLabel;
@结束

你认为这里有什么问题吗?

我猜你的工具栏上有一个
UITextField
。试试看

[aTextField resignFirstResponder];

。。。当您点击发送/返回按钮时。

我猜您的工具栏中有一个
UITextField
。试试看

[aTextField resignFirstResponder];

。。。当您点击发送/返回按钮时。

文本字段控制键盘,因此resignfirstresponder将向键盘发送一条消息以删除自身。波段是否移动到屏幕底部或在按下发送按钮后被移除。无论哪种方式,按照Michael说的去做,一旦键盘消失,你就会知道横幅会发生什么。我希望这有帮助

文本字段控制键盘,因此resignfirstresponder会向键盘发送一条消息以删除自身。波段是否移动到屏幕底部或在按下发送按钮后被移除。无论哪种方式,按照Michael说的去做,一旦键盘消失,你就会知道横幅会发生什么。我希望这有帮助

您可以使用UITextFieldDelegate-

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
     [textField resignFirstResponder];
}
或者,如果您有textField的IBOutlet,则在“键盘将隐藏”中,呼叫退出FirstResponder

// Keyboard will hide
- (void)keyboardWillHide {

      [self.myTextField resignFirstResponder];

      [UIView animateWithDuration:0.25 animations:^{
          [self.tableView setFrame:CGRectMake(0, 0, 320, 481)];
      }];

      [UIView animateWithDuration:0.25 animations:^{
          [self.toolBar setFrame:CGRectMake(0, 393, 320, 43)];
      }];    
}

您可以使用UITextFieldDelegate-

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
     [textField resignFirstResponder];
}
或者,如果您有textField的IBOutlet,则在“键盘将隐藏”中,呼叫退出FirstResponder

// Keyboard will hide
- (void)keyboardWillHide {

      [self.myTextField resignFirstResponder];

      [UIView animateWithDuration:0.25 animations:^{
          [self.tableView setFrame:CGRectMake(0, 0, 320, 481)];
      }];

      [UIView animateWithDuration:0.25 animations:^{
          [self.toolBar setFrame:CGRectMake(0, 393, 320, 43)];
      }];    
}

问题是我不希望它在发送信息时辞职。用户必须点击外部才能退出(这很好)。可能我误解了你的问题,我以为你希望在他们发送消息后键盘消失。问题是我不希望它在发送消息时退出。用户必须点击外部才能退出(这很好)。也许我误解了你的问题,我以为你希望在他们发送消息后键盘消失。我这样做了,工具栏被移动到与tableview相同的原始位置。这就像文本字段被删除了,但键盘仍然在那里……我这样做了,工具栏被移动到了与tableview相同的原始位置。这就像文本字段已辞职,但键盘仍在那里…我认为问题被误解了,我不希望在按下发送按钮时移除键盘。感谢到目前为止的回复!我想这个问题理解错了,我不想在按下发送按钮时移除键盘。感谢到目前为止的回复!