我有一个包含每个单元格中文本视图的表格视图。如何滚动tableview以便输入到单元格的textview中的文本始终可见?这与带有文本字段的tableview不同,因为文本字段不会改变它的高度,而textview则会改变它的高度。
谢谢!
答案 0 :(得分:0)
每次按下返回时,UITextView
都会更改其高度。按下返回时检测并稍微向上滚动UITableView
。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
CGPoint currentOffset=myTableView.contentOffset;
currentOffset.y-=10;
[myTableview setContentOffset:currentOffset];
}
return YES;
}
答案 1 :(得分:0)
声明新变量 UITextView * activeTextView;
在ViewDidLoad方法中,在显示/隐藏时注册键盘通知。
[self keyboardNotifications];
添加以下方法。 - (void)keyboardNotifications { //键盘显示时注册通知 [[NSNotificationCenter defaultCenter] addObserver:self 选择:@选择(keyboardWillShow :) 名称:UIKeyboardWillShowNotification 对象:无];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
使用cellForRowAtIndexPath中的indexpath设置UITextview标记。
#pragma mark - Keyboard handling
-(void) keyboardWillShow:(NSNotification *)note {
if(activeTextView)
if ([TableCell count] < activeTextView.tag) { // It is validate table cell count and activeTextView.tag
NSDictionary* info = [note userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
tableV.contentInset = contentInsets;
tableV.scrollIndicatorInsets = contentInsets;
[tableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:activeTextView.tag inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
}
-(void) keyboardWillHide:(NSNotification *)note {
[UIView animateWithDuration:.3 animations:^(void) {
tableV.contentInset = UIEdgeInsetsZero;
tableV.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}
UITextView委托设置为当前UITextView的activeTextView。
#pragma mark - UITextViewDelegate ::
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
return YES;
}
// To be link with your TextView event "Editing Did Begin"
// memoryze the current TextView
- (void)textViewDidBeginEditing:(UITextView *)textView
{
activeTextView = textView;
[textView becomeFirstResponder];
}
// To be link with your TextView event "Editing Did End"
// release current TextView
- (void)textViewDidEndEditing:(UITextView *)textView
{
activeTextView = nil;
[textView resignFirstResponder];
}