步驟:
創(chuàng)新互聯(lián)自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元云和做網(wǎng)站,已為上家服務(wù),為云和各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108
1.創(chuàng)建自定義的CellView.xib,操作New File->User Interface->View->命名cellView
2.往上面拖放一個(gè)UITableViewCell,然后向其中拖放添加UILabel,UITextField,UIButton,如下圖:
3.創(chuàng)建一個(gè)類(lèi)Cell,操作New File->Cocoa Touch->Objective-C class->Class:Cell,Subclass of:UITableViewCell
4.將cellView.xib中的Cell的class設(shè)置為Cell類(lèi),然后可以將label關(guān)聯(lián)到Cell.h中,也可以設(shè)置Label的Tag屬性為1,后面有兩種方法調(diào)用xib文件,一種是通過(guò)tag,其次就是通過(guò)cell。注意:xib文件中的File's Owner只能關(guān)聯(lián)到viewController,這里自定義的cell.xib沒(méi)有可以關(guān)聯(lián)的contrller文件,所以不需要寫(xiě),只需要把xib中的cell控件關(guān)聯(lián)到cell類(lèi)就可以了
5.ViewController.h:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDataSource> @property (retain, nonatomic) IBOutlet UITableView *tableView; @property(retain,nonatomic)NSArray *arr; @end6.ViewController.m:
#import "ViewController.h" #import "Cell.h" static NSString *strIdentifier = @"strIdentifier"; @interface ViewController () @end @implementation ViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; //獲得包含tableViewcell的nib文件 UINib *nib = [UINib nibWithNibName:@"cellView" bundle:nil]; //用這個(gè)nib文件注冊(cè)成tableView中表示為strIdentifier的cell [self.tableView registerNib:nib forCellReuseIdentifier:strIdentifier]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.arr count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //詢(xún)問(wèn)是否有未使用的,表示為strIdentifier的cell,如果沒(méi)有,并且tableview注冊(cè)過(guò),系統(tǒng)會(huì)建一個(gè)新的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier]; //用這個(gè)自定義的cell可以用綁定的方式修改內(nèi)容,要將自定義的cell.xib的class修改成Cell類(lèi) ((Cell *)cell).label.text = [self.arr objectAtIndex:[indexPath row]]; //也可以用tag的方式查找到需要修改的view //((UILabel *)[cell viewWithTag:1]).text = [self.arr objectAtIndex:[indexPath row]]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_tableView release]; [self.arr release]; [super dealloc]; } @end 運(yùn)行結(jié)果圖:
分享標(biāo)題:[IOS]用自定義的cell來(lái)創(chuàng)建UITableView
本文鏈接:http://chinadenli.net/article46/ipsceg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、Google、網(wǎng)站設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容