欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

UITableView 系列五 :自定義UITableViewCell

系統 2508 0

有時候我們需要自己定義UITableViewCell的風格,其實就是向行中添加子視圖。添加子視圖的方法主要有兩種:使用代碼以及從.xib文件加載。當然后一種方法比較直觀。

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

當然,我們不會搞得這么復雜,只是有點意思就行。

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

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

UITableView 系列五 :自定義UITableViewCell (實例)

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

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

UITableView 系列五 :自定義UITableViewCell (實例)

為了充分使用這些標簽,還要自己創建一些數據,存在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函數,從而當執行賦值操作時,會執行我們自己寫的函數。

好了,現在自己定義的Cell已經可以使用了。

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

UITableView 系列五 :自定義UITableViewCell (實例)

注意每個節點類型選擇。

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

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

    #import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@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、運行:

UITableView 系列五 :自定義UITableViewCell (實例)

?

來源:? http://my.oschina.net/plumsoft/blog/51723

?

?

UITableView 系列五 :自定義UITableViewCell (實例)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费视频一区二区 | 久久久精品视频免费看 | 日韩电影一区二区三区 | 色免费视频 | 毛片免费观看 | 六月激情综合 | 麻豆国产一区二区三区四区 | 99热久久国产综合精品久久国产 | 日韩欧美中文字幕在线观看 | 国产成人综合精品 | 亚洲国产一区在线 | 好看的91视频 | 国产福利不卡一区二区三区 | 亚洲欧美另类色妞网站 | 性欧美一级 | 中文字幕亚洲一区 | 开心深爱激情 | 欧美一区二区三区gg高清影视 | 成人资源在线观看 | 久久免费视频在线 | 精品三级国产精品经典三 | 国产成人小视频在线观看 | 欧美一区二区三区四区视频 | 日韩特级毛片 | 99综合网 | 日韩一级片在线观看 | 久在线| 免费一级欧美片片线观看 | 国产精品免费一区二区三区 | 在线国产一区二区 | 在线色网站 | 欧美白人黑人xxxx猛交 | 色老师| 九九亚洲视频 | 91麻豆精品一二三区在线 | 亚洲综合电影 | 逼逼网 | 国产v欧美v日本v精品 | 婷婷开心六月久久综合丁香 | 久草视频在线首页 | 深夜做爰性大片中文 |