0

ユーザーがセルの TextLabel を変更できるようにするために、TextField を UICell に追加しました。Return キーを押すと、キーボードが表示され、TextLabel が正しい値に変更されるという点で、すべて正常に機能します。ただし、編集中のテキストを見ることはできません。これが私のコードです:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {


     [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
     UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
     NSString *aCategory = aCell.textLabel.text;
     [[aCell detailTextLabel] setText:aCategory];


     [aCell setEditing:YES animated:YES];
     UITextField *userText =[[UITextField alloc] init];
     userText.tag = indexPath.row;
     [aCell addSubview:userText];
     NSArray *test =[aCell subviews];
     NSLog(@"I have %d many subviews",[test count]);
     userText.alpha=1.0;
     [userText setDelegate:self];
     userText.autocorrectionType = UITextAutocorrectionTypeNo;
     [aCell bringSubviewToFront:userText];
     [userText becomeFirstResponder];


 }

できればカスタムセルなしでやりたいです。前もって感謝します。

4

1 に答える 1

0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSString *aCategory = aCell.textLabel.text;
    [[aCell detailTextLabel] setText:aCategory];

    UITextField *userText = [[UITextField alloc] initWithFrame:aCell.textLabel.frame];
    userText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    userText.font = aCell.textLabel.font;
    userText.text = aCell.textLabel.text;
    aCell.textLabel.text = nil;
    userText.tag = indexPath.row;
    [aCell addSubview:userText];

    [userText setDelegate:self];
    userText.autocorrectionType = UITextAutocorrectionTypeNo;
    [aCell bringSubviewToFront:userText];
    [userText becomeFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    UITableViewCell *aCell = [self.tableView cellForRowAtIndexPath:indexPath];
    aCell.textLabel.text = textField.text;
    [textField removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
于 2012-06-20T22:54:08.597 に答える