1、使用代码
自定义tableView样式有两种方法,一种是用代码写cell的subView,另一种是导入nib文件(就是用Interface Builder设计),这篇笔记记录的是代码的方法.
1、新建一个Single View Application 项目,如前文,只选Use Automatic Reference Counting.
2、打开PDViewController.xib,拖进一个Table View,选中Table View,打开Connections inspector,拖动delegate和dataSource右边的小圆到File’s Owner.
3、新建文件,选Cocoa Touch—-Objective-C class,输入类名,我这是MyCell,Subclass of UItableViewCell.生成MyCell.h和MyCell.m两文件。
MyCell.h:
1、新建一个Single View Application 项目,如前文,只选Use Automatic Reference Counting.
2、打开PDViewController.xib,拖进一个Table View,选中Table View,打开Connections inspector,拖动delegate和dataSource右边的小圆到File’s Owner.
3、新建文件,选Cocoa Touch—-Objective-C class,输入类名,我这是MyCell,Subclass of UItableViewCell.生成MyCell.h和MyCell.m两文件。
MyCell.h:
- #import <UIKit/UIKit.h>
- @interface MyCell : UITableViewCell
- @property (copy,nonatomic) NSString *name;
- @property (copy,nonatomic) NSString *color;
- @end
MyCell.m:
- #import "MyCell.h"
- #define kNameTag 1
- #define kColorTag 2
- @implementation MyCell
- @synthesize name=_name,color=_color;
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- // Initialization code
- CGRect nameLableRect=CGRectMake(0, 5, 100, 15);
- //定义一个距形,位置0,5,宽100高15,就是绝对定位
- UILabel *nameLable=[[UILabel alloc] initWithFrame:nameLableRect];
- //在距形定义的位置实例化一个UILable对象
- nameLable.tag=kNameTag;
- [self.contentView addSubview:nameLable];
- CGRect colorLableRect=CGRectMake(0, 30, 100, 15);
- //定义一个距形,距离上面的nameLable 10
- UILabel *colorLable=[[UILabel alloc] initWithFrame:colorLableRect];
- //在距形定义的位置实例化一个UILable对象
- colorLable.tag=kColorTag;
- [self.contentView addSubview:colorLable];
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated
- {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- //自己写name和color的setter方法
- -(void)setName:(NSString *)name
- {
- if(![name isEqualToString:_name])
- {
- _name=[name copy];
- UILabel * nameLable=(UILabel *)[self.contentView viewWithTag:kNameTag];
- //通过viewWithTag方法得到UILable
- nameLable.text=_name;
- }
- }
- -(void)setColor:(NSString *)color
- {
- if(![color isEqualToString:_color])
- {
- _color=[color copy];
- UILabel * colorLable=(UILabel *)[self.contentView viewWithTag:kColorTag];
- colorLable.text=_color;
- }
- }
- @end
PDViewControllor.h:
- #import <UIKit/UIKit.h>
- @interface PDViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
- //简单起见,不再定义数据,每行数据都一样
- @end
PDViewController.m
- #import "PDViewController.h"
- #import "MyCell.h"
- @implementation PDViewController
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {