【问题标题】:UITableView resizes when adding elementUITableView 在添加元素时调整大小
【发布时间】:2013-04-15 22:15:24
【问题描述】:

我正在为 iPhone 开发 IRC 客户端,但我的 UITableView 出现问题。

首先,我调整了表格视图的大小,以便在用户输入消息时工具栏和键盘适合屏幕。看起来像这样:

但问题是,当用户完成输入并想要发送消息(发送到流并添加到 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

有没有想过这里可能出了什么问题?

【问题讨论】:

  • 我认为这个问题被误解了,我不想在按下发送按钮时移除键盘。感谢您到目前为止的回复!

标签: iphone ios uitableview storyboard


【解决方案1】:

我猜你的工具栏中有一个UITextField。试试……

[aTextField resignFirstResponder];

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

【讨论】:

  • 问题是我不希望它在发送消息时辞职。用户必须点击外部才能退出(效果很好)。
  • 也许我误解了你的问题,我以为你希望在他们发送消息后键盘消失。
【解决方案2】:

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

【讨论】:

  • 我这样做了,工具栏被移动到它的原始位置,就像 tableview 一样。这就像文本字段已辞职,但键盘仍然存在......
【解决方案3】:

你可以使用 UITextFieldDelegate -

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
     [textField resignFirstResponder];
}

或者,如果您有 textField 的 IBOutlet,则在“键盘将隐藏”中调用 resignFirstResponder

// 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)];
      }];    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-10-28
    相关资源
    最近更新 更多