自定义UITableViewCell的内容

  大家根据书上写的都是最基本的表格,那么看到有些应用程序中的表格单元既有文本,又能直接录入数据,有的还带有图片之类的内容,那么是不是实现起来很难呢,其实实现起来特别简单,有两种方法,一种是采用代码的方式实现,另一种是采用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;

}


原帖地址 http://www.cocoachina.com/bbs/read.php?tid-6978-fpage-44.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值