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

请更改您的单元分配代码并替换

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

使用此代码:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
于 2013-05-15T13:53:06.233 回答
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 回答
0

allocinit您的tf并将其添加到单元格的子视图中。我认为您的 tf 为零,这就是为什么它没有添加到单元格的子视图中。

希望这可以帮助。

于 2013-05-15T14:03:37.080 回答