ios 开发自定义cell
2015-11-05 18:49
429 查看
ios 开发自定义cell
1.拦截frame的设置,修改fram
3.背景
4.cell的最后一行不要分割线
1.拦截frame的设置,修改fram
/** * 拦截frame的设置 */ - (void)setFrame:(CGRect)frame { if (!iOS7) { CGFloat padding = 10; frame.size.width += padding * 2; frame.origin.x = -padding; } [super setFrame:frame]; }2. 分割线
/** * 初始化分割线 */ - (void)setupDivider { UIView *divider = [[UIView alloc] init]; divider.backgroundColor = [UIColor blackColor]; divider.alpha = 0.2; [self.contentView addSubview:divider]; self.divider = divider; }
/** * 设置子控件的frame */ - (void)layoutSubviews { [super layoutSubviews]; if (iOS7) return; // 设置分割线的frame CGFloat dividerH = 1; CGFloat dividerW = [UIScreen mainScreen].bounds.size.width; CGFloat dividerX = 0; CGFloat dividerY = self.contentView.frame.size.height - dividerH; self.divider.frame = CGRectMake(dividerX, dividerY, dividerW, dividerH); }
3.背景
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // 初始化操作 // 1.初始化背景 [self setupBg]; // 2.初始化子控件 [self setupSubviews]; // 3.初始化分割线 if (!iOS7) { [self setupDivider]; } } return self; } /** * 初始化子控件 */ - (void)setupSubviews { self.textLabel.backgroundColor = [UIColor clearColor]; self.detailTextLabel.backgroundColor = [UIColor clearColor]; } /** * 初始化背景 */ - (void)setupBg { // 设置普通背景 UIView *bg = [[UIView alloc] init]; bg.backgroundColor = [UIColor whiteColor]; self.backgroundView = bg; // 设置选中时的背景 UIView *selectedBg = [[UIView alloc] init]; selectedBg.backgroundColor = IWColor(237, 233, 218); self.selectedBackgroundView = selectedBg; }
4.cell的最后一行不要分割线
//1.在cell.m中添加属性 @property (nonatomic, assign, getter = isLastRowInSection) BOOL lastRowInSection; //2.在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)方法中添加下面一行代码: cell.lastRowInSection = (group.items.count - 1 == indexPath.row); //3.在cell.m中重写set方法 - (void)setLastRowInSection:(BOOL)lastRowInSection { _lastRowInSection = lastRowInSection; self.divider.hidden = lastRowInSection; }
相关文章推荐
- 使用Instruments解决EXC_BAD_ACCESS
- iOS JSONModel使用详解
- iOS开发系列--通知与消息机制
- iOS导航栏不能显示纯色的解决方法
- iOS单例
- ios开发-宏区分TARGET_OS_IPHONE
- iOS的全局session
- iOS使用帧动画
- 更准确的获取iOS设备型号
- iOS开发中打开其他应用
- iOS MD5加密算法
- iOS中的各种手势
- iOS TableView实现QQ好友列表(三)
- iOS TableView实现QQ好友列表(二)
- iOS TableView实现QQ好友列表(一)
- iOS常用CGRect的交错,边缘,中心的检测
- iOS常用CGRect的交错,边缘,中心的检测
- ios 一个正则表达式测试(只可输入中文、字母和数字)
- IOS中用正则表达式判断输入的内容为8-16位且同时包含数字和字母
- iOS的单例模式