IOS TableView组件二:可视化组件的创建


之前通过纯代码的方式在viewController中完成tableView的创建和使用,接下来如何通过xib文件的方式创建一个tableView.

使用

创建一个新的viewController出来,要把Also create XIB file钩上
在这里插入图片描述
找到tableview,拖拽到视图中
在这里插入图片描述
并调整大小,添加约束。
在这里插入图片描述

按住control键,点击tableview拖到@interface下,生成一个tableview的对象属性,可以在之后的代码中使用它。
在这里插入图片描述
拖动到File’s Owner,因为File’s Owner本身就是SecondTableViewController,
在这里插入图片描述
在这里插入图片描述
选中dataSource,暂时不管delegate,成功把tableview的dataSource设置成控制器本身了。
既然遵守了协议,就要实现协议中的方法。
还是在代码中@interface写一下协议,但是不用写数据源是什么了。

#import "SecondTableViewController.h"

@interface SecondTableViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SecondTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - table view data source -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    cell.textLabel.text=@"Text";
    return cell;
}

如何自定义一个tableview的类,去完成tableview的创建和封装

另外创建一个view controller,不勾选Also create XIB file。然后再自定义一个继承自UITableView的类,还要创建一个和本类相关联的xib文件出来
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
新建的xib文件的File’s Owner,里完成类的关联
在这里插入图片描述
拖拽一个tableview上去,注意界面的设备和模拟设备要相同,视图才能恰当的分布。

CustomTableView.h中声明一个类方法,用于在controller中去实例话这个类的对象,用一个工厂方法。
传入一个人一类型的,只要遵循datasource协议这个对象的协议即可。

//CustomTableView.h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomTableView : UITableView
+(instancetype)tableViewWithData:(id<UITableViewDataSource>)dataSource;
@end

实现该函数,加载xib文件。[NSBundle mainBundle] loadNibNamed,函数返回结果是一个数组,是因为在一个xib文件中可能有多个视图。

//CustomTableView.m
#import "CustomTableView.h"

@implementation CustomTableView
+(instancetype)tableViewWithData:(id<UITableViewDataSource>)dataSource{
    CustomTableView *tableView=[[[NSBundle mainBundle] loadNibNamed:@"CustomTableView" owner:self options:nil] lastObject];
    [tableView setDataSource:dataSource];
    return tableView;
}
@end

ThirdTableViewController.m中,创建一个customTableView的类,并将它条驾到视图控制器中。并声明代理,和实现代理方法。

#import "ThirdTableViewController.h"
#import "CustomTableView.h"

@interface ThirdTableViewController ()<UITableViewDataSource>

@end

@implementation ThirdTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CustomTableView *tableView=[CustomTableView tableViewWithData:self];
    [self.view addSubview:tableView];
}
#pragma mark - table view data source -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    cell.imageView.image=[UIImage imageNamed:@"1.jpg"];
    return cell;
}

在这里插入图片描述
这就是第三种创建tableView的方式。
可以看到,我们的dataSource的实现部分都是在controller当中的,在后续的开发中,controller会承载很多的代码,无疑会使controller越来越庞大。可以吧dataSource做一个封装,把它提取出来。
创建一个继承自NSObject的模型类。
在这里插入图片描述
在这里插入图片描述
创建好后,要想让它成为tableview的dataSource,一样必须要让它遵守dataSource的协议。需要更换库为UIKIT
在这里插入图片描述

在这里插入图片描述
在FouthTableViewController.m中去创建我们的tableViewDataSource的时候,不能在viewDidLoad中去创建一个局部的变量,因为创建一个局部变量出来,不是强引用,并且设置成tableView的dataSource的代理,,局部变量出了viewDidLoad后,就会被释放掉,tableview在加载数据源的时候就无从下手了。所以数据源的生命周期是要伴随tableview本身的。说明一个强引用的属性
zhi

#import "FouthTableViewController.h"
#import "CustomTableView.h"
#import "tableViewDataSource.h"
@interface FouthTableViewController ()
@property (nonatomic,strong)tableViewDataSource *dataSource;
@end

@implementation FouthTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setDataSource:[tableViewDataSource dataSource]];
    CustomTableView *tableView=[CustomTableView tableViewWithData:self.dataSource];
    [self.view addSubview:tableView];
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值