当前位置:网站首页>About me writing a custom cell
About me writing a custom cell
2022-07-28 04:07:00 【Light fiction has a good name】
Why customize cell: The system provides cell It's not enough for complex styles , therefore : Customize Cell Just like a custom view , Create one that meets our needs Cell And use this Cell.
- Unregistered
-(id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
If there is no reuse cell, The program may return nil, So it's done cell After that, we must judge the air , If not, recreate . This method can be used without registration .
// Try to get reusable cells . But not necessarily . No return nil
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"mycell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
}
- register
(id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
Get the reused cell, If there is no reuse cell, Will automatically use the provided class Class to create a new cell And back to , There is no need to judge the space later . Before that, you must register cell.
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"mycell"];
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
- difference
The difference in code is , The registered method needs to be reused in advance Cell Class registration , Instead of getting Cell Manually judge dequeue Is the result of nil, This is because dequeueReusableCellWithIdentifier:identifier forIndexPath: This process is handled internally , Make the last return must be available Cell.
Code customization cell:
Establish inheritance UITableViewCell Of MyTableViewCell file , Add the attributes we need for this class
// MyTableViewCell.h file
@interface MyTableViewCell : UITableViewCell
@property UILabel *label;
@property UIButton *button;
@end
rewrite - (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier Method
& - (void) layoutSubviews Method
#import "MyTableViewCell.h"
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if ([self.reuseIdentifier isEqualToString:@"mycell"]) {
_label = [[UILabel alloc] init];
[self.contentView addSubview:_label];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:_btn];
}
return self;
}
// Layout
- (void)layoutSubviews {
_label.frame = CGRectMake(20, 20, 100, 50);
_button.frame = CGRectMake(20, 80, 50, 50);
}
@end
.h Add an agreement to the document
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
[self.view addSubview:self.tableView];
// Set proxy object
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"mycell"];
}
// Group number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Number of rows in the group
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
// Cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
// Set cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
if(indexPath.section == 0) {
cell.label.text = @"Leo";
[cell.btn setImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
} else {
cell.label.text = @"Vincent";
[cell.btn setImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
}
return cell;
}
@end
边栏推荐
- 金仓数据库KingbaseES安全指南--6.1. 强身份验证简介
- Adding DSP library to STM32F103 and its solution
- conda虚拟环境总结与解读
- What technical capabilities should a qualified software testing engineer have?
- UBI read only file system
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)
- Is there a bonus period for robot engineering
- [reach out to Party welfare] the easiest way to scan the H5 page in wechat
- Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
猜你喜欢

Classification cluster analysis

Leetcode58. 最后一个单词的长度

Combination of Oracle and Premier League statistics and presentation

Machine learning 07: Bayesian learning
![[MySQL database] index and transaction (often used in interview)](/img/14/fa96e1f9dd22a1294a2018785e10b6.png)
[MySQL database] index and transaction (often used in interview)

Analysis of static broadcast transmission process

IPC: multi process binder Aidl user guide, including cases
![[image classification] 2021 MLP mixer nips](/img/fb/ca25da210c8da2b6b55f372a325a6c.png)
[image classification] 2021 MLP mixer nips

idea2022更改本地仓库,配置阿里云中央仓库

Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
随机推荐
Redis cluster
Convert py file to exe executable file
"Three no's and five requirements" principle of enterprise Digitalization Construction
I did these three things before the interview, and the result was actually direct
Filters, interceptors, listeners
金仓数据库KingbaseES安全指南--5.2. 数据完整性保护
Dynamic planning - 62. Different paths
Detailed explanation of pl/sql parameters ("box model")
Shell rental reptile
Data rich Computing: m.2 meets AI at the edge
Greed 45. Jumping game II
H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
openpose的一些个人理解
un7.27:redis数据库常用命令。
《关于我写自定义cell这件事》
From Clickhouse to Snowflake: MPP query layer
Notes to subject 2
金仓数据库KingbaseES安全指南--6.1. 强身份验证简介
【伸手党福利】微信中h5网页调起扫一扫最简单的方法
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions