iOS開發15:自定義UITableViewCell

上篇文章介紹瞭如何用UITableView顯示錶格,並講了幾種UITableViewCell的風格。不過有時候咱們須要本身定義 UITableViewCell的風格,其實就是向行中添加子視圖。添加子視圖的方法主要有兩種:使用代碼以及從.xib文件加載。固然後一種方法比較直 觀。函數

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

固然,咱們不會搞得這麼複雜,只是有點意思就行。atom

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

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

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

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

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

以後選擇Next和Create,就創建了兩個文件:CustomCell.h和CustomCell.m。圖片

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

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

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

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

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

六、向新加的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文件中,後邊會作。

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

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

八、打開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函數,從而當執行賦值操做時,會執行咱們本身寫的函數。

好了,如今本身定義的Cell已經可使用了。

不過在此以前,咱們先新建一個plist,用於存儲想要顯示的數據。創建plist文件的方法前面的文章有提到。咱們建好一個friendsInfo.plist,往其中添加數據以下:

注意每一個節點類型選擇。

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

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

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSArray *dataList;
@property (strong, nonatomic) NSArray *imageList;
@end

十一、打開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;
}

十二、運行:

Yaodao 發表於12-09-14 16:37
相關文章
相關標籤/搜索