1

I have a UITableView and 10 sections in it. For each section I have a custom header.

In custom header I have a label as heading of section with text value date.

Table content size is (450,1000).

Here my label with text is also scrolling, but I want my label text should aligned center to screen and it should not scroll horizontally with table.

Thanks in advance.

  • Add that label as subview of you current controller view or on window. that way this label will not move with table. [Ref](http://stackoverflow.com/questions/12176666/appdelegate-uiwindow-addsubview-in-different-viewcontroller) – Pawan Rai Feb 16 '15 at 11:09
  • @pawan - Label will be there for every section and it should scroll vertically but not horizontally. as i scroll table view horizontally it is also scrolling with it. – Abhishek Kumar Verma Feb 16 '15 at 11:34
  • Your table view scrolls in both directions? That’s really unusual. How did you do it? – Douglas Hill Feb 16 '15 at 13:08
  • @Douglas Hill - i have modified its content size to make it scrollable vertically and horizontally. – Abhishek Kumar Verma Feb 16 '15 at 13:10
  • I’m glad you have it doing what you want, but be aware that it would be reasonable for the table view to reset its `contentSize` to match its own layout calculations. – Douglas Hill Feb 16 '15 at 18:22

3 Answers3

1

After knocking my head for hours finally i got this solution.

Simply i used

(void)scrollViewDidScroll:(UIScrollView *)scrollView*

method and changed my label frame with table view content offset.

myLabel.frame = CGRectMake(tableview.contentoffset.x, 0, deviceHeight, 27);

It will keep my label at same position to screen, logically it is changing but will look like its stable at same position.

Hima
  • 1,249
  • 1
  • 14
  • 18
0

That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.

May be the following links will help you to solve your problem: 1. UITableView with fixed section headers 2. https://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/

Community
  • 1
  • 1
nagarajan
  • 474
  • 3
  • 21
0
    - (void)addButtonsOnScrollViewHorizontal:(int)postion :(UIScrollView*)scrollView
    {
        [scrollView setShowsVerticalScrollIndicator:NO];
        float x = 30;
        for (int i = 1; i <=postion; i++)
        {
            UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTag:i];
            [button setTitle:[NSString stringWithFormat:@"%i",i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:26.0f/255.0f green:188.0f/255.0f blue:156.0f/255.0f alpha:100.0f] forState:UIControlStateSelected];
            button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f];
            CGSize maxSize = CGSizeMake(button.frame.size.width, FLT_MAX);
            CGRect labRect = [button.titleLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:button.titleLabel.font} context:nil];
            [button setFrame:CGRectMake(x, 0, labRect.size.width+10, 30)];
            [button addTarget:self action:@selector(scrollViewDaysbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (i==postion)
            {
                [_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f];
                [button setSelected:YES];
                currentSelectionDays =i;
            }
            [scrollView addSubview:button];
            x +=labRect.size.width+20;
        }
        scrollView .contentSize = CGSizeMake(x, 0);
    }


-(void)scrollViewDaysbtnClicked:(UIButton*)sender
{

    for (UIButton *button in _scrollViewDays.subviews)
    {
        if ([button isKindOfClass:[UIButton class]])
        {
            if (button.tag == sender.tag)
            {
                [_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f];
                [button setSelected:YES];
                currentSelectionDays =(int)sender.tag;
            }else
            {
                [button setSelected:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f];
            }
        }
    }
}

use it....

Chandan kumar
  • 1,074
  • 12
  • 31