美文网首页
双表连动

双表连动

作者: rainbow_H | 来源:发表于2018-07-23 09:07 被阅读0次

#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

{

    NSArray*arr;

}

// 定义左侧的表格

@property (nonatomic,strong) UITableView *leftTableView;

// 定义左侧的表格

@property (nonatomic,strong) UITableView *rightTableView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18"];

    [self.view addSubview:self.leftTableView];

    [self.view addSubview:self.rightTableView];

//    [UIScreen mainScreen].bounds.size.width

    // Do any additional setup after loading the view, typically from a nib.

}

-(UITableView*)leftTableView

{

    if (!_leftTableView)

    {

        _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,KSCREEN_WIDTH *0.3, KSCREEN_HEIGHT) style:UITableViewStylePlain];

        _leftTableView.delegate = self;

        _leftTableView.dataSource = self;

    }

    return _leftTableView;

}

-(UITableView*)rightTableView

{

    if (!_rightTableView)

    {

        _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(KSCREEN_WIDTH*0.3, 0, KSCREEN_WIDTH*0.7, KSCREEN_HEIGHT) style:UITableViewStyleGrouped];

        _rightTableView.delegate = self;

        _rightTableView.dataSource = self;

    }

    return _rightTableView;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    if(tableView ==_rightTableView)

    {

        returnarr.count;

    }

    else

    {

        return1;

    }

}

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

{

    if(tableView ==_leftTableView)

    {

        returnarr.count;

    }

    else

    {

        return5;

    }

}

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

{

    staticNSString*identifier =@"cell";

    UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

    if(!cell)

    {

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

    }

    if(tableView ==_leftTableView)

    {

        cell.textLabel.text=arr[indexPath.row];

    }

    else

    {

        cell.textLabel.text= [NSStringstringWithFormat:@"第%ld组 第%ld行",indexPath.section+1,indexPath.row+1];

    }

    returncell;

}

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section

{

    if(tableView ==self.rightTableView)

    {

        returnarr[section];

    }

    else

    {

        returnnil;

    }

}

//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

//{

//    return 10;

//}

// 

// 点击单元格的方法

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

    if(tableView ==self.leftTableView)

    {

        // 获取点击单元格的下标 (下标索引类)

        NSIndexPath*moveToPath = [NSIndexPathindexPathForRow:0inSection:indexPath.row];

        [self.rightTableView selectRowAtIndexPath:moveToPath animated:YES scrollPosition:UITableViewScrollPositionTop];

    }

}

// 只要滚动就调用

-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{

    if(scrollView ==self.leftTableView)

    {

        return;

    }

    else

    {

        // 取出显示在视图最靠上的cell的indexPath

        NSIndexPath *topHeaderIndex = [[self.rightTableView indexPathsForVisibleRows]firstObject];

        // 获取左侧表格移动的indexPath

        NSIndexPath*moveIndex = [NSIndexPathindexPathForRow:topHeaderIndex.sectioninSection:0];

        [self.leftTableView selectRowAtIndexPath:moveIndex animated:YES scrollPosition:UITableViewScrollPositionTop];

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

相关文章

  • 双表连动

    #define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.w...

  • 连表查询

    看上一篇帖子的表结构

  • 连表操作

    Mysql的链表操作语句为:SELECT * FROM table1 INNER|LEFT|RIGHT JOIN ...

  • sql _ 连表查询 & 授权

    一,连表查询 1)连表 简单查询_where 2) 连表 多种方式查询 3)子查询 综合以上查询示例 二,DCL数...

  • mybatis sql 语句

    双表更新

  • 表格连动效果

    摘要:两个tableview的联动,滑动左侧tableview,右侧tableview跟着滑动其实实现起来比较简单...

  • 连动句教学

    今天,廖老师跟大家讲一讲汉语里一个比较特殊的句式教学,连动句。连动句是什么呢?请看专业回答。 【现代汉语版】 由两...

  • 喜庆十月

    喜庆十月 南飞银燕排云上, 北动车驰逐月圆。 双节小城连万里, 京华更待亮屏前。

  • 双表联查

    表结构 t_course和t_sys_user系统用户表image 建立web模块,webapp类型的maven项...

  • 语法

    连动句、兼语句、双宾句 连谓句: 谓语是由两个或两个以上的谓语(动词或形容词)构成,中间没有停顿,没有关联词,也没...

网友评论

      本文标题:双表连动

      本文链接:https://www.haomeiwen.com/subject/nfjdmftx.html