自定义 UITableViewCell 的布局
来源:互联网 发布:防范电信网络诈骗图片 编辑:程序博客网 时间:2024/05/17 12:53
转自http://my.oschina.net/lvlove/blog/93778
自定义TableViewCell 布局有很多种,个人建议简单的cell使用第一种,复杂的用第二种。除此之外还有很多种实现方法,但是从代码重用的角度来说不是很好,没有写的必要。
言归正传:
1) 程序默认带有一个 ImageView , 一个textLabel, 一个DetailLabel, 和一个accessoryView,如果不需要过多的其它控件,可以创建一个UITableViewCell的子类。在 -(void)layoutSubviews 方法中改变这些子视图的Frame即可,比如:
#import "MyCell.h"@implementation MyCell/*这里定义Frame*/-(void)layoutSubviews{ self.imageView.frame =CGRectMake(0.0f, 0.0f, 50.0f, 50.0f); self.textLabel.frame =CGRectMake(60.0f, 0.0f, 100.0f, 20.0f); self.detailTextLabel.frame =CGRectMake(60.0f, 25.0f, 150.0f, 20.0f); self.accessoryView.frame =CGRectMake(280.0f, 10.0f, 30.0f, 30.0f); }
2) 如果cell内容过于复杂,可以自己定义属性,比如:
#import <UIKit/UIKit.h>@interface MyCell : UITableViewCell@property (nonatomic,retain) UILabel *nameLabel;@property (nonatomic,retain) UILabel *sexLabel;@property (nonatomic,retain) UILabel *ageLabel;@end
#import "MyCell.h"@implementation MyCell@synthesize nameLabel,sexLabel,ageLabel;- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)]; sexLabel =[[UILabel alloc]initWithFrame:CGRectMake(60.0f, 0.0f, 100.0f, 20.0f)]; ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(60.0f, 25.0f, 150.0f, 20.0f)]; } return self;}- (void)dealloc{ self.nameLabel =nil; self.sexLabel =nil; self.ageLabel =nil; [super dealloc];}
如果使用nib来绘制cell,参考http://my.oschina.net/plumsoft/blog/51723
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; static BOOL nibsRegistered = NO; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier]; nibsRegistered = YES; } CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowData = [self.dataList objectAtIndex:row]; cell.name = [rowData objectForKey:@"name"]; cell.dec = [rowData objectForKey:@"dec"]; cell.loc = [rowData objectForKey:@"loc"]; cell.image = [imageList objectAtIndex:row]; return cell;}
- 自定义 UITableViewCell 的布局
- 关于自定义UITableViewCell布局点击Cell消失的问题
- 自定义UITableViewCell的内容
- 自定义UITableViewCell的内容
- 自定义UITableViewCell的背景
- 自定义的UITableViewCell
- 自定义UITableViewCell的样式
- UITableViewCell的自定义问题
- UITableViewCell的自定义
- UITableViewcell 样式的自定义
- AutoLayout——由切图引发的UITableViewCell自定义高度布局的失败!
- 使用xib自定义UIView和UITableViewCell以方便布局的方法
- 使用xib自定义UIView和UITableViewCell以方便布局的方法
- 使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- iOS 使用纯代码自定义UITableViewCell实现一个简单的微博界面布局
- 使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- Python建最小堆
- phpQuery占用过多内存的解决方法
- windows操作系统下:QT编译mysql驱动文件问题总结
- Ext Grid 报表锁定列+多表头的设计与实现(一)
- iOS中scrollview自动滚动的实现
- 自定义 UITableViewCell 的布局
- 《Python 第二章》列表和元组
- 订票助手
- bigbluebutton 安装日志
- Gridview 增加多个空行。
- macro definitions (#define, #undef)
- CF-14C - Four Segments(几何)
- 如何在 Web 浏览器中启用 Java?
- 五块美金+二小时,你要怎样赚到钱?