user5102362
user5102362

Reputation:

Custom view is not fit to header in UItableview

Hi i have tried to set custom view on tableview header but custom view is not fit to header.

Custom view is coming like below image,in this image custom view is orange color and header view is gray color.But i want fit custom view is full of header view.

enter image description here

please help.

myCode:

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

        UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)];
        header = [[HeaderView alloc] initWithFrame:CGRectMake(sectionView.frame.origin.x, sectionView.frame.origin.y, sectionView.frame.size.width, sectionView.frame.size.height)];
        [sectionView addSubview:header];
        sectionView.tag=section;
        sectionView.backgroundColor = [UIColor grayColor];

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];

        return  sectionView;
    }

Upvotes: 2

Views: 1842

Answers (5)

Jaimish
Jaimish

Reputation: 629

If you use Xib to load custom view as a section header then do the code like below:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *headerview = [[[NSBundle mainBundle] loadNibNamed:@"SecHeader"
                                                            owner:self
                                                          options:nil] objectAtIndex:0];
    headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80);

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerview addGestureRecognizer:headerTapped];

    return headerview;
}

Upvotes: 3

Jaimish
Jaimish

Reputation: 629

If you use the story board then create an view as a tableview cell

  1. Drag and drop 2 tableview cell
  2. Design one cell as an header view and other cell will use as tableview listing cell. And give auto layout of sub views of each cell.

enter image description here

Now give Different cell identifier to each cell from interface like below

enter image description here

Now use this cell in view for section in header view delegate of tableview.

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *identifier = @"SectionHeader";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    return cell;
}

Hope It helps!

Upvotes: 0

iSashok
iSashok

Reputation: 2446

Try to use autolayout

[header setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(header);
NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views];
NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views];
[sectionView addConstraints:horizontalConstraints];
[sectionView addConstraints: verticalConstraints];

and your code will look like below

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

        UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)];
        [header setTranslatesAutoresizingMaskIntoConstraints:NO];
        header = [[HeaderView alloc] init];
        [sectionView addSubview:header];

        NSDictionary *views = NSDictionaryOfVariableBindings(header);
        NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views];
        NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views];

        [sectionView addConstraints:horizontalConstraints];
        [sectionView addConstraints: verticalConstraints];

        sectionView.tag=section;
        sectionView.backgroundColor = [UIColor grayColor];

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];

        return  sectionView;
    }

Upvotes: 1

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

You can use UITableViewHeaderFooterView. Create your custom view as subclass of UITableViewHeaderFooterView. Use proper constraints .Now use this view as header view.

YourHeaderView *YourHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YourHeaderViewIdentifier"];

Upvotes: 1

user3182143
user3182143

Reputation: 9609

in viewForHeaderInSection

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *labelSection;
UIView  *viewSection = [[UIView alloc]init];
viewSection.frame = CGRectMake(0, 0, tableview.frame.size.width, 20);
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);

[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = @"section title";
labelSection.text = name;
[viewSection setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName"]]];
[labelSection setTextColor:[UIColor whiteColor]];
[viewSection addSubview:labelSection];
return viewSection;
}

Upvotes: 0

Related Questions