ios-表视图-demo-自定义cell和心得
2014-04-28 21:39
381 查看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellindentifier=@"cell"; if (self.celltype==KTableViewCellContenview) {//第一种自定义cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier]; UILabel*lable= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];//在这里创建性能更高,最少的创建实例滑动的时候 lable.tag=101; [cell.contentView addSubview:lable]; } UILabel * lable=(UILabel *) [cell.contentView viewWithTag:101]; lable.text=_dataarray[indexPath.row]; lable.font=[UIFont fontWithName:_dataarray[indexPath.row] size:16]; return cell; }else if(self.celltype==KTableViewCellCustom){ MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier]; if (cell==nil) { cell=[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier]; NSLog(@"排版前%d",indexPath.row); } return cell; }else if(self.celltype==KTableViewCellNib){//从nib来 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier]; if (cell==nil) { NSArray*nibs=[[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:self options:nil]; cell=[nibs objectAtIndex:0]; } UILabel * lable=(UILabel *) [cell.contentView viewWithTag:102]; lable.text=_dataarray[indexPath.row]; lable.font=[UIFont fontWithName:_dataarray[indexPath.row] size:16]; return cell; }else{ return nil; } }
首先是对我们为什么自定义系统的cell尺寸没用,因为ios会在我们创建了一屏幕的cell的时候会调用 -(void)layoutSubviews{ [super layoutSubviews]; self.textlabel.frame=CGRectMake(0, 0, 300, 44); } 对创建了的cell进行重新排版,当然这里的重新排版只会对contentview以外的进行排版,以后滑动屏幕的时候,可能是新建立的cell或者重用的都会进行排版, 想要对contentview进行手动排版的话可以手动来覆写 -(void)layoutSubviews{ [super layoutSubviews]; //在这里写,比如下面这个文件这样 }
// // MyCell.m // CustomCellDemo // // Created by liyang on 14-4-28. // Copyright (c) 2014年 liyang. All rights reserved. // #import "MyCell.h" @implementation MyCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { _label=[[UILabel alloc]initWithFrame:CGRectZero]; _label.backgroundColor=[UIColor orangeColor]; [self.contentView addSubview:_label]; } return self; } -(void)layoutSubviews{ [super layoutSubviews]; _label.frame=CGRectMake(0, 0, 300, 44); NSLog(@"排版后%d"); } @end
相关文章推荐
- iOS UITableView表视图(3)自定义cell
- IOS学习之——Cell自定义内容视图&自定义辅助视图
- iOS开发之高级视图—— UITableView(四)自定义Cell
- 【iOS开发-65】QQ聊天界面案例:自定义cell、图片拉伸处理、NSNotification通知、键盘与视图移动以及输入框左边缩进处理
- iOS在自定义视图(常用于自定义cell)中获取导航控制器上面的当前的控制器对象
- IOS之UITableView cell自定义视图中插入Tableview
- iOS开发-------模拟团购网站(UITableView 表格视图)以及自定义cell
- ios-表视图-demo7-cell的编辑
- IOS开发中UITableView(表视图)的滚动优化及自定义Cell
- iOS 设计中实现Cell自定义滑块操作
- iOS雨笙 lable自适应高度(多用于tableview的自定义cell中)
- iOS 自定义cell改变其中子控件frame值
- [IOS]UITableView重用(使用多个自定义Cell)
- iOS通过代码自定义cell(cell的高度不同)的步骤
- 我的iOS学习历程 - 自定义视图
- IOS封装自定义Cell方法
- IOS 使用storyboard,自定义TableViewCell的坑,dequeueReusableCellWithIdentifier 报错
- iOS自定义cell导致数据加载混乱
- 源码推荐(9.06):collectionView集合视图展开二级视图,可自定义的 iOS 价格日历
- IOS学习之——自定义的Cell (代码)