直接上代买吧:
@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];
}