詳解ios中自定義cell,自定義UITableViewCell
通過繼承UITableViewCell來自定義cell
1、創(chuàng)建一個空的項目、命名:
2、創(chuàng)建一個UITableViewController 并且同時創(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]; //自動釋放 //設(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS開發(fā)使用KeychainItemWrapper 持久存儲用戶名和密碼
這篇文章主要介紹了IOS開發(fā)使用KeychainItemWrapper 持久存儲用戶名和密碼的相關(guān)資料,需要的朋友可以參考下2015-11-11IOS開發(fā)UIPasteboard類的粘貼板全面詳解
這篇文章主要為大家介紹了IOS開發(fā)UIPasteboard類的粘貼板全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下2016-04-04iOS中UILabel實現(xiàn)長按復(fù)制功能實例代碼
在iOS開發(fā)過程中,有時候會用到UILabel展示的內(nèi)容,那么就設(shè)計到點擊UILabel復(fù)制它上面展示的內(nèi)容的功能,也就是Label長按復(fù)制功能,下面這篇文章主要給大家介紹了關(guān)于在iOS中UILabel實現(xiàn)長按復(fù)制功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10ios基于UICollectionView實現(xiàn)橫向瀑布流
這篇文章主要為大家詳細介紹了ios基于UICollectionView實現(xiàn)橫向瀑布流,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12