Jason
Jason

Reputation: 650

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;
}

Upvotes: 2

Views: 203

Answers (4)

Ashvin
Ashvin

Reputation: 8997

Please change your cell allocation code and replace

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

With this code:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

Upvotes: 0

geo
geo

Reputation: 1791

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

Upvotes: 3

iEinstein
iEinstein

Reputation: 2100

Please alloc and init your tf and add it to subview of cell. I think your tf is nil that's why it is not adding to subview of cell.

Hope this helps.

Upvotes: 0

Harshal Chaudhari
Harshal Chaudhari

Reputation: 710

You should add textField on content view off the cell like this

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

}

Upvotes: 0

Related Questions