iOS中自定义UITableViewCell的用法
2024-09-30 09:34:38
1、先创建一个View继承 UITableViewCell并使用xib快速建立模型。
#import <UIKit/UIKit.h>
#import "Score.h" @interface ShowScoreCell : UITableViewCell
//在.h文件中声明对象
@property(nonatomic,strong)Score *score;
@end
2、把需要的控件拖上xib并调整xib的大小
3、把对应控件连线至.m文件中
#import "ShowScoreCell.h" @interface ShowScoreCell()
//以下属性都有连线
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *positionLabel; @property (weak, nonatomic) IBOutlet UILabel *paperLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *view; @end
4、在调用实体的set方法时,给面面中需要的控件赋值
- (void)setScore:(Score *)score{
_score = score;
self.nameLabel.text = score.name;
self.positionLabel.text = score.position;
self.paperLabel.text = score.paper;
self.scoreLabel.text = score.score;
}
5、根据需要重写每个控件
//重写
-(void)layoutSubviews
{
[super layoutSubviews]; if (self.selected)
{
//可以单独设置每一个Label选中时的背景
// self.nameLabel.backgroundColor = [UIColor redColor];
// self.positionLabel.backgroundColor = [UIColor redColor];
// self.paperLabel.backgroundColor = [UIColor redColor];
// self.scoreLabel.backgroundColor = [UIColor lightGrayColor];
//也可以直接设置用来放Label的View的背景
self.view.backgroundColor = [UIColor redColor]; } }
6、在视图控制器中做以下工作
1> 拿到实体对象的数据,一般用懒加载的形式存在数组中。
- (NSArray *)allScores{
if (!_allScores) {
_allScores = [[DBManager shareManager] allScore];
}
return _allScores;
}
2>在viewDidLoad方法中注册自定义的Cell
- (void)viewDidLoad {
[super viewDidLoad]; [self.navigationController setNavigationBarHidden:NO];
self.navigationItem.title = @"查看成绩";
//nibWithNibName:自定义Cell的名字
[self.tableView registerNib:[UINib nibWithNibName:@"ShowScoreCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(beginEditing:)];
}
提前声明一个cellIdentifier,在@implementation之前
static NSString *cellIdentifier = @"scoreCell";
3>在返回每个单元格时使用自定义的Cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ShowScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// if (!cell) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// }
//设计cell
Score *s = self.allScores[indexPath.row];
cell.score = s; return cell;
}
最新文章
- BZOJ 后缀自动机四&#183;重复旋律7
- java中的算术运算符、赋值运算符、比较运算符、逻辑运算符、条件运算符
- java基础知识点复习
- 【微信开发之问题集锦】redirect_uri 参数错误
- (Problem 2)Even Fibonacci numbers
- 解析 .Net Core 注入 (1) 注册服务
- OJ题:奇偶归一猜想——求归一过程中的最大值
- 补习系列(8)-springboot 单元测试之道
- 【codeforces 914H】Ember and Storm&#39;s Tree Game
- HTML 滚动条样式修改
- bootstrap知识笔记
- kali 2016.2安装及配置
- 把.html转换成.jsp中jqplot画图表不能正常显示,出错的心得
- 使用 NuGet 管理我们的程序集 - 预发行版
- Mac pycharm专业版安装以及破解方法
- JS三种消息框的使用
- python 2.0 s12 day5 常用模块介绍
- ubuntu sudo apt-get update与sudo apt-get upgrade的作用及区别,以及python pip的安装
- PHP消息队列实现
- jenkins没安装git报错
热门文章
- EF(Entity Framework)通用DBHelper通用类,增删改查以及列表
- linux centos7 安装常用软件java,node,mysql,Seafile
- Finally语句块的运行
- 将线上服务器生成的日志信息实时导入kafka,采用agent和collector分层传输,app的数据通过thrift传给agent,agent通过avro sink将数据发给collector,collector将数据汇集后,发送给kafka
- https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py
- field 属性操作
- myeclipse -vmargs -Xmx512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=64m
- Hadoop MapReduce两种架构 以及 YARN
- Swift(二)控制流
- 移动前端开发之viewport的理解