自定义UITableViewCell的内容
2010-02-10 15:38
253 查看
图片:
大家根据书上写的都是最基本的表格,那么看到有些应用程序中的表格单元既有文本,又能直接录入数据,有的还带有图片之类的内容,那么是不是实现起来很难呢,其实实现起来特别简单,有两种方法,一种是采用代码的方式实现,另一种是采用nib文件的方式实现.
但
是原理都是一样的都是采用自定义UITableViewCell的方式来实现的,也就是说我们想实现一个表格,表格中的单元格是由标签和文本录入框来组成
的则需要将这两种控件加入到cell中即可实现.如下的为两个函数,第一个函数实现了自定义cell的功能,第二个为标准的表格控制函数,大家可以直接在
项目中使用,该代码只是简单的实现抛砖引玉的功能,有复杂的可以根据需要进行编写.
//自定义TableViewCell子视图
//函数一 自定义cell的功能
-(void)makeSubCell:(UITableViewCell *)aCell withTitle:(NSString *)title
value:(NSString *)value
{
CGRect tRect = CGRectMake(20,5, 320, 40);
id lbl = [[UILabel alloc] initWithFrame:tRect]; //此处使用id定义任何控件对象
[lbl setText:title];
[lbl setBackgroundColor:[UIColor clearColor]];
CGRect tEdtRect = CGRectMake(100,15, 320, 40);
id edtPassword = [[UITextField alloc] initWithFrame:tEdtRect];
[edtPassword setText:value];
[edtPassword setBackgroundColor:[UIColor clearColor]];
[edtPassword setKeyboardType:UIKeyboardTypeNumberPad];
[edtPassword setSecureTextEntry:YES];
[aCell addSubview:lbl];
[aCell addSubview:edtPassword];
//release someone
[lbl release];
[edtPassword release];
}
//函数二 表格控制函数
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"Simple";
UITableViewCell
*cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier] autorelease];
NSUInteger row = [indexPath row];
switch (row) {
case 0:
[self makeSubCell:cell withTitle:@"当前密码:" value:@"password"];
break;
case 1:
[self makeSubCell:cell withTitle:@"新 密 码:" value:@"new password"];
break;
case 2:
[self makeSubCell:cell withTitle:@"密码确认:" value:@"confirm password"];
break;
}
if (cell == nil)
{
NSLog(@"cell = nil");
}else
{
NSLog(@"cell <> nil");
}
return cell;
}
大家根据书上写的都是最基本的表格,那么看到有些应用程序中的表格单元既有文本,又能直接录入数据,有的还带有图片之类的内容,那么是不是实现起来很难呢,其实实现起来特别简单,有两种方法,一种是采用代码的方式实现,另一种是采用nib文件的方式实现.
但
是原理都是一样的都是采用自定义UITableViewCell的方式来实现的,也就是说我们想实现一个表格,表格中的单元格是由标签和文本录入框来组成
的则需要将这两种控件加入到cell中即可实现.如下的为两个函数,第一个函数实现了自定义cell的功能,第二个为标准的表格控制函数,大家可以直接在
项目中使用,该代码只是简单的实现抛砖引玉的功能,有复杂的可以根据需要进行编写.
//自定义TableViewCell子视图
//函数一 自定义cell的功能
-(void)makeSubCell:(UITableViewCell *)aCell withTitle:(NSString *)title
value:(NSString *)value
{
CGRect tRect = CGRectMake(20,5, 320, 40);
id lbl = [[UILabel alloc] initWithFrame:tRect]; //此处使用id定义任何控件对象
[lbl setText:title];
[lbl setBackgroundColor:[UIColor clearColor]];
CGRect tEdtRect = CGRectMake(100,15, 320, 40);
id edtPassword = [[UITextField alloc] initWithFrame:tEdtRect];
[edtPassword setText:value];
[edtPassword setBackgroundColor:[UIColor clearColor]];
[edtPassword setKeyboardType:UIKeyboardTypeNumberPad];
[edtPassword setSecureTextEntry:YES];
[aCell addSubview:lbl];
[aCell addSubview:edtPassword];
//release someone
[lbl release];
[edtPassword release];
}
//函数二 表格控制函数
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"Simple";
UITableViewCell
*cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier] autorelease];
NSUInteger row = [indexPath row];
switch (row) {
case 0:
[self makeSubCell:cell withTitle:@"当前密码:" value:@"password"];
break;
case 1:
[self makeSubCell:cell withTitle:@"新 密 码:" value:@"new password"];
break;
case 2:
[self makeSubCell:cell withTitle:@"密码确认:" value:@"confirm password"];
break;
}
if (cell == nil)
{
NSLog(@"cell = nil");
}else
{
NSLog(@"cell <> nil");
}
return cell;
}
相关文章推荐
- 自定义UItableviewCell内容
- 自定义UITableViewCell的内容
- 自定义UITableViewCell的内容
- iOS开发:自定义UITableViewCell(转)
- [ios]纯代码实现UITableViewCell的自定义扩展
- Android EditText得到和失去焦点时,自定义处理内容
- iOS 自定义UITableViewCell
- jquery创建一个新的节点对象(自定义结构/内容)的好方法
- C++内容总结--自定义数据类型
- iOS开发 - 友盟分享(自定义分享)如何让新浪分享显示蓝色文字,其他平台内容不显示链接
- Android 自定义TextView实现文本内容自动调整字体大小
- dede 自定义内容模型 不能添加文章
- iOS UI-团购案例(通过xib文件自定义UITableViewCell)
- weblogic的Serverlog内容自定义
- dedecms网站文章内容按自定义排序的方法
- jQuery Easyui加载表格出错时在表格中间显示自定义的提示内容
- iOS UItableviewCell 自定义选中颜色及默认选中首行
- 可任意自定义的UITableViewCell
- 25.ContentProvider自定义内容提供者与ContentResolver内容访问者
- 自定义提示框,可以改变左右菜单和提示的内容