这是以Android的开发角度实现的,虽然是Android的角度却也是编程思想。也就是说所有的语言理论上都是这样实现!
实现流程:
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;
好吧,运行程序效果图:

本文链接:https://it72.com/5633.htm