wsmrcool 2013-01-17
Dynamic TableView的使用方法
1 设置TableView的delegate和dataSource(TableView has two important @propertys: its delegate and its dataSource. )
2 声明实现这两个Delegate <UITableViewDelegate, UITableViewDataSource>
3 UITableView dataSource protocol必须实现3个方法
1) section的数目
2)section中row的数目
3)cell的具体内容
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return section_rows;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cell_rows;
}
// 自定义TableCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Custom Table Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
//自定义header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SectionHeader *header = [[SectionHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, HEADER_HEIGHT)];
return header;
}
//header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return HEADER_HEIGHT;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [CustomCell cellHeight];
}