0%

IOS开发 自定义TableView样式(使用Interface Builder)

前面说到用代码写TableViewCell的样式(),但代码必竟没有Interface Builder来得方便,今天就介绍使用Interface Builder编辑NIB文件来实现自定义样式。 前面两步与用代码相同,即: 1、新建一个Single View Application 项目,如前文,只选Use Automatic Reference Counting. 2、打开PDViewController.xib,拖进一个Table View,选中Table View,打开Connections inspector,拖动delegate和dataSource右边的小圆到File’s Owner. 第3步,也是写一个继承自UItableViewCell的类(我这取名NIBCell),但内容稍有不同。 NIBCell.h:

#import 

@interface NIBCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *thumbView;
@property (strong, nonatomic) IBOutlet UILabel *titleView;
//两个outlet与NIB文件中的控件连接
@property (copy,nonatomic)  NSString * title;
@property (strong,nonatomic) UIImage * thumb;
//两个属性分别用于设置上面两个outlet的属性
@end

NIBCell.m

#import "NIBCell.h"

@implementation NIBCell
@synthesize thumbView = _thumbView;
@synthesize titleView = _titleView;
@synthesize title=_title,thumb=_thumb;

/*
 因为样式是在nib文件中定义的,所以不再需要
 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 方法
 */

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)setTitle:(NSString *)title
{//覆盖默认生成的setter,但有outlet,不再需要content的viewWithTag方法来取得View
    if(![self.title isEqualToString:title])
    {
        _title=title;
        _titleView.text=_title;
    }

}
-(void)setThumb:(UIImage *)thumb
{
    if(![self.thumb isEqual:thumb])
    {
        _thumb=thumb;
        [_thumbView setImage:_thumb];
    }
}

@end

第四步,新建文件,IOS–User Interface–Empty,我这取名TableViewCell.xib 5、单击TableViewCell.xib以便用Interface Builder打开编辑(这时是空的),从控件库里找到Table View Cell,拖进Interface Builder,再拖进一个UILable,和一个UIImage View用于显示一段文字和图片(与前面NIBCell类中的定义相符) 6、选中整个Table View Cell,打开Identity Inspector,把Class改为NIBCell。 7、打开Connections Inspector,找到两个Outlet,thumbView和titleView(我们在NIBCell中定义的),将其右边的小圆圈拖动到Interface Builder里相应的UIImage View和 UILable控件上,建立连接。 8、PDViewController代码: PDViewController.h

#import 

@interface PDViewController : UIViewController
//与前面相同,为了简便,不定义用于存储每行信息的数组
@end

PDViewController.m

#import "PDViewController.h"
#import "NIBCell.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
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//返回行数
    return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * tableIdentifier=@"CellFromNib";
    static BOOL nibsRegistered=NO;
    if(!nibsRegistered)
    {//第一次运行时注册nib文件,文件名不需要扩展名
        UINib *nib=[UINib nibWithNibName:@"TableViewCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:tableIdentifier];
        nibsRegistered=YES;
    }
    NIBCell *cell=[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    NSUInteger rowNumber=[indexPath row];
    NSString *title=[[NSString alloc] initWithFormat:@"%d",rowNumber];
    cell.title=title;
    UIImage *image=[UIImage imageNamed:@"57"];
    cell.thumb=image;
    //显示行号,和一张固定的图片,图片同样不需要扩展名
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //返回行高
    return 100;
}

@end