Show table view separator for empty sections - ios

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

Related

UITableview footer comes back after deselecting the last row

I have successfully removed the colored separator at the bottom of my UITableView with:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
//[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
However when I deselect the last row, the footer gets drawn again.
How can I prevent the footer in my section 1 from ever being drawn please?

UITableView Background Image for Static Section/Cells

I know we can add background images/colors to section headers in dynamic table view/cells but can anyone help me do the same in a table view using Static Cells ?
What I want to do is use a background image for my 3rd section in a TableView which is using static cells (total of 4 sections it has)
I want to add a background image and change text color to say some RGB value for the 3rd section
You can use the delegate methods of UITableView to set the height and view for a section header. This should do what you want:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 100:30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
label.text = #"Description";
label.textColor = [UIColor whiteColor];
[imageView addSubview:label];
imageView.image = [UIImage imageNamed:#"House.tiff"];
return (section == 2)? imageView:nil;
}
UITableViewCells have the backgroundView property which is a UIView. You can change that to be a UIImageView, for example, or build a more complex background for you table view cells. It shouldn't matter, whether the cell is static or dynamic.
The text color for the cell can then simply be changed by setting the UITableViewCells cell.textLabel.textColor.
Same applies for the UITableViewHeaderFooterView, which are (as the name says) used for the header and footer of your table view sections. In code, you can access the header for a section using
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
This saves you from completely recreating the header from scratch, just to make small adjustments. Alternatively, override
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and build a custom UIView that will become your header.
You should assign a tag(say 15) to the static cell in
tableview:didSelectRowForIndexPath
and then
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag==15)
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:250/255.0 blue:243/255.0 alpha:1.0];
}

Problems setting view for header in a grouped table

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.

Increasing the space between header and table cell in iOS

So my UITableView has a header, which is the UIImageView shown, and comments below the image. I am trying to increase the space between the image and the comments table.
(I have tried increasing the height of the header, but it doesn't work in my case because it will result in a bigger UIImageView and the image won't cover the view completely)
I experimented with this hack:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CommentsTableCell";
CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Comment *comment = [self.comments objectAtIndex:indexPath.row];
[cell setUsername:comment.handle andText:comment.text];
/* Dirty hack:
1. We cannot increase the height of the header because that will leave spaces in the image.
2. The only way we can increase the margin from the comments table to the picture is by
increasing the individual inset of the first and last comments cell
*/
if (indexPath.row == 0) {
[cell setContentInset:UIEdgeInsetsMake(COMMENTS_PADDING * 10 , 0, 0, 0)];
} else if (indexPath.row == [self.comments count] - 1) {
[cell setContentInset:UIEdgeInsetsMake(0, 0, COMMENTS_PADDING * 10 , 0)];
}
return cell;
}
and in my CommentsCell.m:
- (void)awakeFromNib {
self.commentText.scrollEnabled = false;
self.commentText.editable = false;
self.commentText.contentInset = UIEdgeInsetsMake(-1 * COMMENTS_PADDING, 0, 0, 0);
}
- (void)setUsername:(NSString *)username andText:(NSString *)text {
[self.commentText setAttributedText:[CommentsCell getContentStringForUsername:username andText:text]];
}
- (void)setContentInset:(UIEdgeInsets)inset {
self.commentText.contentInset = inset;
}
but the first comment still has the same inset. I checked the debugger and awakeFromNib is occurring before cellForRowAtIndexPath. Do you see why my method is not working?
I am also open to other suggestions.
You should be able to add some space to the header view just below the image you display. Instead of setting the table's header view to a UIImageView, why not create a container view that you can add the image view to and then just have some space below it.
- (UIView *) buildTableHeaderView {
UIImage *image = [UIImage imageNamed: #"my_image.png"];
CGFloat height = image.size.height + 20;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.frame.size.width, height)];
[imageView setImage: image];
UIView *headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.bounds.size.width, height)];
[headerView addSubview: imageView];
return headerView;
}
What you can do is create a custom UIView (with .xib if you want for easier UI design) with a space on the bottom and return it from - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method of the UITableViewDelegate, also don't forget to return the height for the header view by implementing the - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section method of the same UITableViewDelegate.
Here is a short example:
-UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
YourCustomHeaderView *headerView = [YourCustomHeaderView instantiateView]; //static method (you can rename it) that will load the custom view from a .xib
//do aditional UI setup or not
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return DEFAULT_HEADER_HEIGHT; //a defined value
}
If you are having a single header view, then you should use the same custom header view creation/init/setup, but move your table downwards in is superview and add the custom header view at the top at any position you like.

How can I remove the vertical spacing between UITableView sections?

I have a grouped table view with a few sections. By default, there is gap between the sections. I've been looking around for a simple way to remove the gap completely without any luck. The best I could do through the Interface Builder is to set the "Section Height" values in the "Table View Size" property section, but these fields do not accept 0 as valid input. Can anyone tell me the best way to sort this out?
The above answer for me did not work, I had to actually create a UIView with a height of 1 and a background color of clear and then set it's y origin coordinate to -1.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -1, tableView.frame.size.width, 1)];
[view setBackgroundColor:[UIColor clearColor]];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}
This gave me a Grouped tableView where the section cells would not scroll underneath the headerView and not have padding in-between the sections.
In your table view's delegate, you can implement tableView:heightForHeaderInSection: and tableView:heightForFooterInSection:.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}

Resources