您的位置:首页 > 产品设计 > UI/UE

自定义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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: