iOS开发之cell中self 和selfios cell.contentvieww的区别

iOS开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 - 文顶顶 - 博客园
最怕你一生碌碌无为 还安慰自己平凡可贵
ios开发UI篇&使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
一、实现效果
二、使用纯代码自定义一个tableview的步骤
1.新建一个继承自UITableViewCell的类
2.重写initWithStyle:reuseIdentifier:方法
添加所有需要显示的子控件(不需要设置子控件的数据和frame, &子控件要添加到contentView中)
进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
3.提供2个模型
数据模型: 存放文字数据\图片数据
frame模型: 存放数据模型\所有子控件的frame\cell的高度
4.cell拥有一个frame模型(不要直接拥有数据模型)
5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame&
6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)
三、文件结构和实现代码
1.文件结构
2.实现代码:
NJWeibo.h文件
1 #import &Foundation/Foundation.h&
3 @interface NJWeibo : NSObject
4 @property (nonatomic, copy) NSString * // 内容
5 @property (nonatomic, copy) NSString * // 头像
6 @property (nonatomic, copy) NSString * // 昵称
7 @property (nonatomic, copy) NSString * // 配图
8 @property (nonatomic, assign) BOOL
10 - (id)initWithDict:(NSDictionary *)
11 + (id)weiboWithDict:(NSDictionary *)
NJWeibo.m文件
1 #import "NJWeibo.h"
3 @implementation NJWeibo
5 - (id)initWithDict:(NSDictionary *)dict
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
13 + (id)weiboWithDict:(NSDictionary *)dict
return [[self alloc] initWithDict:dict];
NJWeiboCell.h文件
1 #import &UIKit/UIKit.h&
2 @class NJWeiboF
4 @interface NJWeiboCell : UITableViewCell
接收外界传入的模型
8 //@property (nonatomic, strong) NJWeibo *
10 @property (nonatomic, strong) NJWeiboFrame *weiboF
12 + (instancetype)cellWithTableView:(UITableView *)tableV
NJWeiboCell.m文件
1 #import "NJWeiboCell.h"
2 #import "NJWeibo.h"
3 #import "NJWeiboFrame.h"
5 #define NJNameFont [UIFont systemFontOfSize:15]
6 #define NJTextFont [UIFont systemFontOfSize:16]
8 @interface NJWeiboCell ()
12 @property (nonatomic, weak) UIImageView *iconV
16 @property (nonatomic, weak) UIImageView *vipV
20 @property (nonatomic, weak) UIImageView *pictureV
24 @property (nonatomic, weak) UILabel *nameL
28 @property (nonatomic, weak) UILabel *introL
31 @implementation NJWeiboCell
33 + (instancetype)cellWithTableView:(UITableView *)tableView
// NSLog(@"cellForRowAtIndexPath");
static NSString *identifier = @"status";
// 1.缓存中取
NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[NJWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
构造方法(在初始化对象的时候会调用)
一般在这个方法中添加需要显示的子控件
51 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 让自定义Cell和系统的cell一样, 一创建出来就拥有一些子控件提供给我们使用
// 1.创建头像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconV
// 2.创建昵称
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.font = NJNameF
// nameLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:nameLabel];
self.nameLabel = nameL
// 3.创建vip
UIImageView *vipView = [[UIImageView alloc] init];
vipView.image = [UIImage imageNamed:@"vip"];
[self.contentView addSubview:vipView];
self.vipView = vipV
// 4.创建正文
UILabel *introLabel = [[UILabel alloc] init];
introLabel.font = NJTextF
introLabel.numberOfLines = 0;
// introLabel.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:introLabel];
self.introLabel = introL
// 5.创建配图
UIImageView *pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureView];
self.pictureView = pictureV
92 - (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame
_weiboFrame = weiboF
// 1.给子控件赋值数据
[self settingData];
// 2.设置frame
[self settingFrame];
设置子控件的数据
106 - (void)settingData
NJWeibo *weibo = self.weiboFrame.
// 设置头像
self.iconView.image = [UIImage imageNamed:weibo.icon];
// 设置昵称
self.nameLabel.text = weibo.
// 设置vip
if (weibo.vip) {
self.vipView.hidden = NO;
self.nameLabel.textColor = [UIColor redColor];
self.vipView.hidden = YES;
self.nameLabel.textColor = [UIColor blackColor];
// 设置内容
self.introLabel.text = weibo.
// 设置配图
if (weibo.picture) {// 有配图
self.pictureView.image = [UIImage imageNamed:weibo.picture];
self.pictureView.hidden = NO;
self.pictureView.hidden = YES;
设置子控件的frame
138 - (void)settingFrame
// 设置头像的frame
self.iconView.frame = self.weiboFrame.iconF;
// 设置昵称的frame
self.nameLabel.frame = self.weiboFrame.nameF;
// 设置vip的frame
self.vipView.frame = self.weiboFrame.vipF;
// 设置正文的frame
self.introLabel.frame = self.weiboFrame.introF;
// 设置配图的frame
if (self.weiboFrame.weibo.picture) {// 有配图
self.pictureView.frame = self.weiboFrame.pictrueF;
计算文本的宽高
@param str
需要计算的文本
@param font
文本显示的字体
@param maxSize 文本显示的范围
@return 文本占用的真实宽高
169 - (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
NSDictionary *dict = @{NSFontAttributeName : font};
// 如果将来计算的文字的范围超出了指定的范围,返回的就是指定的范围
// 如果将来计算的文字的范围小于指定的范围, 返回的就是真实的范围
CGSize size =
[str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].
NJWeiboFrame.h文件
专门用来保存每一行数据的frame, 计算frame
3 #import &Foundation/Foundation.h&
4 @class NJW
5 @interface NJWeiboFrame : NSObject
头像的frame
9 @property (nonatomic, assign) CGRect iconF;
昵称的frame
13 @property (nonatomic, assign) CGRect nameF;
vip的frame
17 @property (nonatomic, assign) CGRect vipF;
正文的frame
21 @property (nonatomic, assign) CGRect introF;
配图的frame
25 @property (nonatomic, assign) CGRect pictrueF;
29 @property (nonatomic, assign) CGFloat cellH
34 @property (nonatomic, strong) NJWeibo *
NJWeiboFrame.m文件
1 #import "NJWeiboFrame.h"
2 #import "NJWeibo.h"
3 #define NJNameFont [UIFont systemFontOfSize:15]
4 #define NJTextFont [UIFont systemFontOfSize:16]
7 @implementation NJWeiboFrame
10 - (void)setWeibo:(NJWeibo *)weibo
CGFloat padding = 10;
// 设置头像的frame
CGFloat iconViewX =
CGFloat iconViewY =
CGFloat iconViewW = 30;
CGFloat iconViewH = 30;
self.iconF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
// 设置昵称的frame
// 昵称的x = 头像最大的x + 间隙
CGFloat nameLabelX = CGRectGetMaxX(self.iconF) +
// 计算文字的宽高
CGSize nameSize = [self sizeWithString:_weibo.name font:NJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
CGFloat nameLabelH = nameSize.
CGFloat nameLabelW = nameSize.
CGFloat nameLabelY = iconViewY + (iconViewH - nameLabelH) * 0.5;
self.nameF = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
// 设置vip的frame
CGFloat vipViewX = CGRectGetMaxX(self.nameF) +
CGFloat vipViewY = nameLabelY;
CGFloat vipViewW = 14;
CGFloat vipViewH = 14;
self.vipF = CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);
// 设置正文的frame
CGFloat introLabelX = iconViewX;
CGFloat introLabelY = CGRectGetMaxY(self.iconF) +
CGSize textSize =
[self sizeWithString:_weibo.text font:NJTextFont maxSize:CGSizeMake(300, MAXFLOAT)];
CGFloat introLabelW = textSize.
CGFloat introLabelH = textSize.
self.introF = CGRectMake(introLabelX, introLabelY, introLabelW, introLabelH);
// 设置配图的frame
CGFloat cellHeight = 0;
if (_weibo.picture) {// 有配图
CGFloat pictureViewX = iconViewX;
CGFloat pictureViewY = CGRectGetMaxY(self.introF) +
CGFloat pictureViewW = 100;
CGFloat pictureViewH = 100;
self.pictrueF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
// 计算行高
self.cellHeight = CGRectGetMaxY(self.pictrueF) +
// 没有配图情况下的行高
self.cellHeight = CGRectGetMaxY(self.introF) +
计算文本的宽高
@param str
需要计算的文本
@param font
文本显示的字体
@param maxSize 文本显示的范围
@return 文本占用的真实宽高
80 - (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
NSDictionary *dict = @{NSFontAttributeName : font};
// 如果将来计算的文字的范围超出了指定的范围,返回的就是指定的范围
// 如果将来计算的文字的范围小于指定的范围, 返回的就是真实的范围
CGSize size =
[str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].
NJViewController.m文件
1 #import "NJViewController.h"
2 #import "NJWeibo.h"
3 #import "NJWeiboCell.h"
4 #import "NJWeiboFrame.h"
6 @interface NJViewController ()
7 @property (nonatomic, strong) NSArray *statusF
10 @implementation NJViewController
12 #pragma mark - 数据源方法
14 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.statusFrames.
20 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NJWeiboCell *cell = [NJWeiboCell cellWithTableView:tableView];
// 3.设置数据
cell.weiboFrame = self.statusFrames[indexPath.row];
29 #pragma mark - 懒加载
30 - (NSArray *)statusFrames
if (_statusFrames == nil) {
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
// 创建模型
NJWeibo *weibo = [NJWeibo weiboWithDict:dict];
// 根据模型数据创建frame模型
NJWeiboFrame *wbF = [[NJWeiboFrame alloc] init];
wbF.weibo =
[models addObject:wbF];
self.statusFrames = [models copy];
return _statusF
50 #pragma mark - 代理方法
51 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// NSLog(@"heightForRowAtIndexPath");
// 取出对应航的frame模型
NJWeiboFrame *wbF = self.statusFrames[indexPath.row];
NSLog(@"height = %f", wbF.cellHeight);
return wbF.cellH
60 - (BOOL) prefersStatusBarHidden
return YES;
四、补充说明
由于系统提供的tableview可能并不能满足我们的开发需求,所以经常要求我们能够自定义tableview。
自定义tableview有两种方式,一种是使用xib创建,一种是使用纯代码的方式创建。
对于样式一样的tableview,通常使用xib进行创建,对于高度不一样,内容也不完全一致的通常使用纯代码进行自定义。
随笔 - 178
评论 - 1401IOS中UITableviewCell 这种效果怎么实现? - 跟谁学
搜索你想学的科目、老师试试,例如“钢琴”搜索吉安
&&IOS中UITableviewCell 这种效果怎么实现?如图:所示 OC纯代码布局 TableviewCell 的两端并没有对齐屏幕(箭头中指的线就是屏幕的边框) 还有就是上面三个cell. 第0行的 左上 和 右上 有圆角.第1行的是 没有圆角 的.第2行的 左下 和 右下 有圆角 这样的效果怎么封装? 快快快,代码贴出来啊...谢谢啦...我就叫土豆pandadou
圆角的cell,之前iOS 6的时候是这种的(拟物),iOS 7 (扁平化)之后就进行了修改!主要的实现在tableView的代理方法中 - (void)tableView:willDisplayCell: forRowAtIndexPath: ,绘制table view cell 的背景view。下面的代码,你可以直接拷贝到控制器中演示效果,如果要集成到你自己的代码中,你需要把
- (void)tableView:willDisplayCell: forRowAtIndexPath: 这个方法中的东西拷贝过去,还要修改table view 的样式是分组和分割线为 none,这些在 - viewDidLoad中你也可以找到。//
圆角cell#import "ViewController.h"@interface ViewController ()&UITableViewDelegate,UITableViewDataSource&@property (strong, nonatomic)NSArray *dataS@end@implementation ViewController//懒加载--数据源- (NSArray *)dataSource{
if (!_dataSource) {
_dataSource = @[@[@"我发布的",@"我回复的",@"我喜欢的"],@[@"关于我们",@"退出登录"]];
_dataS}- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark - 这里注意样式--分组
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
tableView.dataSource =
tableView.delegate =#pragma mark - 把原生的分割线去掉
tableView.separatorStyle = UITableViewCellSeparatorStyleN#pragma mark - 这里我不让cell选择了
tableView.allowsSelection = NO;
[self.view addSubview:tableView];}#pragma mark- UITableViewDelegate or dataSource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
self.dataSource.}//单元格的个数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = self.dataSource[section];
array.}//cell创建- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
cell.textLabel.text = self.dataSource[indexPath.section][indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"icon"];}#pragma mark- cell 将要显示的时候调用这个方法,就在这个方法内进行圆角绘制/**本质:就是修改cell的背景view,这个view的layer层自己分局cell的类型(顶,底和中间)来绘制*/- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(tintColor)])
CGFloat cornerRadius = 5.f;//圆角大小
cell.backgroundColor = [UIColor clearColor];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
else if (indexPath.row == 0)
//最顶端的Cell(两个向下圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
//最底端的Cell(两个向上的圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
//中间的Cell
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
layer.path = pathR
CFRelease(pathRef);
layer.fillColor = [UIColor whiteColor].CGC //cell的填充颜色
layer.strokeColor = [UIColor lightGrayColor].CGC //cell 的边框颜色
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
lineLayer.backgroundColor = [UIColor lightGrayColor].CGC
//绘制中间间隔线
[layer addSublayer:lineLayer];
UIView *bgView = [[UIView alloc] initWithFrame:bounds];
[bgView.layer insertSublayer:layer atIndex:0];
bgView.backgroundColor = UIColor.clearC
cell.backgroundView = bgV
}}@end效果图:
wenghengcong
我之前这种排列也可以不用UITableViewCell,而是直接用自定义UIView.外面整个有圆角,里面放两个按钮即可。假如用UITableViewCell,那么其圆角最好是UI给带圆角的背景图,然后自定义里面的视图就行。
自定义cell然后重写 setframe
我就叫土豆
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath这个方法比layoutSubviews方法先调用,以至于我在layoutSubviews方法修改了Cell的宽度.#import "LMSMeViewCell.h"@implementation LMSMeViewCell- (void)layoutSubviews {
[super layoutSubviews];
//修改cell的frame
CGRect rect = self.
//如果已经修改过就不用修改了
if (self.frame.size.width == SCREEN_WIDTH) {
NSLog(@"%f",SCREEN_WIDTH);
rect = CGRectMake(self.frame.origin.x + 10, self.frame.origin.y, self.frame.size.width - 20, self.frame.size.height);
self.frame =
NSLog(@"**********%@",NSStringFromCGRect(rect));
self.textLabel.font = [UIFont systemFontOfSize:13];
self.textLabel.textColor = [UIColor colorWithRed:0.51 green:0.51 blue:0.51 alpha:1];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(10);
make.width.height.offset(22);
make.centerY.equalTo(self.contentView);
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.imageView.mas_right).offset(20);
make.centerY.equalTo(self.imageView.mas_centerY);
}];}@endController关键代码:#pragma mark- cell 将要显示的时候调用这个方法,就在这个方法内进行圆角绘制/** 本质:就是修改cell的背景view,这个view的layer层自己分局cell的类型(顶,底和中间)来绘制*/- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"___");
if ([cell respondsToSelector:@selector(tintColor)])
CGFloat cornerRadius = 6.f;//圆角大小
cell.backgroundColor = [UIColor clearColor];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
NSLog(@"___%@",NSStringFromCGRect(bounds));
CGRect bounds = CGRectInset(cell.frame, 0, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
else if (indexPath.row == 0)
//最顶端的Cell(两个向下圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
//最底端的Cell(两个向上的圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
//中间的Cell
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
layer.path = pathR
CFRelease(pathRef);
layer.fillColor = [UIColor orangeColor].CGC //cell的填充颜色//
layer.strokeColor = [UIColor lightGrayColor].CGC //cell 的边框颜色
layer.strokeColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGC
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
lineLayer.backgroundColor = [UIColor lightGrayColor].CGC
//绘制中间间隔线
[layer addSublayer:lineLayer];
UIView *bgView = [[UIView alloc] initWithFrame:bounds];
[bgView.layer insertSublayer:layer atIndex:0];
bgView.backgroundColor = UIColor.clearC
cell.backgroundView = bgV
相关问题大家都在看最新提问
关注我们官方微信关于跟谁学服务支持帮助中心你的位置: >
> iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
在section=10;row=1;的UITableView中,每一个cell都带有一个按钮,例如如下的图片一样
每一个cell中都有一个“进入店铺的按钮”,但是如果我点击相应的cell要进入对应的店铺如何处理呢?
如果用”- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath”这个方法的话会的确可以用“indexPath.section”定位到我点击的是哪一个section,但是会使得整个cell都能点击。如果不介意的话这个方法当然可以,下面来说一下只通过按钮来确定是哪一个section的方法。
首先,你的按钮必须要绑定你的事件,和storyBoard拖个线就行了。
然后一定要在storyBoard仔细观察你的button上面一共有几层才能到你的cell,也就是属一下上面有几个父类才到cell
如图所示,方框里的是button,上面到cell一共有三层。为什么要看有几层,我们来看一下按钮的代码
- (IBAction)enterShopButton:(UIButton *)sender {
UIView *v = [sender superview];//获取父类view
UIView *v1 = [v superview];
UITableViewCell *cell = (UITableViewCell *)[v1 superview];//获取cell
NSIndexPath *indexPathAll = [self.tableView indexPathForCell:cell];//获取cell对应的section
NSLog(@&indexPath:--------%@&,indexPathAll);
首先第一个“v”是获取“View”这一层,然后继续调用superview往上翻(不懂的对照上面的图来看)
“v1”是获取“Content View”这一层,
“cell”就获取到了对应的cell这一层。然后取出cell的路径
path = 2 - 0
代表当前cell所在的section,“0”代表当前cell里的row位置。
再通过“indexPathAll.section”就能获取当前的section了。
个人觉得非常好用,而且很容易理解。
本文永久地址:/3400.html本文出自
,转载时请注明出处及相应链接。
与本文相关的文章

我要回帖

更多关于 self.contentview 的文章

 

随机推荐