/**
* 可复用cell制作
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 定义cell重用的静态标志符
static NSString *cell_id = @"cell_id_demo";
// 优先使用可复用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
// 如果要复用的cell还没有创建,则创建一个供之后复用
if (cell == nil) {
// 新创建cell并使用cell_id复用符标记
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
}
// 配置cell数据
cell.textLabel.text = [NSString stringWithFormat:@"Cell%i", countNumber];
// 其他cell设置...
return cell;
}
/**
* 分区个数设置为1
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
/**
* 创建20个cell,保证覆盖并超出整个tableView
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
/**
* cell复用机制测试
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 定义cell重用的静态标志符
static NSString *cell_id = @"cell_id_demo";
// 计数用
static int countNumber = 1;
// 优先使用可复用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
// 如果要复用的cell还没有创建,则创建一个供之后复用
if (cell == nil) {
// 新创建cell并使用cell_id复用符标记
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
// 计数器标记新创建的cell
cell.textLabel.text = [NSString stringWithFormat:@"Cell%i", countNumber];
// 计数器递增
countNumber++;
}
return cell;
}
// 假设有三个视图控制器,一个作为父控制器,两个作为子控制器 UIViewController *superVC = [[UIViewController alloc]init]; UITableViewController *subVC1 = [[UITableViewController alloc]init]; UITableViewController *subVC2 = [[UITableViewController alloc]init]; // 将子视图控制器添加到父视图控制器(要注意调整子视图的尺寸和位置合理显示,这里忽略) [superVC.view addSubview:subVC1.view]; [superVC addChildViewController:subVC1]; [superVC.view addSubview:subVC2.view]; [superVC addChildViewController:subVC2]; // 子视图控制器的移除有对称的方法,但只能是子视图控制器主动从父视图控制器中移除 [subVC1.view removeFromSuperview]; [subVC1 removeFromParentViewController]; [subVC2.view removeFromSuperview]; [subVC2 removeFromParentViewController];
// 显示子视图控制器,completion后的代码块如果不为空添加结束后会触发 [[parentVC presentViewController:childVC animated:NO completion:nil]; // 移除子视图控制器,completion后的代码块如果不为空添加结束后会触发 [childVC dismissViewControllerAnimated:NO completion:nil];
@interface Model : NSObject @property (nonatomic,copy) NSString *name; // 姓名 @property (nonatomic,copy) NSString *url; // 图片 @end
// 数据源 @property (nonatomic, strong)NSArray *name_datasource; @property (nonatomic, strong)NSArray *url_datasource; @property (nonatomic, strong)NSMutableArray *datasource;
/**
* 请求数据
*/
- (void)request {
// 姓名数据源
_name_datasource = @[@"张三", @"李四", @"小明", @"小李"];
_url_datasource = @[@"male", @"male", @"male", @"male"];
// 合并数据源
for (int i; i<_name_datasource.count; i++) {
Model *model = [[Model alloc]init];
model.name = _name_datasource[i];
model.url = _url_datasource[i];
[_datasource addObject:model];
}
}
/**
* cell数据回调
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"identifier";
// 自制cell组件
AccountCell *cell = [[AccountCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
/** 多数据源分开对接:**/
// 头像
[cell.avatar setImage:[UIImage imageNamed:_url_datasource[indexPath.row]]];
// 姓名
cell.name.text = _name_datasource[indexPath.row];
// 或者:
/** 数据源合并后对接**/
// 取出对应数据模型
Model *model = _datasource[indexPath.row];
// 头像
[cell.avatar setImage:[UIImage imageNamed:model.url]];
// 姓名
cell.name.text = model.name;
return cell;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有