1

enter image description here

I want to create data table with header and data. Header and data should scroll horizontally. Header should not scroll vertically (i.e. 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

6

3 Answers 3

1

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;
     }
5
  • This help you to scroll in both direction @Madhuri Apr 12, 2014 at 8:52
  • Yes I know, but what I want is scroll view and table view both scroll together
    – Madhuri
    Apr 12, 2014 at 8:59
  • @madhuri there are scrollview already added in tableview .. no never need to create a scroll view and then add a tableview on scroll view Apr 12, 2014 at 9:04
  • Please have look on image which I have posted ... Solution which u told me was working correct for horizontal view. But I need to scroll data vertically too without affecting to header position
    – Madhuri
    Apr 12, 2014 at 9:09
  • @Madhuri have a look at this stackoverflow.com/questions/22221897/… Apr 12, 2014 at 9:16
0

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;
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.