ios学习笔记 UITableView(纯代码) (一)

 

 

参考 “https://www.cnblogs.com/ai-developers/p/4557487.html”

 

UITableViewCell 有一个代码重用 减少资源的浪费 

 

参考  https://www.cnblogs.com/ai-developers/p/4562311.html 

 

这里没有写

 

 

 

#import "ViewController.h"

 

@interface ViewController ()

 

//引用UITableView

@property (nonatomic,strong)UITableView * tableView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self MyTableView];

}

 

//代码简洁化

-(void) MyTableView

{

    //创建一个不分组的TableView  分组的下一节

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    //TableView 的背景颜色

    self.view.backgroundColor = [UIColor whiteColor];

    

    //绑定数据源 需要实现两个方法 也可以设置section的数量

    //-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    //  -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    //-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath

    self.tableView.dataSource = self;

    

    //添加到TableView的主屏幕

    [self.view addSubview:self.tableView];

}

 

//实现rows

-(NSInteger)tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 4;

}

 

//实现section

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 4;

}

 

//实现数据的绑定

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

    //实例化一个默认样式的cell  可更改

    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    //设置cell 的文本  类似于lable

    cell.textLabel.text = [NSString stringWithFormat:@"section is %lu,Row is %lu",(long)indexPath.section,(long)indexPath.row];

    

    return cell;

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

猜你喜欢

转载自www.cnblogs.com/wangbaishi/p/9228453.html