国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

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

2020-07-26 03:01:29
字體:
來源:轉載
供稿:網友

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

最終效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙游县| 社会| 凤冈县| 黑龙江省| 泸溪县| 满洲里市| 秭归县| 绵竹市| 张家港市| 河间市| 汽车| 琼中| 柳林县| 应用必备| 洞头县| 明水县| 绥中县| 寿阳县| 平泉县| 宁都县| 探索| 平和县| 公主岭市| 县级市| 周至县| 湖北省| 鄂托克旗| 崇义县| 包头市| 德格县| 盘山县| 涿鹿县| 东台市| 饶河县| 五大连池市| 平阳县| 德惠市| 南郑县| 长武县| 柞水县| 芮城县|