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;
}

iOS中自定义UITableViewCell的用法的更多相关文章

  1. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  2. iOS中 自定义cell分割线/分割线偏移 韩俊强的博客

    在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...

  3. ios中自定义checkbox

    //自定义button#import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton. ...

  4. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  5. iOS中 自定义cell升级版 (高级)

    接上次分享的自定义cell进行了优化:http://blog.csdn.net/qq_31810357/article/details/49611255 指定根视图: self.window.root ...

  6. ios中自定义图层的2种方法

    1:自定义图层,在图层中画图 #import <QuartzCore/QuartzCore.h> @interface MJLayer : CALayer @end #import &qu ...

  7. ios中自定义图层

    图层和VIEW的区别 1:view不具备显示功能,是因view内部有一个图层,才能显示出来 2:图层不具备事件功能,VIEW继承UIRespone具有处理事件功能 3:自定义的图层有一个影式动画,VI ...

  8. ios中webview的高级用法(二)

     框架: webview与js的通信框架 #import "MJViewController.h" #import "MBProgressHUD+Add.h" ...

  9. ios中陀螺仪CoreMotion的用法

    转自:http://code.eoe.cn/471/title/ios涓檧铻轰华CoreMotion鐨勭敤娉 README.md 外部引用 原始文档 以前在iphone中要得到加速度时,只能使用Ac ...

随机推荐

  1. Android Baseline小tip

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40709353 Baseline Alignment

  2. redis10---Setbit 的实际应用

    Setbit 的实际应用 场景: 1亿个用户, 每个用户 登陆/做任意操作 ,记为 今天活跃,否则记为不活跃 每周评出: 有奖活跃用户: 连续7天活动,每月评,等等. 思路: Userid dt ac ...

  3. (C/C++)register关键字

    register:这个关键字的作用是请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率. 注意是尽可能,不是绝对.一个CPU 的寄存器也就那么几个或几十个,你要是定义了 ...

  4. httpd2.4.27rpm包制作

    http2.4.27 rpm包制作1.安装rpm-buildyum -y install rpm-build2.使用普通用户创建spec规则文件su - lxhvim httpd.spec Name: ...

  5. uestc 250 windy数(数位dp)

    题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路:数位dp #include<iostream ...

  6. My Notes

    1.类似于border.margin.padding的四个方向数值顺序为上右下左.2.属性z-index参数值越大,则被层叠在最上面.3.标签<a>和属性display:block和适合在 ...

  7. 利用百度地图API制作房产酒店地图

    摘要: 想亲手制作一张酷讯.去哪儿.安居客.链接地产那样的房产.酒店地图麼?那赶快来学习吧.(以酷讯为例,如下图) 更多成功案例请点击:http://dev.baidu.com/wiki/map/in ...

  8. I.MX6 U-boot imxotp MAC address 写入

    /***************************************************************************** * I.MX6 U-boot imxotp ...

  9. SPOJ:NO GCD (求集合&秒啊)

    You are given N(1<=N<=100000) integers. Each integer is square free(meaning it has no divisor ...

  10. Vue之组件的内容分发

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUF ...