UI-瀑布流多个TableView刷新

瀑布流就是多个tableview在一个屏幕上显示时,当其中一个tableview滚动时,有其他同时滚动的效果

以下是实现的代码:

- (void)viewDidLoad

{

    [super viewDidLoad];

    //创建表1

    table1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 160, 460) style:UITableViewStylePlain];

    table1.delegate = self;

    table1.dataSource = self;

    [self.view addSubview:table1];

    //创建表2

    table2 = [[UITableView alloc] initWithFrame:CGRectMake(160, 20, 160, 460) style:UITableViewStylePlain];

    table2.delegate = self;

    table2.dataSource = self;

    [self.view addSubview:table2];

}

 

//行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 20;

}

//创建样式

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *SectionsTableIdentefier = @"SectionsTableIdentefier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentefier];

    if(cell == nil)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentefier];

    }

    for (id aa in cell.contentView.subviews)

    {

        [aa removeFromSuperview];

    }

    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 40)];

    label.text = [NSString stringWithFormat:@"这里是第%d行",indexPath.row];

    [cell.contentView addSubview:label];

    return cell;

}

//高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(tableView == table1)

    {

        return 40;

    }

    else

    {

        return 80;

    }

}

//设置显示偏移量,让两个table保持一致,这个是保证瀑布流刷新关键

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if(scrollView == table1)

    {

        [table2 setContentOffset:table1.contentOffset];

    }

    else

    {

        [table1 setContentOffset:table2.contentOffset];

    }

}

posted @ 2015-02-15 23:20  IT赏金猎人  阅读(171)  评论(0)    收藏  举报