詳解ios中自訂cell,自訂UITableViewCell_IOS

來源:互聯網
上載者:User

通過繼承UITableViewCell來自訂cell

1、建立一個空的項目、命名:

2、建立一個UITableViewController 並且同時建立xib:

3、設定AppDelegate.m中window的根控制器為剛剛建立的TableViewController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];   TableViewController *tableViewController = [[[TableViewController alloc] init] autorelease]; //自動釋放   //設定根控制器   self.window.rootViewController = tableViewController;   [self.window makeKeyAndVisible];   return YES; } 

4、建立自訂的UITableViewCell:

5、建立自訂cell的xib 拖放需要的控制項
選擇User Interface。

建立空的xib。

拖入Cell控制項。

完成自訂的cell控制項。

設定cell控制項的Identfier。

綁定Cell類並且將控制項的輸出口關聯到TableViewCell.h檔案中。

6、對TableViewController類編碼,在委託方法中設定自訂的Cell:

#import "TableViewController.h" #import "TableViewCell.h"  @interface TableViewController (){   NSMutableArray *tableData; //表格式資料 }  @end  @implementation TableViewController  - (id)initWithStyle:(UITableViewStyle)style {   self = [super initWithStyle:style];   if (self) {     // Custom initialization   }   return self; }  - (void)viewDidLoad {   [super viewDidLoad];   //初始化表格式資料   tableData = [[NSMutableArray alloc] init];   for (int i = 0; i< 10; i++) {     [tableData addObject:[NSString stringWithFormat:@"MyCellDemon%i",i]];   }    //設定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];   }      //添加測試資料   cell.titleLabel.text = [tableData objectAtIndex:indexPath.row];   cell.content.text = @"這是一些測試資料";   //測試圖片   cell.iamge.image = [UIImage imageNamed:@"testImage.jpg"];    return cell; }  #pragma mark - Table view delegate  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  }  @end 

最終效果:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.