-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中进行,或者是封装在自己定义的类中,不过就算封装了,也要在这个方法中调用.
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 #import "XibTableViewCell.h"
12
13 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
14 @property (nonatomic, strong) UITableView *tableView;
15 @end
16
17 @implementation ViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 // Do any additional setup after loading the view, typically from a nib.
22 [self config];
23 }
24
25 - (void)didReceiveMemoryWarning {
26 [super didReceiveMemoryWarning];
27 // Dispose of any resources that can be recreated.
28 }
29
30
31 -(void)config {
32 //初始化tableView,并给tableView设置frame以及样式
33 self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
34 //遵守代理和数据源(因为要用到代理和数据源方法)
35 self.tableView.delegate = self;
36 self.tableView.dataSource = self;
37 //添加到ViewController的视图中
38 [self.view addSubview:self.tableView];
39 }
40
41 /**
42 * 返回多少个组(默认是1组,如果只有一组可以不实现这个方法)
43 *
44 * @param tableView 当前tableView
45 *
46 * @return 组的个数
47 */
48 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
49 return 1;
50 }
51 /**
52 * 每一组返回多少行
53 *
54 * @param tableView 当前tableView
55 * @param section 当前组
56 *
57 * @return 行的个数
58 */
59 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
60 return 20;
61 }
62
63 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
64 //指定cell的重用标识符
65 static NSString *reuseIdentifier = @"CELL";
66 //去缓存池找名叫reuseIdentifier的cell
67 //这里换成自己定义的cell
68 XibTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
69 //如果缓存池中没有,那么创建一个新的cell
70 if (!cell) {
71 //这里换成自己定义的cell,并调用类方法加载xib文件
72 cell = [XibTableViewCell xibTableViewCell];
73 }
74 //给cell赋值
75 cell.backView.image = [UIImage imageNamed:@"223733vuf3mhajhd04hdh5"];
76 cell.infoLabel.text = @"金三胖真帅";
77 cell.infoLabel.textColor = [UIColor redColor];
78 cell.zanView.image = [UIImage imageNamed:@"103112778vn00czp59p6w7"];
79 cell.zanLabel.text = @"100";
80 cell.zanLabel.textColor = [UIColor redColor];
81 //返回当前cell
82 return cell;
83 }
84 /**
85 * 返回cell的行高
86 *
87 * @param tableView 当前tableView
88 * @param indexPath
89 *
90 * @return cell的行高