欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解ios中自定義cell,自定義UITableViewCell

 更新時(shí)間:2016年12月20日 11:38:49   作者:SwingPyzf  
本篇文章主要介紹了ios中自定義cell,自定義UITableViewCell,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

通過繼承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)文章

最新評論