2

これが私のコードです。セクション 0 にはタイトルが表示されますが、テキストフィールドやプレースホルダーは表示されません。セクション1は大丈夫です!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:12];

if (indexPath.section == 0) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Name" ;
            tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
            [cell addSubview:nameFieldTextField];
            break ;
        }
        case 1: {
            cell.textLabel.text = @"Address" ;
            tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"];
            [cell addSubview:addressFieldTextField];
            break ;
        }
        case 2: {
            cell.textLabel.text = @"Email" ;
            tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"example@gmail.com"];
            [cell addSubview:emailFieldTextField];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Phone" ;
            tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"XXX-XXX-XXXX"];
            [cell addSubview:phoneFieldTextField];
            break ;
        }

    }

} else if (indexPath.section == 1) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Company" ;
            tf = workNameTextField = [self makeTextField:self.workName placeholder:@"Company Name"];
            [cell addSubview:workNameTextField];
            break ;
        }
        case 1: {
            cell.textLabel.text = @"Address" ;
            tf = workAddressTextField = [self makeTextField:self.workAddress placeholder:@"Work Address"];
            [cell addSubview:workAddressTextField];
            break ;
        }
        case 2: {
            cell.textLabel.text = @"Phone" ;
            tf = workPhoneTextField = [self makeTextField:self.workPhone placeholder:@"xxx-xxx-xxxx"];
            [cell addSubview:workPhoneTextField];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Title" ;
            tf = workTitleTextField = [self makeTextField:self.workTitle placeholder:@"Position"];
            [cell addSubview:workTitleTextField];
            break ;
        }
        case 4: {
            cell.textLabel.text = @"Manager" ;
            tf = workManagerTextField = [self makeTextField:self.workManager placeholder:@"Mr. Boss"];
            [cell addSubview:workManagerTextField];
            break ;
        }
        case 5: {
            cell.textLabel.text = @"Manager Phone" ;
            tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"XXX-XXX-XXXX"];
            [cell addSubview:workManagerPhoneTextField];
            break ;
        }
        case 6: {
            cell.textLabel.text = @"Annual Salary" ;
            tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"$50,000"];
            [cell addSubview:workManagerPhoneTextField];
            break ;
        }

    }
    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

}

return cell;
}
4

4 に答える 4

3

セクション 1 に対してのみフレーム プロパティを設定し、セクション 0 に対しては設定しません。

    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

中括弧の外側の部分else if(indexPath.section == 1)(および最初のifの前にもtf変数を宣言する)または最初のスイッチの後にコピー/貼り付け:P

于 2013-05-15T14:35:04.547 に答える
0

セルのサブビューに追加してallocください。あなたのtfはnilだと思うので、セルのサブビューに追加されていません。inittf

お役に立てれば。

于 2013-05-15T14:03:37.080 に答える
0

このように、セルの外側のコンテンツ ビューに textField を追加する必要があります。

if (indexPath.section == 0) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Name" ;
            tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
            [cell.contentview addSubview:nameFieldTextField];
            break ;
        }
        case 1: {
            cell.textLabel.text = @"Address" ;
            tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"];
            [cell.contentview addSubview:addressFieldTextField];
            break ;
        }
        case 2: {
            cell.textLabel.text = @"Email" ;
            tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"example@gmail.com"];
            [cell.contentview addSubview:emailFieldTextField];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Phone" ;
            tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"XXX-XXX-XXXX"];
            [cell.contentview addSubview:phoneFieldTextField];
            break ;
        }

    }

} else if (indexPath.section == 1) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Company" ;
            tf = workNameTextField = [self makeTextField:self.workName placeholder:@"Company Name"];
            [cell.contentview addSubview:workNameTextField];
            break ;
        }
        case 1: {
            cell.textLabel.text = @"Address" ;
            tf = workAddressTextField = [self makeTextField:self.workAddress placeholder:@"Work Address"];
            [cell.contentview addSubview:workAddressTextField];
            break ;
        }
        case 2: {
            cell.textLabel.text = @"Phone" ;
            tf = workPhoneTextField = [self makeTextField:self.workPhone placeholder:@"xxx-xxx-xxxx"];
            [cell.contentview addSubview:workPhoneTextField];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Title" ;
            tf = workTitleTextField = [self makeTextField:self.workTitle placeholder:@"Position"];
            [cell.contentview addSubview:workTitleTextField];
            break ;
        }
        case 4: {
            cell.textLabel.text = @"Manager" ;
            tf = workManagerTextField = [self makeTextField:self.workManager placeholder:@"Mr. Boss"];
            [cell.contentview addSubview:workManagerTextField];
            break ;
        }
        case 5: {
            cell.textLabel.text = @"Manager Phone" ;
            tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"XXX-XXX-XXXX"];
            [cell.contentview addSubview:workManagerPhoneTextField];
            break ;
        }
        case 6: {
            cell.textLabel.text = @"Annual Salary" ;
            tf = workManagerPhoneTextField = [self makeTextField:self.workManagerphone placeholder:@"$50,000"];
            [cell.contentview addSubview:workManagerPhoneTextField];
            break ;
        }

    }
    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

}
于 2013-05-15T13:45:13.160 に答える