.h檔
帶入二個delegate
<UITableViewDelegate, UITableViewDataSource>
//設Table group
myTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) style:UITableViewStyleGrouped];
[myTableView setDelegate:self];
[myTableView setDataSource:self];
[self.viewaddSubview:myTableView];
//TableView要使用的方法
#pragma mark - Table view data source
//section數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return 2; }
// Return the number of rows in the section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 3;
}else
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
// row命名:
cell.textLabel.text=[NSString stringWithFormat:@"No:%id",indexPath.row];
return cell;
}
//群組命名:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section==0) {
return @"群組A";
}else
return @"群組B";
}