Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Created March 4, 2016 07:03
Show Gist options
  • Save wisaruthk/1b6522394fc05a6ef6d6 to your computer and use it in GitHub Desktop.
Save wisaruthk/1b6522394fc05a6ef6d6 to your computer and use it in GitHub Desktop.
iOS custom uitableview header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView==self.tableView1){
CGRect frame = tableView.frame;
//NSLog(@"self view %@",NSStringFromCGRect(self.view.frame));
//NSLog(@"tableview frame %@",NSStringFromCGRect(frame));
UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
CGFloat btnWidth = 50;
CGFloat btnTrail = 10;
UIButton *sortButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-(btnWidth+btnTrail), 0, btnWidth, 20)];
sortButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[sortButton setTitle:LocalizedString(@"Sort") forState:UIControlStateNormal];
sortButton.backgroundColor = [UIColor redColor];
[sortButton addTarget:self action:@selector(sortIngdPopup:withEvent:) forControlEvents:UIControlEventTouchUpInside];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
title.text = LocalizedString(@"Ingredients");
//UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[headerView addSubview:sortButton];
return headerView;
} else {
CGRect frame = tableView.frame;
//NSLog(@"self view %@",NSStringFromCGRect(self.view.frame));
//NSLog(@"tableview frame %@",NSStringFromCGRect(frame));
UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
CGFloat btnWidth = 50;
CGFloat btnTrail = 10;
UIButton *sortButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-(btnWidth+btnTrail), 0, btnWidth, 20)];
sortButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[sortButton setTitle:LocalizedString(@"Sort") forState:UIControlStateNormal];
sortButton.backgroundColor = [UIColor redColor];
[sortButton addTarget:self action:@selector(sortNtPopup:withEvent:) forControlEvents:UIControlEventTouchUpInside];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
title.text = LocalizedString(@"Nutrient Specs");
//UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[headerView addSubview:sortButton];
return headerView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment