使用xib定義一個自定義Cell

自定義一個Cell,使得它像QQ好友列表的一行一樣:左邊是一張圖片,圖片的右邊是三行標籤:

1、運行Xcode ,新建一個Single View Application,名稱爲Custom Cell:

2、將圖片資源導入到工程。爲此,我找了14張50×50的.png圖片,名稱依次是1、2、……、14,放在一個名爲Images的文件夾中。將此文件夾拖到工程中,在彈出的窗口中選中Copy items into…

添加完成後,工程目錄如下:

3、創建一個UITableViewCell的子類:選中Custom Cell目錄,依次選擇File — New — New File,在彈出的窗口,左邊選擇Cocoa Touch,右邊選擇Objective-C class:

單擊Next,輸入類名CustomCell,Subclass of選擇UITableViewCell:

之後選擇Next和Create,就建立了兩個文件:CustomCell.h和CustomCell.m。

4、創建CustomCell.xib:依次選擇File — New — New File,在彈出的窗口,左邊選擇User Interface,右邊選擇Empty:

單擊Next,選擇iPhone,再單擊Next,輸入名稱爲CustomCell,選擇好位置:

單擊Create,這樣就創建了CustomCell.xib。

5、打開CustomCell.xib,拖一個Table View Cell控件到面板上:

選中新加的控件,打開Identity Inspector,選擇Class爲CustomCell;然後打開Size Inspector,調整高度爲60。

6、向新加的Table View Cell添加控件:拖放一個ImageView控件到左邊,並設置大小爲50×50。然後在ImageView右邊添加三個Label,設置標籤字號,最上邊的是14,其餘兩個是12:

接下來向CustomCell.h添加Outlet映射,將ImageView與三個Label建立映射,名稱分別爲imageView、nameLabel、decLabel以及locLable,分別表示頭像、暱稱、個性簽名,地點。

選中Table View Cell,打開Attribute Inspector,將Identifier設置爲CustomCellIdentifier:

爲了充分使用這些標籤,還要自己創建一些數據,存在plist文件中,後邊會做。

7、打開CustomCell.h,添加屬性:


@property (copy, nonatomic) UIImage *image;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *dec;
@property (copy, nonatomic) NSString *loc;

8、打開CustomCell.m,向其中添加代碼:

8.1 在@implementation下面添加代碼:


@synthesize image;
@synthesize name;
@synthesize dec;
@synthesize loc;

8.2 在@end之前添加代碼:


- (void)setImage:(UIImage *)img {
    if (![img isEqual:image]) {
        image = [img copy];
        self.imageView.image = image;
    }
}

-(void)setName:(NSString *)n {
    if (![n isEqualToString:name]) {
        name = [n copy];
        self.nameLabel.text = name;
    }
}

-(void)setDec:(NSString *)d {
    if (![d isEqualToString:dec]) {
        dec = [d copy];
        self.decLabel.text = dec;
    }
}

-(void)setLoc:(NSString *)l {
    if (![l isEqualToString:loc]) {
        loc = [l copy];
        self.locLabel.text = loc;
    }
}

這相當於重寫了各個set函數,從而當執行賦值操作時,會執行我們自己寫的函數。

接着新建一個plist,用於存儲想要顯示的數據。建立plist文件的方法前面的文章有提到。我們建好一個friendsInfo.plist,往其中添加數據如下:

注意每個節點類型選擇。

9、打開ViewController.xib,拖一個Table View到視圖上,並將Delegate和DataSource都指向File’ Owner,就像上一篇文章介紹的一樣。

10、打開ViewController.h,向其中添加代碼:


#import 
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *dataList;
@property (strong, nonatomic) NSArray *imageList;
@end

11、打開ViewController.m,添加代碼:

11.1 在首部添加:


#import "CustomCell.h"

11.2 在@implementation後面添加代碼:


@synthesize dataList;
@synthesize imageList;

11.3 在viewDidLoad方法中添加代碼:


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //加載plist文件的數據和圖片
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"friendsInfo" withExtension:@"plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    
    NSMutableArray *tmpDataArray = [[NSMutableArray alloc] init];
    NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[dictionary count]; i++) {
        NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1];
        NSDictionary *tmpDic = [dictionary objectForKey:key];
        [tmpDataArray addObject:tmpDic];
        
        NSString *imageUrl = [[NSString alloc] initWithFormat:@"%i.png", i+1];
        UIImage *image = [UIImage imageNamed:imageUrl];
        [tmpImageArray addObject:image];
    }
    self.dataList = [tmpDataArray copy];
    self.imageList = [tmpImageArray copy];
}

11.4 在ViewDidUnload方法中添加代碼:


self.dataList = nil;
self.imageList = nil;

11.5 在@end之前添加代碼:


#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    
    static BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
        nibsRegistered = YES;
    }
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
 
    
    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.dataList objectAtIndex:row];
    
    cell.name = [rowData objectForKey:@"name"];
    cell.dec = [rowData objectForKey:@"dec"];
    cell.loc = [rowData objectForKey:@"loc"];
    cell.image = [imageList objectAtIndex:row];
     
    return cell;
}

#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

- (NSIndexPath *)tableView:(UITableView *)tableView 
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    return nil;
}

12、運行:

發佈了26 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章