2010-02-27 113 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節])的輪胎之後,當然還有