UITableView
在UIViewController之后添加<UITableViewDelegate, UITableViewDataSource>,UITableViewDelegate和 UITableViewDataSource在Objective-C中称之为协议,要实现协议中必要的方法(因为有可选的方法)。为什么要使用这两个协议呢?因为我们要将数据填充到UITableView中,这样子,那UITableViewDelegate和 UITableViewDataSource应该与数据填充有关了,其实看它的命名也可以看得出了。
UITableViewDataSource是用来连接数据和表视图的,要实现两个方法,一个是tableView:cellForRowAtIndexPath,另一个是tableView:numberOfRowsInSection,实现这两个方法,你就告诉了表视图显示多少行数据和每一行中的数据。
UITableViewDelegate是负责处理UITableView的表现,该协议中的可选方法让你管理表行的高度,配置节点头部和底部,对表单元重新排序等。
实现UITableViewDataSource协议中的两个方法:
|
#pragma mark - #pragma mark dateSource必须实现的协议 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_tableArray count];//相当于行数 } #pragma mark - #pragma mark UITableViewCell创建过程 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"indexPath.section == %d",indexPath.section); NSLog(@"indexPath.row == %d",indexPath.row); static NSString *celldentify = @"celldentify"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celldentify]; if (!cell) { cell = [[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:celldentify]autorelease]; } TableModel *model = _tableArray[indexPath.row]; cell.showImageView.image = [UIImage imageNamed:model.imageName]; [cell.nameLabel setText:model.name]; [cell.numberLabel setText:model.number];
return cell; } |