您的位置:首页 > 产品设计 > UI/UE

UITableView的用法--俩个UITableView并排

2016-01-28 18:57 232 查看
摘要: UITableView继承与UIScrollView

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView*_tableView1;
UITableView*_tableView2;
}
@property(nonatomic,strong)NSMutableArray*dataArray1;
@property(nonatomic,strong)NSMutableArray*dataArray2;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self createTableView];
[self loadData];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)loadData{
//创建数据

self.dataArray1=[NSMutableArray arrayWithCapacity:10000];
self.dataArray2=[NSMutableArray arrayWithCapacity:10000];
for (int i=0; i<10000; i++) {
NSString*str=[NSString stringWithFormat:@"%d",i];
NSString*str1=[NSString stringWithFormat:@"%d",i+1000];
[self.dataArray1 addObject:str];
[self.dataArray2 addObject:str1];

}
[_tableView1 reloadData];
[_tableView2 reloadData];

}
-(void)createTableView{
_tableView1=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView1.delegate=self;
_tableView1.dataSource=self;
[self.view addSubview:_tableView1];

_tableView2=[[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView2.delegate=self;
_tableView2.dataSource=self;
[self.view addSubview:_tableView2];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==_tableView1) {
return self.dataArray1.count;
}else{
return self.dataArray2.count;
}

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==_tableView1) {
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"tableView1"];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableView1"];
}
cell.textLabel.text=self.dataArray1[indexPath.row];

return cell;
}else{
UITableViewCell*cell1=[tableView dequeueReusableCellWithIdentifier:@"tableView2"];
if (!cell1) {
cell1=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableView2"];
}
cell1.textLabel.text=self.dataArray2[indexPath.row];
return cell1;

}

}
#pragma mark 瀑布流核心方法(控制俩个tableview1和tableview2一起滑动)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView==_tableView1) {
_tableView2.contentOffset=_tableView1.contentOffset;
}else{
_tableView1.contentOffset=_tableView2.contentOffset;
}

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return arc4random()%40+40;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: