//
// ViewController.m
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource> // 4.遵守代理
@end
@implementation ViewController
- (void)viewDidLoad {
UITableView * tabview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 5.赋值代理
tabview.delegate = self;
tabview.dataSource = self;
[self.view addSubview:tabview];
}
// 6.实现协议中的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"reuseId";
UITableViewCell * myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (myCell == nil) {
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
myCell.textLabel.text = @"test";
return myCell;
}
@end
tableview中的delegate、DataSource原理
于 2017-11-14 10:35:26 首次发布