有時(shí)候我們需要自己定義UITableViewCell的風(fēng)格,其實(shí)就是向行中添加子視圖。添加子視圖的方法主要有兩種:使用代碼以及從.xib文件加載。當(dāng)然后一種方法比較直觀。
我們這次要自定義一個(gè)Cell,使得它像QQ好友列表的一行一樣:左邊是一張圖片,圖片的右邊是三行標(biāo)簽:
當(dāng)然,我們不會(huì)搞得這么復(fù)雜,只是有點(diǎn)意思就行。
1、運(yùn)行Xcode 4.2,新建一個(gè)Single View Application,名稱為Custom Cell:
2、將圖片資源導(dǎo)入到工程。 為此,我找了14張50×50的.png圖片,名稱依次是1、2、……、14,放在一個(gè)名為Images的文件夾中。將此文件夾拖到工程中,在彈出的窗口中選中Copy items into…
添加完成后,工程目錄如下:
3、創(chuàng)建一個(gè)UITableViewCell的子類 :選中Custom Cell目錄,依次選擇File — New — New File,在彈出的窗口,左邊選擇Cocoa Touch,右邊選擇Objective-C class:
之后選擇Next和Create,就建立了兩個(gè)文件:CustomCell.h和CustomCell.m。
4、創(chuàng)建CustomCell.xib :依次選擇File — New — New File,在彈出的窗口,左邊選擇User Interface,右邊選擇Empty:
單擊Next,選擇iPhone,再單擊Next,輸入名稱為CustomCell,選擇好位置:
單擊Create,這樣就創(chuàng)建了CustomCell.xib。
5、打開CustomCell.xib,拖一個(gè)Table View Cell控件到面板上:
選中新加的控件,打開Identity Inspector,選擇Class為CustomCell;然后打開Size Inspector,調(diào)整高度為60。
6、向新加的Table View Cell添加控件: 拖放一個(gè)ImageView控件到左邊,并設(shè)置大小為50×50。然后在ImageView右邊添加三個(gè)Label,設(shè)置標(biāo)簽字號(hào),最上邊的是14,其余兩個(gè)是12:
接下來向CustomCell.h添加Outlet映射,將ImageView與三個(gè)Label建立映射,名稱分別為imageView、nameLabel、decLabel以及l(fā)ocLable,分別表示頭像、昵稱、個(gè)性簽名,地點(diǎn)。
選中Table View Cell,打開Attribute Inspector,將Identifier設(shè)置為CustomCellIdentifier:
為了充分使用這些標(biāo)簽,還要自己創(chuàng)建一些數(shù)據(jù),存在plist文件中,后邊會(huì)做。
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; } }
這相當(dāng)于重寫了各個(gè)set函數(shù),從而當(dāng)執(zhí)行賦值操作時(shí),會(huì)執(zhí)行我們自己寫的函數(shù)。
好了,現(xiàn)在自己定義的Cell已經(jīng)可以使用了。
不過在此之前,我們先新建一個(gè)plist,用于存儲(chǔ)想要顯示的數(shù)據(jù)。建立plist文件的方法前面的文章有提到。我們建好一個(gè)friendsInfo.plist,往其中添加數(shù)據(jù)如下:
注意每個(gè)節(jié)點(diǎn)類型選擇。
9、打開ViewController.xib,拖一個(gè)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文件的數(shù)據(jù)和圖片 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、運(yùn)行:
?
來源:? http://my.oschina.net/plumsoft/blog/51723
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
