您的位置:首页 > 其它

tableview中的delegate、DataSource原理

2017-11-14 10:35 579 查看




//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: