直接上代買吧:
@implementation ViewController - (void)viewDidLoad { a = [NSArray arrayWithObjects:@"ant",@"alpaca",@"albatross", nil]; b = [NSArray arrayWithObjects:@"badger",@"bat",@"bear", nil]; c = [NSArray arrayWithObjects:@"cat",@"calf",@"cattle", nil]; d = [NSArray arrayWithObjects:@"A",@"B",@"C", nil]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } //設置表視圖的行數 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if (indexPath.section == 0) { cell.textLabel.text = [a objectAtIndex:indexPath.row]; } if (indexPath.section == 1) { cell.textLabel.text = [b objectAtIndex:indexPath.row]; } if (indexPath.section == 2) { cell.textLabel.text = [c objectAtIndex:indexPath.row]; } return cell; } //設置表視圖的節數 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [d count]; } //獲取節的名稱 - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [d objectAtIndex:section]; }