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];
}
以下に示すように、カスタムセルをテーブルビューに追加します。
- (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 をクリックすると、キーボード通知が受信されるため、テーブルビューがスクロールアップしてキーボードに対応します。しかし、「textviewDidEndEditing」でキーボードを辞任すると、テーブルビューのセルが交換されます。
これが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 の FirstResponder を辞任したときに、セルが交換されている理由がわかりません。前もって感謝します。