【UIKit】UITableView 自定义Cell2
2014-04-20 21:57
225 查看
【自定义Cell2】【Code】:全部通过代码添加
【1】:设置Cell
1):创建一个MsgCell类 继承UITableViewCell
.h中声明2个属性一个是用户头像,另外一个是发表的文字
@property (nonatomic,weak,readonly) UIImageView *iconView; @property (nonatomic,weak,readonly)UIButton *msgBtn;
.m中,创建出具体的cell内容
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 添加子控件 1,左边的头像 UIImageView *iconView=[[UIImageView alloc] init]; iconView.frame=CGRectMake(10, 10, 50, 50); [self addSubview:iconView]; [self.contentView addSubview:iconView]; _iconView=iconView; // 添加右边的对话按钮 UIButton *btn=[[UIButton alloc]init]; btn.frame=CGRectMake(100, 0, 100, 60); [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_normal.png"] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_focused.png"] forState: UIControlStateHighlighted]; [self.contentView addSubview:btn]; // 设置文字颜色 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _msgBtn=btn; } return self; }
创建一个ViewController:UITableViewController
#import "ViewController.h" #import "MsgCell.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 去除横线 self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; self.tableView.editing = YES; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; // 20个cell } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 用static修饰的局部变量,只会初始化一次 static NSString *ID=@"Cell"; // 2.拿到一个标识先去缓存池中查找对应的Cell MsgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; // 3. 如果缓存池中没有,才需要创建新的标识 if(cell==nil) { cell=[[MsgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.iconView.image=[UIImage imageNamed:@"0.png"]; [cell.msgBtn setTitle:@"哈哈哈" forState:UIControlStateNormal]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 每一行的高度自定义 return 70; } @end
相关文章推荐
- iPhone Wax:用Lua生成自然的UIKit程序
- iPhone应用开发之一:窗口,视图,视图控制器和UIKit基础
- iOS5 UIKit新特性---定制各种控件
- IOS UIKit框架类之视图动画和坐标系介绍!
- 学习iPhone UIKit 6
- NSAttributedString UIKit Additions 参考
- UIKit UIApplication
- OC学习之UIKit的类层次结构图
- IOS开发笔记--UIKit框架之 UIView
- 【UIKit.framework】UIView UIControl UIViewController 三者的区别
- IOS学习笔记---UIKit之UILable
- 09-UIKit(UICollectionViewController、UITabBarController)
- 高级UIKit-01(总结基础UIKit)
- ios7新特性1-UI变化、UIKit动态行为支持与Text Kit新接口
- 【UIKit】UIScrollView
- 【UIKit】UITableView 7 常用属性
- ios UIKit框架分析 第3天
- iOS.UIKit.04.UISwitch_UISegmentedControl
- cocos2d-x与UIKit混合编程实现半透明效果
- 【UIKit】UITableView.08 常用属性