1.UITableViewController用法。
(1)设置ViewController继承与UITableViewController
(2)删除stroyboard中所有的控制器
(3)拖进新的UITableViewController
(4)设置新控制器的Class:ViewController
(5)设置新的is initial
(6) 实现数据源代理方法
-plist数据->模型数组
(1)根据plist文件数据中的样式判断转换成json的样式
(2)根据dict的keys创建模型Model
(3)解析plist文件(自定义代码块)
2:自定义cell
(1)创建一个继承与UITableViewCell的类:例如contentCell
(2)写好封装cell的代码
(3)为cell添加数据模型Model,并且重写set方法,在该方法中给UI数据进行设置
(4)重写initWithFram方法添加控件
(5)重写layoutSubviews方法(⚠️先要调用父类的layoutSubviews. [super layoutSubviews ])
3:完善数据源方法显示数据。
——在自定义cell中添加子控件
(1)自定义cell的时候,子控件应该在initWithStyle方法中添加到contentView上
#import "contentCell.h"
@interface contentCell ()
@property(nonatomic,strong)UIImageView * Icon_Ima;
@property(nonatomic,strong)UILabel * Title_Lab;
@end
@implementation contentCell
//自定义cell的时候,添加子控件应该在这个方法中实现
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
UIImageView * icon_Ima = [[UIImageView alloc]init];
[self.contentView addSubview:icon_Ima];
self.Icon_Ima = icon_Ima;
UILabel * title_Lab = [[UILabel alloc]init];
[self.contentView addSubview:title_Lab];
self.Title_Lab = title_Lab;
}
return self;
}
//布局子控件
-(void)layoutSubviews
{
//布局子控件之前 调用父类的方法
[super layoutSubviews];
CGFloat margin = 10;
//1.布局图片:上,左,上下间距相同,宽度80
CGFloat iconX = margin;
CGFloat iconY = margin;
CGFloat iconW = 80;
CGFloat iconH = self.bounds.size.height - 2 * iconY;
self.Icon_Ima.frame = CGRectMake(iconX, iconY, iconW, iconH);
//2.布局title:顶部对齐icon,左边距离图片10,右边距离10,高度20 宽度80
CGFloat titleX = CGRectGetWidth(self.Icon_Ima.frame) + 10;
CGFloat titleY = margin;
CGFloat titleW = 80;
CGFloat titleH = 20;
self.Title_Lab.frame = CGRectMake(titleX, titleY, titleW, titleH);
}
- plist数据->模型数组
* 根据plist中数据的样式判断转成JSON的样式
* 根据dict的keys创建模型XMGTg
* 解析plist文件(自定义代码块)
* dict-Model(宏)
懒加载
- (NSArray *)tgs
{
if (!_tgs) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
NSArray *dicts = [NSArray arrayWithContentsOfFile:filePath];
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dicts.count];
for (NSDictionary *dict in dicts) {
XMGTg *obj = [XMGTg tgWithDict:dict];
[arrayM addObject:obj];
}
_tgs = [arrayM copy];
}
return _tgs;
}
自定义view(cell)
2.自定义view(cell)
- 创建一个继承UITableViewCell的类:XMGTgCell
- 写好封装cell的代码(传统写法)
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *identifier = @"TgCellID";
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
- 为cell增添模型属性tg,并且重写set方法,先给系统自带的赋值
@class XMGTg;
@interface XMGTgCell : UITableViewCell
@property (nonatomic, strong) XMGTg *tg; /**< tg模型 */
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
此处给子控件赋值
- (void)setTg:(XMGTg *)tg
{
_tg = tg;
#warning noCode
}
静态单元格
staticCells静态单元格
一般的死数据界面都可以用静态cell
- 例如:微信的发现 / 我两个界面,iPhone的 设置 界面
- 样式都是group样式
- 调整组间距的方法有两种:
* 1.设置组头/尾 为@" "这种空字符串
* 2.添加没有cell的section
- 自定义静态cell需要注意:
* 拖线需要先写代码,在通过代码拖
* 也可以通过tag获取子控件操作
[self viewWithTag:22];
customSeparator自定义分割线
系统自带的分割线不能满足产品需求
* 设置cell不同的背景色
* 自定义分割线
* 删除原有的分割线
* 1.设置tableView.separatorStyle为none
* 2.设置分割线颜色为clearColor(不建议cell中不要使用clearColor)
* 添加一个view,高度为1,背景色设置成分割线颜色,贴底
设置分割线颜色为透明色
warning 在cell中,尽量不要使用透明色
self.tableView.separatorColor = [UIColor clearColor];
设置分割线样式为None
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
另一种自定义分割线的方法
设置好子控件以后会自动调用这个方法
- (void)awakeFromNib
{
另一种快速设置cell分割线的土方法
self.layer.borderColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3].CGColor;
self.layer.borderWidth = 0.5;
}