0

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 を辞任したときに、セルが交換されている理由がわかりません。前もって感謝します。

4

2 に答える 2

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;
}
于 2017-06-12T08:54:27.913 に答える
0

私は自分で答えを見つけました。同じ「CellIdentifier」を使用してTextFieldとTextAreaを割り当てているので、問題が発生します。

そこで、xibで2つのカスタムUITableViewCellを作成しました。1つはカスタムTextFieldセル用、もう1つはカスタムTextViewセル用で、セル識別子、つまりそれぞれ「TitleCell」と「MessageCell」を指定しました。ここで、cellForRowAtIndexPath関数に移動し、[indexPathセクション] ==0の識別子として「TitleCell」を指定し、[indexPathセクション]==4の識別子として「MessageCell」を指定しました。

それでおしまい。私の問題は解決しました。ありがとうございました :)

于 2012-06-15T06:25:33.130 に答える