PureCode--iOS--自定义UITableViewCell(含疑问)
纯代码编写的简单自定义UITableViewCell:
1.像处理普通视图一样处理Cell:
clsTableViewCell.h:
1 #import <UIKit/UIKit.h> 2 3 @interface clsTableViewCell : UITableViewCell 4 @property (nonatomic,strong) UILabel *label; 5 @property (nonatomic,strong) UIImageView *img; 6 @end
clsTableViewCell.m:
1 #import "clsTableViewCell.h" 2 3 @implementation clsTableViewCell 4 @synthesize img; 5 @synthesize label; 6 7 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 8 { 9 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 10 if (self) { 11 // Initialization code 12 } 13 return self; 14 } 15 16 - (id)init 17 { 18 self = [super init]; 19 if(self) 20 { 21 [self defaultSetting]; 22 } 23 return self; 24 } 25 26 - (void)awakeFromNib 27 { 28 // Initialization code 29 } 30 31 - (void)setSelected:(BOOL)selected animated:(BOOL)animated 32 { 33 [super setSelected:selected animated:animated]; 34 35 // Configure the view for the selected state 36 } 37 - (void)addSubViews 38 { 39 img = [[UIImageView alloc] init]; 40 label = [[UILabel alloc] init]; 41 42 [self.contentView addSubview:img]; 43 [self.contentView addSubview:label]; 44 } 45 - (void)setSubViews 46 { 47 label.text = @"默认标题"; 48 label.backgroundColor = [UIColor clearColor]; 49 [img setImage:[UIImage imageNamed:@"img"]]; 50 } 51 - (void)layoutSubviews 52 { 53 img.frame = CGRectMake([img image].size.width + 10, 30, [img image].size.width, [img image].size.height); 54 label.frame = CGRectMake(10, 30, 100, 30); 55 self.contentView.frame = CGRectMake(0, 0, 320, 80); 56 } 57 - (void)defaultSetting 58 { 59 [self addSubViews]; 60 [self setSubViews]; 61 }
2.视图控制器使用这个Cell:
普通UIViewController的clsMainVC.h:
1 #import <UIKit/UIKit.h> 2 3 @interface clsMainVC : UIViewController<UITableViewDelegate,UITableViewDataSource> 4 { 5 UITableView *_tableView; 6 } 7 @end
clsMainVC.m:
1 #import "clsMainVC.h" 2 #import "clsTableViewCell.h" 3 4 @interface clsMainVC () 5 { 6 NSArray *data; 7 } 8 @end 9 10 @implementation clsMainVC 11 12 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 13 { 14 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 15 if (self) { 16 // Custom initialization 17 } 18 return self; 19 } 20 21 - (void)viewDidLoad 22 { 23 [super viewDidLoad]; 24 data = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil]; 25 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]; 26 [self.view addSubview:_tableView]; 27 _tableView.delegate = self; 28 _tableView.dataSource = self; 29 30 // Do any additional setup after loading the view. 31 } 32 33 - (void)didReceiveMemoryWarning 34 { 35 [super didReceiveMemoryWarning]; 36 // Dispose of any resources that can be recreated. 37 } 38 #pragma mark 39 #pragma mark tableview delegate 40 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 41 { 42 return 80; 43 } 44 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 45 { 46 return data.count; 47 } 48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 49 { 50 51 return 1; 52 } 53 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 54 { 55 NSLog(@"select %d",indexPath.row + 1); 56 } 57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 58 { 59 // 比较奇怪的是使用- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier 方法 就无法正常显示自己定义的Cell 60 // [tableView registerClass:[clsTableViewCell class] forCellReuseIdentifier:@"cell"]; 61 clsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 62 63 if(nil == cell) 64 { 65 cell = [[clsTableViewCell alloc] init]; 66 } 67 cell.label.text = [data objectAtIndex:indexPath.row]; 68 if((indexPath.row + 1)% 2 == 0) 69 { 70 cell.contentView.backgroundColor = [UIColor greenColor]; 71 } 72 return cell; 73 } 74 75 @end
3.显示效果:
4.自定义实现倒不难,只是不懂
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
这个方法的作用究竟是什么?为什么会出现无法正常显示自定义Cell的问题?
Developer Library的意思是使用这个方法注册自己写的Cell类吧?
registerClass:forCellReuseIdentifier:
Registers a class for use in creating new table cells.
Parameters
- cellClass
-
The class of a cell that you want to use in the table.
- identifier
-
The reuse identifier for the cell. This parameter must not be
nil
and must not be an empty string.
Discussion
Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier:
method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.
If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil
for cellClass if you want to unregister the class from the specified reuse identifier.
Availability
- Available in iOS 6.0 and later.
Declared In
UITableView.h