Hey i try to change the width of my footer for sections with:
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
[viewFooter setBackgroundColor:[UIColor blueColor]];
return viewFooter;
}
But the footer is always as width as the tableview (320.0f).....
Yes, the table view will always make the section head the full width of the table.
The easiest solution is to make your "real" footer a subview of the actual footer view.
Something like this:
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *mainFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
[viewFooter setBackgroundColor:[UIColor blueColor]];
viewFooter.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth;
[mainFooter addSubview:viewFooter];
return mainFooter;
}
// You must implement this when you implement viewForFooterInSection
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 44;
}
You need to add a container around your desired footer custom view if it will have a different size than the actual tableview. This should work:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
[viewFooter setBackgroundColor:[UIColor blueColor]];
// Create a footer container that has a fixed width
// but in which you can adjust subviews frame as you want.
//
UIView *footerContainer = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 320, 44)];
[footerContainer addSubview:viewFooter];
return footerContainer;
}
Related
I'm trying to make a custom TableView Section. That is higher than the normal section header.
The problem is when i example increase the height from 18 to 30. Nothing happen it does not get any bigger. What am i doing wrong?
i have this code:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =[[sectionsArray objectAtIndex:section] objectForKey:#"league"];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
Implement this method :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 18.0; // whatever height you want
}
If you are using iOS 7, you can use implemeent the following method also :
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection: (NSInteger)section NS_AVAILABLE_IOS(7_0);
In the screenshot below, the blue colored line is the header and footer of the sections of my tableView (In the tableView, I am treating rows as sections).
However, i want the blue line to be just below the row of the tableView (of same width as the row). Any idea how to do it??
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
return headerView;
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
footerView.backgroundColor = [UIColor blueColor];
return footerView;
}
You can add UIView with Blue color above the UIView for which set the background color to Clear Color
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *dummyfooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 2)] ;
dummyfooterView.backgroundColor = [UIColor clearColor];
// Widht should be less than dummyfooterView
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320-20, 2)] ;
footerView.backgroundColor = [UIColor blueColor];
[dummyfooterView addSubview:footerView];
return dummyfooterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 2;
}
I am not sure though, hope this may help you! The output will be like below screen. I have used Grouped tableview style here.
I think the best way is to add 1px view at bottom of cell at row at indexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageBoxCell";
LGMessageBoxCell *cell = (LGMessageBoxCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.fram.size.height - 1, cell.fram.size.width, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
cell.backgroundView = headerView;
return cell;
}
Set height for header and fooder
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 58;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 58;
}
I have try to give title for section in uitabelview, The label contaning view size is not able to increase more than 50, Here my code,
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView.tag==20)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 55)];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]];
return view;
}
}
How to add section like as in the image,
I think this will resolve your issue.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
Enjoy the coding......
Go to xib in Table set in setction Height see the below image
I used the following code to set the header view of a uitableview section. The problem is, the background color (mainLightColor_2) doesn't show up until I scroll the tableview. How can I make the background color shown on load?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerView.backgroundColor = [Utility mainLightColor_2]; //mainLightColor is a blue color
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}
Before scroll:
After scroll:
You should also implement - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section method.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 24;
}
So I found a solution to this. However, I still don't understand why setting the background color of headerView doesn't work.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
UIView *headerViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerViewBackground.backgroundColor = [Utility mainLightColor_2];
[headerView addSubview:headerViewBackground];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}
I created a new Single-view project (using storyboard) in Xcode with a single UITableViewController. Here is the setup code:
- (void)viewDidLoad {
[super viewDidLoad];
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 44, 44)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return _footerView.frame.size.height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return _footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView dequeueReusableCellWithIdentifier:#"Cell"];
}
I want the label in the custom table footer view to be drawn at x = 60, but when I run the project, at first the label is invisible (in portrait, screen attached). Then if I rotate once, it becomes visible and if I rotate back to portrait it's visible.
What am I missing?
You seem to be initialising your footer view with a width and height of 44px, yet adding the label outside of its bounds.
Try the following instead:
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectInset(_footerView.bounds, 60.0f, 0.0f)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
As a further point, try not to use [UIColor clearColor] as a label background colour if you can help it - it significantly reduces scrolling performance. In this case you should use [UIColor lightGrayColor] so it matches its superview.