Search code examples
iosuitableviewcell

Uitableview Cells not all working?


Here is my code, section 0 shows the title, but not the textfield or placeholder, whats the deal? Section 1 is fine!

- (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:@"[email protected]"];
            [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;
}

Solution

  • You set your frame property only for section 1, but not for section 0. just set the

        // 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];
    

    part outside the the else if(indexPath.section == 1) braces (and declare the tf variable also before the first if) or copy/paste it after the first switch :P