2013-04-22 34 views
0

我正在爲iPhone的IRC客戶端工作,而我的UITableView出現問題。UITableView在添加元素時調整大小

首先我調整了桌面視圖的大小,以便在用戶輸入消息時,工具欄和鍵盤將適合屏幕。這樣看:

User typing mode

但問題是,當用戶完成輸入,並希望發送信息(發送到流並添加到tableview中)的實現代碼如下調整大小和工具欄在放底部,但鍵盤仍然存在,我不是哪個文本框辭職的第一響應者,我想保持鍵盤和工具欄在按下發送按鈕之前和之後。這樣看:

Fail mode

這是我的一些代碼:

-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 

任何想到這裏可能是錯誤的?

+0

我認爲這個問題是missunderstood,我不希望按下發送按鈕時可以移除鍵盤。感謝迄今爲止的迴應! – RobertH 2013-04-24 20:37:03

回答

1

我猜你在工具欄中有UITextField。嘗試...

[aTextField resignFirstResponder]; 

...當你處理點擊發送/返回按鈕。

+0

事情是我不希望它在發送消息時辭職。用戶必須點擊外部才能辭職(工作正常)。 – RobertH 2013-04-23 10:52:44

+0

也許我誤解了你的問題,我以爲你想讓鍵盤在發送信息後消失。 – 2013-04-23 18:49:01

1

文本字段控制鍵盤,因此resignfirstresponder會向鍵盤發送消息以將其自身刪除。樂隊是否移動到屏幕底部或在按下發送按鈕後將其移除。無論哪種方式做邁克爾說要做的事情,一旦鍵盤消失,你會知道橫幅會發生什麼。我希望這有幫助!

+0

我這樣做了,工具欄被移動到它的原始位置,就像tableview一樣。這就像文本字段已經退出,但鍵盤仍然存在... – RobertH 2013-04-23 18:39:33

1

您可以使用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)]; 
     }];  
} 
相關問題