iOS Developer

基础 (十六) : 等高cell

2016-08-18  本文已影响74人  JonesCxy

等高Cell-frame:

等高Cell-frame

知识回顾

拖入plist文件以及图片

1.(tableView展示数据)

UITableViewController用法

dict-Model(宏)

2.自定义view(cell)

3.完善数据源方法展示数据

新知识

自定义cell的时候,子控件应该添加在initWithStyle方法中, 子控件应该添加在contentView上

{

if (self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier]) {

FunLog

   1.图片

UIImageView *icon_Ima = [[UIImageView alloc] init];

[self.contentView
addSubview:icon_Ima];

self.icon_Ima = icon_Ima;

   2.名称

UILabel *title_Lab = [[UILabel alloc] init];

[self.contentView
addSubview:title_Lab];

self.title_Lab = title_Lab;

   3.价格

...

     4.购买人数

...

}

return

self;

}

布局:注意要先调用[super
layoutSubviews],然后再设置子控件的frame;

{

[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

CGFloat

titleX = ;

CGFloat

titleY = ;

CGFloat

titleW = ;

CGFloat

titleH = ;

self.title_Lab.frame = CGRectMake(titleX, titleY,
titleW, titleH);

 3.价格: 底部和icon对齐,左边和title对齐,宽100,高15

CGFloat

priceX = ;

CGFloat

priceW = ;

CGFloat

priceH = ;

CGFloat

priceY = ;

self.price_Lab.frame = CGRectMake(priceX, priceY,
priceW, priceH);

 4.购买人数: 底部和价格对齐,左边距离价格10,右边10,高13,

CGFloat

buyCountX = ;

CGFloat

buyCountW = ;

CGFloat

buyCountH = ;

CGFloat

buyCountY = ;

self.buyCount_Lab.frame = CGRectMake(buyCountX,
buyCountY, buyCountW, buyCountH);

}

basic框架搭建:

basic框架搭建

拖入plist文件以及图片

1.(tableView展示数据)

UITableViewController用法

warning 别把父类改成了UITableViewController💣

@interface XMGTgController : UITableViewController

(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section

{

 1.获取cell

XMGTgCell *cell = [XMGTgCell cellWithTableView:tableView];

 2.覆盖数据

cell.tg

= self.tgs[indexPath.row];

 3.返回cell

return cell;

}

dict-Model(宏)

懒加载

{

if (!_tgs) {

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];

NSArray *dicts = [NSArray arrayWithContentsOfFile:filePath];

NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dicts.count];

for (NSDictionary *dictin dicts) {

  XMGTg *obj = [XMGTg tgWithDict:dict];

 
  [arrayM addObject:obj];

}

_tgs = [arrayM copy];

}

return _tgs;

}

自定义view(cell)

2.自定义view(cell)

{

static NSString *identifier =@"TgCellID";

XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (!cell) {

cell = [[self alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

}

return cell;

}

@class XMGTg;

@interface XMGTgCell : UITableViewCell

@property (nonatomic, strong) XMGTg tg; /< tg模型/

@end

此处给子控件赋值

{

_tg =

tg;

warning

noCode

}

传统通过代码自定义子控件,要将添加子控件的代码写在这个方法中

布局:注意要先调用[super layoutSubviews];

{

[super layoutSubviews];

}


subViews添加子控件:

subViews添加子控件

在这里模拟拖线,创建weak类型的子控件,并且在initWithStyle方法中创建添加到cell上

@property (nonatomic, weak) UIImageView icon_Ima; /*< 图片 */

@property (nonatomic, weak) UILabel title_Lab; /*< 名称 */

@property (nonatomic, weak) UILabel price_Lab; /*< 价格 */

@property (nonatomic, weak) UILabel buyCount_Lab; /*< 购买人数 */

自定义cell的时候,子控件应该添加在initWithStyle方法中, 子控件应该添加在contentView上

{

if (self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier]) {

FunLog

   1.图片

UIImageView *icon_Ima = [[UIImageView alloc] init];

[self.contentView
addSubview:icon_Ima];

self.icon_Ima = icon_Ima;

   2.名称

UILabel *title_Lab = [[UILabel alloc] init];

[self.contentView
addSubview:title_Lab];

self.title_Lab = title_Lab;

   3.价格

...

     4.购买人数

...

}

return

self;

}


layoutSubViews布局子控件

布局:注意要先调用[super 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

CGFloat

titleX = iconX + iconW + margin;

CGFloat

titleY = iconY;

CGFloat titleW = self.bounds.size.width - titleX - margin; // cell宽度 - 右边距离- title左边距离titleX;

CGFloat

titleH = 20;

self.title_Lab.frame = CGRectMake(titleX, titleY,
titleW, titleH);

 3.价格: 底部和icon对齐,左边和title对齐,宽100,高15

CGFloat

priceX = titleX;

CGFloat

priceW = 100;

CGFloat

priceH = 15;

CGFloat priceY = iconY + iconH - priceH; // 底部对齐相当于
priceY + priceH = iconY + iconH;

self.price_Lab.frame = CGRectMake(priceX, priceY,
priceW, priceH);

 4.购买人数: 底部和价格对齐,左边距离价格10,右边10,高13,

CGFloat

buyCountX = priceX + priceW + margin; // priceX +
priceW +

CGFloat

buyCountW = self.bounds.size.width - 10 -
buyCountX; // buyCountX + buyCountW = cellW - 10;

CGFloat

buyCountH = 13;

CGFloat buyCountY = priceY + priceH - buyCountH; // 底部对齐相当于 priceY + priceH = buyCountY + buyCountH;

self.buyCount_Lab.frame = CGRectMake(buyCountX,
buyCountY, buyCountW, buyCountH);

}

frameFun补充

在调用完layoutSubview,约束才会布局

当强制布局后,子控件的约束就变成frame了

NSLog(@"cellH = %.f", CGRectGetHeight(self.bounds));

NSLog(@"maxXoficon = %.f", CGRectGetMaxY(self.icon_Ima.frame));


等高cell--Mansonry

等高Cell-Mansonry框架

define
this constant if you want to use Masonry without the 'mas_' prefix

define

MAS_SHORTHAND

define
this constant if you want to enable auto-boxing for default syntax

define

MAS_SHORTHAND_GLOBALS

import "Masonry.h"

等高cell-xib


等高cell:

等高cell-storyboard

{

if (indexPath.row != 0) {

     1.获取cell

XMGTgCell *cell = [XMGTgCell cellWithTableView:tableView];

     2.覆盖数据

cell.tg = self.tgs[indexPath.row];

     3.返回cell

return cell;

}else

{

可以通过第0行的特殊cell来满足/ 代替 表头视图

return [tableView
dequeueReusableCellWithIdentifier:@"testID"];

}

}


静态单元格

staticCells静态单元格

一般的死数据界面都可以用静态cell

[self viewWithTag:22];

customSeparator自定义分割线

系统自带的分割线不能满足产品需求

设置分割线颜色为透明色

warning 在cell中,尽量不要使用透明色

self.tableView.separatorColor = [UIColor
clearColor];

设置分割线样式为None

self.tableView.separatorStyle =
UITableViewCellSeparatorStyleNone;

另一种自定义分割线的方法

设置好子控件以后会自动调用这个方法

{

 另一种快速设置cell分割线的土方法

self.layer.borderColor = [UIColor colorWithRed:0.5
green:0.5 blue:0.5 alpha:0.3].CGColor;

self.layer.borderWidth = 0.5;

}

通过代码自定义分割线与添加子控件一样

上一篇 下一篇

猜你喜欢

热点阅读