UITableView在添加元素时调整大小
我正在为iPhone的IRC客户端工作,而我的UITableView出现问题。UITableView在添加元素时调整大小
首先我调整了桌面视图的大小,以便在用户输入消息时,工具栏和键盘将适合屏幕。这样看:
但问题是,当用户完成输入,并希望发送信息(发送到流并添加到tableview中)的实现代码如下调整大小和工具栏在放底部,但键盘仍然存在,我不是哪个文本框辞职的第一响应者,我想保持键盘和工具栏在按下发送按钮之前和之后。这样看:
这是我的一些代码:
-Resizing窗户
// 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)];
}];
}
-Adding元素的实现代码如下
// 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];
}
I'我已经看到无处不在,并且我确信它与添加部分o有关桌面视图。我将不胜感激任何帮助或指导。
谢谢!
编辑: 我刚刚意识到,这与使用自定义单元格有关。我创建了一个新项目并试图模拟我所做的一步一步。而且一切工作都很好,我用UILabel来定制单元格。 UILabel通过插座连接到UITableViewCell类,这是发生问题的地方。
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
@end
任何想到这里可能是错误的?
我猜你在工具栏中有UITextField
。尝试...
[aTextField resignFirstResponder];
...当你处理点击发送/返回按钮。
事情是我不希望它在发送消息时辞职。用户必须点击外部才能辞职(工作正常)。 – RobertH 2013-04-23 10:52:44
也许我误解了你的问题,我以为你想让键盘在发送信息后消失。 – 2013-04-23 18:49:01
文本字段控制键盘,因此resignfirstresponder会向键盘发送消息以将其自身删除。乐队是否移动到屏幕底部或在按下发送按钮后将其移除。无论哪种方式做迈克尔说要做的事情,一旦键盘消失,你会知道横幅会发生什么。我希望这有帮助!
我这样做了,工具栏被移动到它的原始位置,就像tableview一样。这就像文本字段已经退出,但键盘仍然存在... – RobertH 2013-04-23 18:39:33
您可以使用UITextFieldDelegate -
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
或者,如果你有文本框的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)];
}];
}
我认为这个问题是missunderstood,我不希望按下发送按钮时可以移除键盘。感谢迄今为止的回应! – RobertH 2013-04-24 20:37:03