我有4个部分的自定义tableview。
第1部分由“TextField”组成。
第二名和第三名包括标签。
第4部分由“TextArea”组成。
我已注册“UIKeyboardWillShowNotification”。我正在调用“keyboardWillShow”函数,如下所示。
- (void)keyboardWillShow:(NSNotification *)notification
{
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardBounds;
[keyboardBoundsValue getValue:&keyboardBounds];
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height + 80, 0);
[eventTableView setScrollIndicatorInsets:e];
[eventTableView setContentInset:e];
}
将自定义单元格添加到tableview,如下所示。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if([indexPath section] == 0 || [indexPath section] == 4)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if([indexPath section] == 0)
{
UITextField* titleTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 280, cell.bounds.size.height)];
[titleTextField setTag:201];
[titleTextField setPlaceholder:@"Title"];
[titleTextField setEnablesReturnKeyAutomatically:YES];
[titleTextField setDelegate:self];
[titleTextField setClipsToBounds:YES];
[[cell contentView]addSubview:titleTextField];
[titleTextField release];
}
else if([indexPath section] == 4)
{
UITextView* messageTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 5, 280, 2 * cell.bounds.size.height)];
[messageTextView setTag:206];
[messageTextView setEnablesReturnKeyAutomatically:YES];
[messageTextView setBackgroundColor:[UIColor clearColor]];
[messageTextView setDelegate:self];
[messageTextView setClipsToBounds:YES];
[[cell contentView]addSubview:messageTextView];
[messageTextView release];
}
}
}
else
{
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
if([indexPath section] == 1)
{
if([indexPath row] == 0)
{
cell.textLabel.text = @"Start";
[cell setTag:202];
}
else if([indexPath row] == 1)
{
cell.textLabel.text = @"End";
[cell setTag:203];
}
}
else if([indexPath section] == 2)
{
cell.textLabel.text = @"Reminder";
[cell setTag:204];
}
else if([indexPath section] == 3)
{
cell.textLabel.text = @"Add Photo";
[cell setTag:205];
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
当我点击textView时,收到键盘通知,因此tableview向上滚动以适应键盘。但是当我在“textviewDidEndEditing”上退出键盘时,tableview的单元格会交换。
这是textviewDidEndEditing的代码。
- (void)textViewDidEndEditing:(UITextView *)textView
{
[UIView beginAnimations:NULL context:nil];
[UIView setAnimationDuration:0.5];
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, 0, 0);
[eventTableView setScrollIndicatorInsets:e];
[eventTableView setContentInset:e];
[UIView commitAnimations];
}
请帮我解决这个问题。我不知道为什么键盘出现时以及当我在textfield和textView中使用resignFirstResponder时要更换单元格。提前谢谢。
答案 0 :(得分:1)
试试这个
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *)[textField superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}
答案 1 :(得分:0)
我自己找到了答案。当我使用相同的“CellIdentifier”分配TextField和TextArea时,它会产生问题。
所以,我在xib中创建了2个Custom UITableViewCell。一个用于Custom TextField单元,一个用于Custom TextView单元,并分别给出Cell标识符,即“TitleCell”和“MessageCell”。现在我去了cellForRowAtIndexPath函数并将[TitleCell]作为[indexPath section] == 0和“MessageCell”的标识符作为[indexPath section] == 4的标识符。
就是这样。我的问题解决了。谢谢:)