我有 4 个部分的自定义表格视图。
第一部分由“TextField”组成。
第 2 和第 3 由标签组成。
第 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];
}
这个你能帮我吗。我不知道为什么当键盘出现以及我 resignFirstResponder 的 textfield 和 textView 时,单元格会被交换。提前致谢。