这是以Android的开发角度实现的,虽然是Android的角度却也是编程思想。也就是说所有的语言理论上都是这样实现!
实现流程:
1.创建一个数据模型,在接触OC的过程中,发现OC的程序员好喜欢用字典,在Android中对应的是哈希表。然后进行几个字典的嵌套使用。(可能是我接触的OC程序员不多)这种实现方式极大的造成了代码的可读性和健壮性,并且非常难以维护。我们之所以要创建类这个东西,就应该把它用的淋漓尽致。干嘛要自己去维护每一个变量,每个集合?不嫌累么?特别是3层,4层的嵌套,人都要晕了……扯远了!
2.创建数据源
3.显示数据源到tableview上面。
看代码:ViewController.h
ViewController.m
然后创建一个新文件,一个Cocoa Touch Class,然后输入名字,把Also Create Xib File勾上。SubClass填UITableViewCell。然后点中新创建的XIB文件,随便拖两个玩意儿上面。如图:
然后点住控件,按住Command键+鼠标左键不放,拖到MyTableViewCell.h文件,然后编译器自动添加以下代码
好吧,运行程序效果图:
实现流程:
1.创建一个数据模型,在接触OC的过程中,发现OC的程序员好喜欢用字典,在Android中对应的是哈希表。然后进行几个字典的嵌套使用。(可能是我接触的OC程序员不多)这种实现方式极大的造成了代码的可读性和健壮性,并且非常难以维护。我们之所以要创建类这个东西,就应该把它用的淋漓尽致。干嘛要自己去维护每一个变量,每个集合?不嫌累么?特别是3层,4层的嵌套,人都要晕了……扯远了!
2.创建数据源
3.显示数据源到tableview上面。
看代码:ViewController.h
// // ViewController.h // Leehom // // Created by leehom on 15/9/1. // Copyright (c) 2015年 leehom. All rights reserved. // #import#define STATUS_HEIGHT 20 // Screen #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCREEN_BOUNDS [UIScreen mainScreen].bounds #define SCREEN_SIZE [UIScreen mainScreen].bounds.size @interface CustomItem : NSObject @property (strong,nonatomic) NSString *lText; @property (strong,nonatomic) NSString *bText; @end @interface ViewController : UIViewController @property (strong,nonatomic) UITableView *tabView; @property (strong,nonatomic) UINib *nib; @property (strong,nonatomic) NSMutableArray *array; @end
ViewController.m
// // ViewController.m // Leehom // // Created by leehom on 15/9/1. // Copyright (c) 2015年 leehom. All rights reserved. // #import "ViewController.h" #import "MyTableViewCell.h" @interface ViewController () @end @implementation CustomItem @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.view setBackgroundColor:[UIColor whiteColor]]; _tabView = [[UITableView alloc]initWithFrame: CGRectMake(0, STATUS_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-STATUS_HEIGHT) style:UITableViewStylePlain]; [_tabView setDataSource:self]; [_tabView setDelegate:self]; _array = [[NSMutableArray alloc]init]; for (int i=0; i<30; ++i) { CustomItem *item = [[CustomItem alloc]init]; [item setLText:[[NSString alloc] initWithFormat:@"Label:%d",i]]; [item setBText:[[NSString alloc] initWithFormat:@"Button:%d",i]]; [_array setObject:item atIndexedSubscript:i]; } [self.view addSubview:_tabView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"mycell"; if(_nib == nil){ //在使用复用机制之前要先确保registerNib这个方法 _nib = [UINib nibWithNibName:@"MyTableViewCell" bundle:nil]; [_tabView registerNib:_nib forCellReuseIdentifier:identifier]; } MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; CustomItem* item = [_array objectAtIndex:indexPath.row]; [cell.customLabel setText:[item lText]]; [cell.customButton setTitle:[item bText] forState:UIControlStateNormal]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_array count]; } -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } @end
然后创建一个新文件,一个Cocoa Touch Class,然后输入名字,把Also Create Xib File勾上。SubClass填UITableViewCell。然后点中新创建的XIB文件,随便拖两个玩意儿上面。如图:
然后点住控件,按住Command键+鼠标左键不放,拖到MyTableViewCell.h文件,然后编译器自动添加以下代码
@property (strong, nonatomic) IBOutlet UILabel *customLabel; @property (strong, nonatomic) IBOutlet UIButton *customButton;
好吧,运行程序效果图:
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2300
- 用户1336
- 访客10861715
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员