关于自定义uitableviewcell

本文介绍如何通过代码自定义UITableViewCell,包括创建继承自UITableViewCell的类、初始化方法中的UI设置及在TableViewController中的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两种方法,一种是通过代码来实现,还有一种是通过xib来实现,好不容易找到个用代码来实现的:

转载自:http://blog.youkuaiyun.com/iorchid/article/details/6552387  (其实特简单,就是继承一个uitableviewcell类,然后在 reuseIdentifier 这个方法里实现就行,我是没有用到那个什么select方法)

这个是用xib来实现的,有空可以看看,网上有另一篇,一搜一大把,这儿就不罗列了:http://blog.youkuaiyun.com/dongstone/article/details/7438254

  1. 新建一个继承自UITableViewCell的类,CustomTableViewCell  
  2. 在函数:- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier中,填写需要自定义的UI  
  3. 比如:  
  4. CustomTableViewCell.m  
  5.  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  6. {  
  7.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  8.     if (self)   
  9.     {  
  10.         // Initialization code  
  11.         _leftImageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];  
  12.           
  13.         _rightImageView = [[UIImageView alloc] initWithFrame: CGRectMake(170, 0, 100, 100)];  
  14.           
  15.         self.backgroundColor = [UIColor redColor];  
  16.           
  17.         //[self.contentView addSubview: _leftImageView];  
  18.         [self.contentView addSubview: _rightImageView];  
  19.           
  20.         label = [[UILabel alloc] initWithFrame: CGRectMake( 0, 0, 60, 40)];  
  21.         [self.contentView addSubview: label];  
  22.           
  23.         textField = [[UITextField alloc] initWithFrame: CGRectMake(60, 0, 100, 40)];  
  24.         textField.secureTextEntry = YES;  
  25.         [self.contentView addSubview: textField];  
  26.     }  
  27.     return self;  
  28. }  
  29. -(void) setImage2:(UIImage *)image  
  30. {  
  31.     //_leftImageView.image = image;  
  32.     _rightImageView.image = image;      
  33.       
  34. }  
  35. - (void) setText:(NSString *)text  
  36. {  
  37.     label.text = text;  
  38. }  
  39. 在TableViewController的实现文件如下:  
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  41. {  
  42.     static NSString *CellIdentifier = @"Cell";  
  43.       
  44.     CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  45.     if (cell == nil)   
  46.     {  
  47.         cell = (CustomTableViewCell *)[[[CustomTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  48.     }  
  49.       
  50.     // Configure the cell...  
  51.       
  52. //    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://www.baidu.com/img/baidu_sylogo1.gif"]];  
  53. //    UIImage *image = [UIImage imageWithData: data];  
  54. //      
  55. //    [cell setImage2: image];  
  56.       
  57.     //cell.textLabel.text = [array objectAtIndex: indexPath.row];  
  58.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  59.       
  60.     cell.label.text = @"密码: ";  
  61.       
  62.     return cell;  
  63. }  
  64.   
  65. 自定义基本完成。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值