简体   繁体   中英

How can I set offset for table view and scroll view in iOS, so that both scroll together

在此处输入图片说明

I want to create data table with header and data. Header and data should scroll horizontally. Header should not scroll vertically (ie header position will be fixed). And data part should scroll horizontally as header scroll, if data is more it should scroll vertically also, but without affecting to header.

First I have created UIView, in that for header part I created ScrollView and for remaining data I have used TableView. I am trying to set offset but its not working.

Please let me know if there is any other way

If you want to scroll your tableview on horizontal direction then make some change in your table view xib......

1- Set tableview Width According your coloumn ..

2- check Direction Lock Enable

3- and check Bounces In your xib..

and then write this line for iPhone and iPad sepretly....

    [tableViewq setContentInset:UIEdgeInsetsMake(0,0,0,500)];

Here you need to change the value 500 only according your scroll

With this your table scroll horizontally

this is the solution for your query.... Do stuff like this

     -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
      {

          UIView *hView = [[UIView alloc] initWithFrame:CGRectZero] ;

          UILabel *hLabel=[[UILabel alloc] init] ;
          hLabel.backgroundColor = [UIColor grayColor];
          hLabel.textColor = [UIColor whiteColor];
         [hView addSubview:hLabel];

          UILabel *hlb1=[[UILabel alloc]init];
          hlb1.backgroundColor=[UIColor grayColor];
          hlb1.text=@"Name"; 
          hlb1.tag=100;
         [hLabel addSubview:hlb1];

        if(IS_IPAD)
           {
              hLabel.frame=CGRectMake(0,0,width Of Table View,Height of Header);
           }

        if(IS_IPHONE)
          {
               hLabel.frame=CGRectMake(0,0,width Of Table View,Height of Header);
           }
       return hView;
     }

You can add to each cell of table view a scroll view. After you would be able to scroll with each cell vertically.

Something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];;
    if(!cell) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:cell.bounds];
        [cell addSubview:scrollView];
        [scrollView setContentSize:CGSizeMake(cell.frame.size.width * 2, cell.frame.size.height)];
    }
    return cell;
}

A UITableView is a subclass of a UIScrollview and so it is not recommended to add a scrollview in the tableview.

I would advice you to use a scrollview and a tableview separately, then set the frame of a tableview to (0, scrollview.frame.origin.y+scrollview.frame.size.height,width,height).

So now you can handle horizontal and vertical scrolling independently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM