1 //
2 // ViewController.m
3 // Xib自定义UITableViewCell
4 //
5 // Created by admin on 16/5/16.
6 // Copyright © 2016年 KXZDJ. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
12 @property (nonatomic, strong) UITableView *tableView;
13 @end
14
15 @implementation ViewController
16
17 - (void)viewDidLoad {
18 [super viewDidLoad];
19 // Do any additional setup after loading the view, typically from a nib.
20 [self config];
21 }
22
23 - (void)didReceiveMemoryWarning {
24 [super didReceiveMemoryWarning];
25 // Dispose of any resources that can be recreated.
26 }
27
28
29 -(void)config {
30 //初始化tableView,并给tableView设置frame以及样式
31 self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
32 //遵守代理和数据源(因为要用到代理和数据源方法)
33 self.tableView.delegate = self;
34 self.tableView.dataSource = self;
35 //添加到ViewController的视图中
36 [self.view addSubview:self.tableView];
37 }
38
39 /**
40 * 返回多少个组(默认是1组,如果只有一组可以不实现这个方法)
41 *
42 * @param tableView 当前tableView
43 *
44 * @return 组的个数
45 */
46 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
47 return 1;
48 }
49 /**
50 * 每一组返回多少行
51 *
52 * @param tableView 当前tableView
53 * @param section 当前组
54 *
55 * @return 行的个数
56 */
57 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
58 return 20;
59 }
60
61 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
62 //指定cell的重用标识符
63 static NSString *reuseIdentifier = @"CELL";
64 //去缓存池找名叫reuseIdentifier的cell
65 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
66 //如果缓存池中没有,那么创建一个新的cell
67 if (!cell) {
68 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
69 }
70 //返回当前cell
71 return cell;
72 }