Problems setting view for header in a grouped table - ios

I'm trying to customize the headers for sections in a grouped table. The view I set for the first section in table looks fine, but subsequent section headers look like cropped at top and at bottom (in this screenshot it is only shown at top):
I've been trying different X and Y values for frame.size.origin, but it remains looking the same. This is my code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
UIView *wrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 70)];
[wrapper setBackgroundColor:[UIColor clearColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.tableView.frame.size.width, 20)];
label.text = NSLocalizedString(#"Section 2 Header", #"");
[label setFont:[UIFont boldSystemFontOfSize:15]];
[label setBackgroundColor:[UIColor clearColor]];
[wrapper addSubview:label];
return wrapper;
}
else
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 1)
return 70;
else
return 0;
}
I do the same for all the section headers, and only the first one is correctly displayed, what could I'm doing wrong? Regarding this issue, is it possible to dynamically know the height of an UILabel will take once its text and font size are known, or should you always set its frame size "at a guess"? The same for the height of view for the header that is set in heightForHeaderInSection: method.
Thanks!

you have not set height for header for the sections other than section 1 in this code,you should remove if(section==1) condition and provide common height for each section and then check
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 1)
return 70;
else
return 0;
}
thanks,
Mittal.

Related

Footer overlays on top of tableview cell contents while scrolls

I have added footerview programmatically for each section as follows. I could able to see the footerview.
However, when I scroll up or down at the bottom or at the top of the tableview, footerview overlays on top of tableviewcells.
How could I able to disable it?
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(adminOrderElements[section].expanded && [adminOrderElements[section].notes length]>0)
{
return 60;
} else {
return 0;
}
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
return footer;
}
Before Scroll
After Scroll
If I understand your question correctly, you only have to change the UITableViewStyle to UITableViewStyleGrouped. According to the docs, for table views using grouped style the section headers and footers do not float.

Show table view separator for empty sections

I have a UITableView set to the UITableViewStyleGrouped style. Whenever a section has one or more items, it displays a one pixel separator beneath the section header. However, if the section has no items, there is no separator between sections.
How can I get the separator to appear in all sections, even those that have no cells?
Notice how section 1 and 2 have a separator between them and their first cell, but section 3 doesn't have a separator between it and section 4.
One solution is to add a footer to each empty section:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return [self tableView:tableView numberOfRowsInSection:section] == 0 ? 0.5 : 0.00000001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.5)];
view.backgroundColor = tableView.separatorColor;
return view;
} else {
return nil;
}
}
Notes:
This uses the separatorColor to ensure it is consistent with the other lines in the table view.
Instead of returning 0 in heightForFooterInSection, it returns a very small number instead.
This produces correct results most of the time:
... but has several problems:
This exploits the footer, so if you need to display a footer, this solution probably won't work.
In edit mode, when you drag the last item out of a section, the border will disappear between that section and the one below it.
In edit mode, when you drag an item into an empty section, there will be a double border at the bottom of the section.
Example of problems 2 and 3:
In the above image, notice how there is no line between sections 2 and 3, since all the items were moved out of that section (problem 2).
Also notice how the last items in section 3 and 4 have double borders, since they were dragged into new sections (problem 3).
You can try customize your section header view. use this delegate funciton: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Use this two method to add separator between Section.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor brownColor]];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
img.backgroundColor = [UIColor whiteColor];
[headerView addSubview:img];
return headerView;
}

UITableView space between section and its header

I'm using a UITableView with 2 sections and i preserve a specific width for each section header as follows:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return 45;
}
if (section==1) {
return 20;
}
return 0;
}
also here's viewForHeaderInSection delegate method
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[UILabel alloc] init];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.font = [UIFont fontWithName:#"AmericanTypewriter-Bold" size:15];
lbl.textColor = [UIColor orangeColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.alpha = 0.9;
lbl.textAlignment = NSTextAlignmentLeft;
if(section == 0){
lbl.text =[LocalizationSystem get:#"Share_Today_times" alter:#""];
}
if(section == 1){
lbl.text =[LocalizationSystem get:#"Share_Month_Times" alter:#""];
}
return lbl;
}
the problem is that there's a space between the first section and its header which doesn't exist in the other sections, as you see in the image below the second section has no space between its header which appears apparently in the first section (the region with the black dots), does any one know how to solve these issue? thanks in advance
Did you try changing your heightForHeaderInSection method's implementation to
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
The reason might be that Share Today Prayers Time section has index 0 and you set height of 45 pixels to it, when the next section has index 1 and height of 20, which is apparently not what you want.

Custom UITableView Section not increasing height

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);

UITableView how to add a custom UIImageView as a separator and set the custom position

I have a UITableView with a number of sections (not rows). I'd like to add the UIImageView as a separator between each sections at the center (except the last cell).
The space between cells is 30
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
The screenshot
I'd like to set the position like at the screenshot.
The code I've made
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
The cell height is 81.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Some code
UIImageView *separatorIView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 96, 196, 2)];
[separatorIView setImage:[UIImage imageNamed:#"grayline.png"]];
[cell.contentView addSubview:separatorIView];
return cell;
}
The grayline appears if I set the y coordinate < then cell height.
Thanks in advance.
The result
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
if (section > 0) {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(62, 14, 196, 2)];
[img setImage:[UIImage imageNamed:#"grayline.png"]];
[headerView addSubview:img];
}
return headerView;
}
Custom separator position is not possible because they are privately defined for its frame, apart from that if you need that kind of effect then you have to add a image in header like custom space and color with require view with that only you can get the desired view
As you are setting a head view height you can add separator in header. Table View has a delegate method named "viewForHeaderInSection".
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 5)];
[img setImage:[UIImage imageNamed:#"grayline.png"]];
return img;
}
If you set the separator image in this delegate method you don't need to do anything in cellForRowAtIndexPath.
Let me know if that helps... :)
Edit:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 5;
}
`
You can set Separator color using setSeparator color in which you can pass image
like,
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"grayline.png"]]];
just try to make the height of the last header as zero and see if it works.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(make_cndition_for_lastsection)
return 0;
else
return 30;
}

Resources