視圖需要我們自己去定義樣式時用到了TableViewCell組件,效果如下
?
?
首先創建View Based App工程,在.xib文件中拖入一個Table View,前面我們說到了,這里就不再重復,注意連接協議和兩個必須方法的實現。完成.h中代碼
?
- #import?<UIKit/UIKit.h> ??
- @interface?TableViewCellViewController?:?UIViewController??
- ????????????<UITableViewDelegate,UITableViewDataSource>{??
- ????????????????IBOutlet?UITableView?*tView;??
- }??
- @property?(nonatomic,retain)UITableView?*tView;??
- @end??
完成.m中代碼
?
- #import?"TableViewCellViewController.h" ??
- #import?"MyCell.h" ??
- @implementation?TableViewCellViewController??
- @synthesize?tView;??
- -?( void )viewDidLoad?{??
- ????[super?viewDidLoad];??
- }??
- -?( void )didReceiveMemoryWarning?{??
- ????[super?didReceiveMemoryWarning];??
- }??
- -?( void )dealloc?{??
- ????[tView?release];??
- ????[super?dealloc];??
- }??
- -(NSInteger)?tableView:(UITableView?*)tableView??
- ?numberOfRowsInSection:(NSInteger)section??
- {??
- ???? return ?9;??
- }??
- -?(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath??
- {??
- ???? static ?NSString?*CellIdentifier?=?@ "CustomCellIdentifier" ;??
- ????MyCell?*cell?=?(MyCell?*)[tableView?dequeueReusableCellWithIdentifier:CellIdentifier];??
- ???? if ?(cell?==?nil)?{??
- ????????NSArray?*array?=?[[NSBundle?mainBundle]?loadNibNamed:@ "mycell" ?owner:self?options:nil];??
- ????????cell?=?[array?objectAtIndex:0];??
- ????????[cell?setSelectionStyle:UITableViewCellSelectionStyleGray];??
- ????}??
- ????[[cell?lable]?setText:@ "31" ];??
- ????[[cell?lable1]?setText:@ "Raul" ];??
- ????[[cell?myImage]?setImage:[UIImage?imageNamed:@ "3316.jpg" ]];??
- ???? return ?cell;??
- }??
- -?(CGFloat)tableView:(UITableView?*)atableView?heightForRowAtIndexPath:(NSIndexPath?*)indexPath?????
- {?????????
- ???? return ?120;??
- }??
- @end??
若不需要cell則改為:?
static NSString *CellIdentifier2 = @"Cell";?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];?
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];?
cell.textLabel.text =@"CCCCCCCC";?
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];?
// cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sign_10x12.png"]];?
cell.selectionStyle = UITableViewCellSelectionStyleBlue;?
[cell.textLabel setTextColor:[UIColor colorWithRed:0/255.0 green:106/255.0 blue:166/255.0 alpha:1.0]];
return cell;?
下面我們新建一個類,注意subclass選擇UITableViewCell,名稱為MyCell,生成之后再創建相應的xib文件
?
?
雙擊MyCell.xib,將Table View Cell拖入主窗口中,并且刪除原主窗口中的View圖標
?
?
在.h文件中完成代碼
?
- #import?<UIKit/UIKit.h> ??
- @interface?MyCell?:?UITableViewCell?{??
- ????IBOutlet?UILabel?*lable;??
- ????IBOutlet?UILabel?*lable1;??
- ????IBOutlet?UIImageView?*myImage;??
- }??
- @property(nonatomic,retain)?UILabel?*lable;??
- @property(nonatomic,retain)?UILabel?*lable1;??
- @property?(nonatomic,retain)?UIImageView?*myImage;??
- @end??
在.m中完成代碼
?
- #import?"MyCell.h" ??
- @implementation?MyCell??
- @synthesize?lable,lable1,myImage;??
- -?(id)initWithStyle:(UITableViewCellStyle)style?reuseIdentifier:(NSString?*)reuseIdentifier?{??
- ???? if ?((self?=?[super?initWithStyle:style?reuseIdentifier:reuseIdentifier]))?{??
- ???????? //?Initialization?code ??
- ????}??
- ???? return ?self;??
- }??
- -?( void )setSelected:( BOOL )selected?animated:( BOOL )animated?{??
- ??
- ????[super?setSelected:selected?animated:animated];??
- ??
- ???? //?Configure?the?view?for?the?selected?state ??
- }??
- -?( void )dealloc?{??
- ????[myImage?release];??
- ????[lable?release];??
- ????[super?dealloc];??
- }??
- @end??
最后我們看一下MyCell.xib中的連接,按住Ctrl拖入將要顯示區域的文字和圖片找到相應的接口即可。
(注意:是myCell和文字和圖片連接,不是file’s owner和文字和圖片連接,我又犯這個錯誤了)
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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