views:

39

answers:

1

I'm writing an app for my father's business, it involves lists, however, I have seen that it seems to reorganize the list, even though I don't want it to. For example, all of section 1 is supposed to say one thing, when I scroll down, content from the other sections is put in section 1's cells, this goes for the other sections as well. Here's all the table code, if it helps.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == ENGINEER_ID) {
        return 3;
    } else if (section == FIREMAN_ID) {
        return 3;
    } else if (section == CONDUCTOR_ID) {
        return 3;
    } else if (section == TRAINMASTER_ID) {
        return 3;
    } else {
        return 0;
    }
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if ([indexPath section] == ENGINEER_ID) {
            cell.textLabel.text = @"Engineer";
        } else if ([indexPath section] == FIREMAN_ID) {
            cell.textLabel.text = @"Fireman";
        } else if ([indexPath section] == CONDUCTOR_ID) {
            cell.textLabel.text = @"Conductor";
        } else if ([indexPath section] == TRAINMASTER_ID) {
            cell.textLabel.text = @"Trainmaster";
        }
    }

    // Configure the cell.

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {    
    if (section == ENGINEER_ID) {
        return @"Master Engineer II";
    } else if (section == FIREMAN_ID) {
        return @"Fireman";
    } else if (section == CONDUCTOR_ID) {
        return @"Conductor";
    } else if (section == TRAINMASTER_ID) {
        return @"Trainmaster";
    } else {
        return @"What.";
    }

}
+1  A: 

You need to move the assignment of text out of the conditional.

After the first 3 cells's are alloc/init'd, you won't necessarily be getting more cells.

You are assigning cell.textLabel.text only if you create a new cell.

Logic should be:

static NSString *CellIdentifier = @"Cell";

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

 if ([indexPath section] == ENGINEER_ID) {
    cell.textLabel.text = @"Engineer";
 } else if ([indexPath section] == FIREMAN_ID) {
     cell.textLabel.text = @"Fireman";
 } else if ([indexPath section] == CONDUCTOR_ID) {
     cell.textLabel.text = @"Conductor";
 } else if ([indexPath section] == TRAINMASTER_ID) {
     cell.textLabel.text = @"Trainmaster";
 }

And of course after you tire of writing cascading if statements, you could use switch([indexPath section])

Jeff Schilling