首页 >> 大全

iOS 自定义cell

2023-07-25 大全 23 作者:考证青年

思路

1.新建 继承自

2.添加需要使用的属性

3.写方法

4.应用

新建

如图:

添加需要的属性

_定义域怎么求_定义cell

以一个label和一个 为例:

@interface TAYTableViewCell : UITableViewCell@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *yimageView;@end

写方法

必写以下方法:

-():()style :( *)

-(void)

#import "TAYTableViewCell.h"@implementation TAYTableViewCell - (void)awakeFromNib {[super awakeFromNib];// Initialization code
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] ;self.yimageView = [[UIImageView alloc] init] ;[self.contentView addSubview: _yimageView];self.label = [[UILabel alloc] init] ;[self.contentView addSubview: _label] ;return  self;}- (void)layoutSubviews {[super layoutSubviews] ;_yimageView.frame = CGRectMake(150, 150, 100, 100) ;_label.frame = CGRectMake(250, 50, 300, 30) ;
}@end

上面两个方法是自带的,下面两个是自己写的

以上cell的自定义就完了

接下来使用

应用

首先需要在 .h 文件中加入协议

#import @interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>@end

下面的代码在 .m 中

//这段代码在 ViewController.m 中
#import "ViewController.h"
#import "TAYTableViewCell.h"@interface ViewController () {UITableView *tableView;
}@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//创建tableView并初始化self->tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, 414, 640) style:UITableViewStylePlain] ;[self.view addSubview:tableView] ;//设置代理tableView.delegate = self;tableView.dataSource = self;//对cell进行注册[tableView registerClass:[TAYTableViewCell class] forCellReuseIdentifier:@"123"];//加到视图中显示[self.view addSubview:tableView] ;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//首先创建一个自定义cellTAYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"123"];//如果没有获取到cell就创建一个if(!cell) {cell = [[TAYTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];cell.selectionStyle = UITableViewCellSelectionStyleGray;}//设置cell的标签cell.label.text = @"123" ;cell.label.backgroundColor = [UIColor redColor] ;//设置cell的图片cell.imageView.image = [UIImage imageNamed: @"yy.jpg"];//返回cellreturn cell;
}//协议必写函数
//设置每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 1;
}//设置一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 130;
}@end

效果图:

注意

如果在同一个里需要使用的cell样式不同,比如第一个单元格里有一张图和一句话,而第二个单元格里只有一张图,那么需要另自定义一个cell,即定义两个cell。如果你在第二个单元格里仍然使用同一个cell,那么即使你没有给label赋值,它的空间也已经申请了,在层次图中可以看到,会造成空间浪费,并且你想在上面加一个之类的控件,是点击不到的,被label挡住了。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了