詳解ios中自定義cell,自定義UITableViewCell
通過繼承UITableViewCell來自定義cell
1、創(chuàng)建一個(gè)空的項(xiàng)目、命名:
2、創(chuàng)建一個(gè)UITableViewController 并且同時(shí)創(chuàng)建xib:
3、設(shè)置AppDelegate.m中window的根控制器為剛剛創(chuàng)建的TableViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; TableViewController *tableViewController = [[[TableViewController alloc] init] autorelease]; //自動(dòng)釋放 //設(shè)置根控制器 self.window.rootViewController = tableViewController; [self.window makeKeyAndVisible]; return YES; }
4、創(chuàng)建自定義的UITableViewCell:
5、創(chuàng)建自定義cell的xib 拖放需要的控件
選擇User Interface。
創(chuàng)建空的xib。
拖入Cell控件。
完成自定義的cell控件。
設(shè)置cell控件的Identfier。
綁定Cell類并且將控件的輸出口關(guān)聯(lián)到TableViewCell.h文件中。
6、對TableViewController類編碼,在委托方法中設(shè)置自定義的Cell:
#import "TableViewController.h" #import "TableViewCell.h" @interface TableViewController (){ NSMutableArray *tableData; //表格數(shù)據(jù) } @end @implementation TableViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //初始化表格數(shù)據(jù) tableData = [[NSMutableArray alloc] init]; for (int i = 0; i< 10; i++) { [tableData addObject:[NSString stringWithFormat:@"MyCellDemon%i",i]]; } //設(shè)置row的高度為自定義cell的高度 self.tableView.rowHeight = 90; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. return [tableData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //指定cellIdentifier為自定義的cell static NSString *CellIdentifier = @"TableViewCell"; //自定義cell類 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //通過xib的名稱加載自定義的cell cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject]; } //添加測試數(shù)據(jù) cell.titleLabel.text = [tableData objectAtIndex:indexPath.row]; cell.content.text = @"這是一些測試數(shù)據(jù)"; //測試圖片 cell.iamge.image = [UIImage imageNamed:@"testImage.jpg"]; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } @end
最終效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果
這篇文章主要介紹了iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果,整體思路是我們將tableView 的contentinset設(shè)置為上面一個(gè)屏幕的高度,下面一個(gè)屏幕的高度,左右為0,這樣保證我們滾動(dòng)過去的時(shí)候2024-06-06
都是準(zhǔn)備好的內(nèi)容,需要的朋友可以參考下IOS開發(fā)使用KeychainItemWrapper 持久存儲(chǔ)用戶名和密碼
這篇文章主要介紹了IOS開發(fā)使用KeychainItemWrapper 持久存儲(chǔ)用戶名和密碼的相關(guān)資料,需要的朋友可以參考下2015-11-11IOS開發(fā)UIPasteboard類的粘貼板全面詳解
這篇文章主要為大家介紹了IOS開發(fā)UIPasteboard類的粘貼板全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06iOS App設(shè)計(jì)模式開發(fā)中對interpreter解釋器模式的運(yùn)用
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對interpreter解釋器模式的運(yùn)用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下2016-04-04iOS中UILabel實(shí)現(xiàn)長按復(fù)制功能實(shí)例代碼
在iOS開發(fā)過程中,有時(shí)候會(huì)用到UILabel展示的內(nèi)容,那么就設(shè)計(jì)到點(diǎn)擊UILabel復(fù)制它上面展示的內(nèi)容的功能,也就是Label長按復(fù)制功能,下面這篇文章主要給大家介紹了關(guān)于在iOS中UILabel實(shí)現(xiàn)長按復(fù)制功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10iOS 對NSMutableArray進(jìn)行排序和過濾的實(shí)例
下面小編就為大家分享一篇iOS 對NSMutableArray進(jìn)行排序和過濾的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01ios基于UICollectionView實(shí)現(xiàn)橫向瀑布流
這篇文章主要為大家詳細(xì)介紹了ios基于UICollectionView實(shí)現(xiàn)橫向瀑布流,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-10-10