#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *dataArray1;
NSMutableArray *dataArray2;
UITableView *mytableViewLeft;
UITableView *mytableViewRight;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mytableViewLeft = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 330, 350) style:UITableViewStylePlain];
[self.view addSubview:mytableViewLeft];
mytableViewLeft.delegate = self;
mytableViewLeft.dataSource = self;
// 定制单元格
[mytableViewLeft registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CELL"];
mytableViewRight = [[UITableView alloc] initWithFrame:CGRectMake(333, 10, 330, 350) style:UITableViewStylePlain];
[self.view addSubview:mytableViewRight];
mytableViewRight.delegate = self;
mytableViewRight.dataSource = self;
[self loadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == mytableViewLeft) {
return dataArray1.count;
}
else{
return dataArray2.count;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == mytableViewLeft) {
return 80;
}
else{
return 40;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == mytableViewLeft) {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
cell.titleLabel.text = dataArray1[indexPath.row];
return cell;
}
else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
}
cell.textLabel.text = dataArray2[indexPath.row];
return cell;
}
}
// 获取数据
- (void) loadData{
if (!dataArray1) {
dataArray1 = [NSMutableArray array];
}
for (int i=0; i<10; i++) {
NSString *str = [NSString stringWithFormat:@"左 %d",i];
[dataArray1 addObject:str];
}
if (!dataArray2) {
dataArray2 = [NSMutableArray array];
}
for (int i=0; i<15; i++) {
NSString *str = [NSString stringWithFormat:@"右 %d",i];
[dataArray2 addObject:str];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == mytableViewLeft) {
NSLog(@"%@",[NSString stringWithFormat:@"左边 第 %ld 行",indexPath.row + 1]);
}
else{
NSLog(@"%@",[NSString stringWithFormat:@"右边 第 %ld 行",indexPath.row + 1]);
}
}
@end
![](https://img.haomeiwen.com/i1388997/400a1c03a88645d6.png)
Simulator Screen Shot 2016年5月5日 下午1.18.55.png
网友评论