2010-02-27 107 views
0

我正在为父亲的业务编写一个应用程序,它涉及列表,但是,我已经看到它似乎重新组织了列表,即使我不想要它。例如,第1部分的所有内容都应该说一件事,当我向下滚动时,其他部分的内容将放入第1部分的单元格中,这也适用于其他部分。 这里有所有的表格代码,如果有帮助。细胞在iPhone上重组?

- (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

您需要将文本的分配移出条件。

在前3个单元的alloc/init'd后,你不一定会获得更多的单元格。

只有在创建新的单元格时,才会分配cell.textLabel.text。

逻辑应该是:

静态的NSString * CellIdentifier = @ “小区”;

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

你写级联if语句,你可以使用开关([indexPath节])的轮胎之后,当然还有