1。初始化
initWithFrame:style:
e.g. [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; //可充满整个视图
2。<UITableViewDelegate> //事件
myTableView.delegate = self ;
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
tableView:viewForHeaderInSection:
tableView:didEndDisplayingCell:forRowAtIndexPath: //当一个单元格滚动出屏幕时,被调用
tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *) tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *) indexPath //单击detail disclosure button时调用
e.g. //参数中的tableView可用于进行确定在view中是哪一个table view接收消息
- (CGFloat) tableView : (UITableView *) tableView heightForRowAtIndexPath : (NSIndexPath *) indexPath {
if ( [ tableView isEqual : self.myTableView ] ) {
return 100.0f ;
}
return 40.0f ;
}
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *ownerCell = [ tableView cellForRowAtIndexPath : indexPath] ; //获取indexPath所在的cell
NSLog(@"Cell Title = %@", ownerCell.textLabel.text);
}
3。<UITableViewDataSource> //数据源
numberOfSectionsInTableView : //每个表有多少个section
tableView : numberOfRowsInSection : //每个section有多少行
tableView : cellForRowAtIndexPath : //每行的单元格内容cell
e.g.
-(void) viewDidLoad {
myTableView.dataSource = self ;
static NSString *TableViewCellIdentifier = @"MyCellId" ; //设置cell标识
[ self.myTableView registerClass : [ UITableViewCell class ] forCellReuseIdentifier : TableViewCellIdentifier ] ;
//注册cell以便后续进行重用,这里的cell可以是UITableViewCell,也可以是自定义的cell类
myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ; //自动伸缩
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
if ( [ tableView isEqual:self.myTableView ] ) {
cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier forIndexPath:indexPath] ; //通过之前定义的cellid获取重用
cell.textLabel.text = [NSString stringWithFormat:。。。。] ;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
// two useful : disclosure indicator (produces no event,)
detail disclosure button(fires an event,)
}
return cell;
}