UITableView使用详解
来源:互联网 发布:网络小贷牌照用途 编辑:程序博客网 时间:2024/05/18 14:44
IOS 学习:UITableView使用详解2 自定义的单元格
1.建立CustomCell类
使用常见的建立类的方法,把被继承的类设置为UITableViewCell。
建立了类之后再次点击新建文件,选择CocoaTouch 下的empty,建立一个nib文件,
讲一个表格单元格Table View Cell控件拖进nib视图,添加,image view 和三个textLabel进来。如下图所示:
将该单元格的class设置为CustomCell。
属性当中的Identifier设置为CustomCellIdentifier待会要用到这个属性。
在CustomCell.m文件里面添加输出口,如下图所示:
点击nib文件,建立好连线。
在该头文件当中添加属性:
想要将这些属性用来改变便签和图片视图,则必须必须使用自定义set方法,当使用点运算符进行赋值时,自动调用set方法,因此在CustomCell.m中添加以下set方法:
- ( void )setImage:( UIImage *)img {
if (![img isEqual : image ]) {
image = [img copy ];
self . imageView . image = image ;
}
}
-( void )setName:( NSString *)n {
if (![n isEqualToString : name ]) {
name = [n copy ];
self . nameLabel . text = name ;
}
}
-( void )setDec:( NSString *)d {
if (![d isEqualToString : dec ]) {
dec = [d copy ];
self . decLabel . text = dec ;
}
}
-( void )setLoc:( NSString *)l {
if (![l isEqualToString : loc ]) {
loc = [l copy ];
self . locLabel . text = loc ;
}
}
这样CustomCell类就配置完毕了。
2.使用CustomCell
在ViewController当中添加,import”CustomCell.h”,添加好数据源和UITableView的委托方法。这和一般的建表方法差不多,有区别的方法如下:
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier" ;//
static BOOL nibsRegistered = NO ;//添加nib文件注册
if (!nibsRegistered) {
UINib *nib = [ UINib nibWithNibName : @"CustomCell" bundle : nil ];
[tableView registerNib :nib forCellReuseIdentifier :CustomCellIdentifier];//注册nib文件
nibsRegistered = YES ;
}
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];//单元格重用
if (cell == nil ) { //如果没有可重用的单元格,则新建一个单元格。
cell = [[ CustomCell alloc ]
initWithStyle : UITableViewCellStyleDefault
reuseIdentifier :CustomCellIdentifier];
}
NSUInteger row = [indexPath row ];
NSDictionary *rowData = [ self . dataList objectAtIndex :row];
cell. name = [rowData objectForKey : @"name" ];
cell. dec = [rowData objectForKey : @"dec" ];//使用点运算符赋值,自动调用set方法,实现标签和图片的更改。
cell. loc = [rowData objectForKey : @"loc" ];
cell. image = [ imageList objectAtIndex :row];
return cell;
}
如此这般,自定义的单元格就建立完毕。
- UITableView使用详解
- UITableView使用详解
- UITableView使用详解
- UITableView的使用详解
- UITableView 使用详解 教程
- UITableView使用详解
- (转)UITableView使用详解
- UITableView使用详解
- iOS UITableView使用详解
- UITableView使用详解
- UITableView的使用详解
- UITableView使用详解
- UITableView使用详解
- UITableView的使用与详解
- iOS-UITableView使用详解深入浅出
- UITableView(表视图)使用详解
- UITableView使用详解 相当详细
- iOS 之 UITableView 使用详解
- Twitter Ostrich
- activiti-explorer 流程中文乱码问题
- ARM中关于绝对寻址相对寻址LDR,ADR的理解
- 高仿iReader书架效果
- perl教程-子程序
- UITableView使用详解
- POJ_2001_Shortest Prefixes(Trie tree)
- 【PB】事件的触发时机及先后顺序
- 如何判断IOS设备类型
- redis 安装 启动
- Servlet 线程安全问题
- mysql oracle时间操作举例
- maven(6)--私有仓库
- Android Activity和Intent机制