UITableView separator gone when using viewForFooterInSection - ios

UITableView showing separator even there is no rows. But when I use viewForFooterInSection the separator is gone. I want it to keep showing up. What should I do ?
Here is my code when I add footer in it :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"s"];
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
// -------------------------- footer code start here --------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor grayColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
title.text = #"footer here";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[view addSubview:title];
return view;
}

this github project may give you some advices:
CLTableWithFooterViewController
Of course need some change if you would like the footer always display on the screen even if with a lot of cells.

remove - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
and - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
use this code to keep the footer down (but always visible) and keep the separator:
#implementation tableView {
__weak UIView *_staticView;
}
- (void)viewDidLoad {
UIView *staticView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bounds.size.height-50, self.tableView.bounds.size.width, 50)];
staticView.backgroundColor = [UIColor redColor];
//add other code for UILabel here
[self.tableView addSubview:staticView];
_staticView = staticView;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_staticView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is needed to prevent cells from being displayed above our static view
[self.tableView bringSubviewToFront:_staticView];
}

I guess you just have a single section,right? Before you using viewForFooterInSection,it just fills blank cells all the way down.But when you call viewForFooterInSection,tableview will put a footer at the end of the section.Since you don't have the second section,it stops draw any cells.So you can make a blank second section,it will fill blank cells to the second section.

Related

How to remove the empty section header in UITableView

I am having a UITableView in that I am having three title headers.problem is: when my title header value become empty means that header should not show in the table view. I have tried many method its not helped for me. can anyone help me please.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
You can use like this
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0])
{
return 0;
}
else
{
return 60;
}
}
In addition to Ravi's answer, make sure you set the section height property of table view to 0 in storyboard.

Nested UITableViewController does not show any cells

I've added a UITableViewController to my main view controller using method from Apple docs:
- (void)viewWillAppear:(BOOL)animated {
myTableViewController = [MyTableViewController new];
[self displayContentController:myTableViewController];
}
- (void)displayContentController:(UIViewController *)viewController; {
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
and I wanted to display some example cells in this controller:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
(I don't think I need any more configuration as it is UITableViewController, not UITableView)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = #"Under development";
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
But all I can see is just empty cells (I've also tried to change background color to make sure it's not just the while label color):
After few hours I have no idea what I did wrong. Do you have any idea what am I missing?
You have to used it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = #"Under development";
return cell;
}
Try it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return Table_Arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"subwaysurfer"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
UIImageView *imgg;
UILabel *txt;
UIImageView *Acc_img;
imgg=[[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 50)];
imgg.image = [UIImage imageNamed:[imageArr objectAtIndex:indexPath.row]];
cell.highlighted=YES;
cell.backgroundColor=[UIColor clearColor];
txt=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-80, 60)];
txt.numberOfLines = 3;
txt.text=[Table_Arr objectAtIndex:indexPath.row];
txt.backgroundColor=[UIColor clearColor];
// txt.textColor=[UIColor colorWithRed:66.0/255.0 green:51.0/255.0 blue:57.0/255.0 alpha:1.0];
txt.textColor=[UIColor colorWithRed:44.f/255.f green:78.f/255.f blue:134.f/255.f alpha:1];
txt.font=[UIFont fontWithName:#"AvenirNextCondensed-Medium" size:16.0];
Acc_img=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 20, 20, 20)];
Acc_img.image=[UIImage imageNamed:#"arrow.png"];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
imgg.frame = CGRectMake(10, 5, 80, 80);
txt.frame = CGRectMake(120, 10, self.view.frame.size.width-160, 70);
Acc_img.frame = CGRectMake(self.view.frame.size.width-80, 25, 40, 40);
txt.font=[UIFont systemFontOfSize:28.0];
}
[cell addSubview:imgg];
[cell addSubview:txt];
[cell.contentView addSubview:Acc_img];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// fixed font style. use custom view (UILabel) if you want something different
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
return 90;
}
else{
return 60;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
}
Ok, problem solved. It appears, that even using UITableViewController which has built-in property tableView it is necessary to initialise the tableView (or at least it did help me).
self.tableView = [UITableView new];

UITableView bottom separator inset - Strange behaviour in IOS 7

My app contains a UITableView which has section footers. When the user scrolls to the bottom of the tableView, sometimes a separator inset appears between the last cell and the tableFooter. This behaviour is inconsistent.
Is there a way to force this separator to always appear or never appear? Did any of you noticed this same inconsistent behaviour? Seems like a bug to me.
EDIT
I'm using
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UILabel *footer = [[UILabel alloc] init];
footer.backgroundColor = [UIColor whiteColor];
footer.font = [footer.font fontWithSize:15];
footer.textAlignment = NSTextAlignmentCenter;
return footer;
}
but you could easily reproduce the same problem only using
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
To do this you'll have to override the standard UIView used as the footer.
You can do this by overriding the delegate method -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section.
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *myFooterView;
// create a view with a label and without a line at the top etc...
return myFooterView;
}
I am not sure why you are getting inconsistent results. Maybe try to add a background to your footer view. Here is my code:
-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 33)];
[footerView setBackgroundColor:[UIColor grayColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, footerView.bounds.size.width, 20)];
label.text = #"Footer";
[label setTextAlignment:NSTextAlignmentCenter];
[footerView addSubview:label];
return footerView;
}
And here is the result:
This is a bit of a lazy way of doing it but it works none the less.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifyer"];
if (indexPath.row == tableArray.count) {
cell.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return cell;
}
here is the solution, very easy!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifyer"];
cell.separatorInset = (indexPath.row == totalNumber-1) ? UIEdgeInsetsMake(0, INT16_MAX, 0, 0) : UIEdgeInsetsZero;
return cell;
}
I found some workaround about it:
Whenever you expect this extra separator could appear (for example, when user scrolls to the bottom like you described) - add these lines of code:
tableView.separatorStyle = UITable​View​Cell​Separator​Style​None;
tableView.separatorStyle = UITable​View​Cell​Separator​Style​Single​Line;
The logic is something like that: first string of code removes bottom separator, and the second one doesn't add it. It worked for me in some cases, but it is not a 100% fix.
I guess, it might be an Apple bug also, as sometimes bottom separator disappears in their 'Reminders' app.

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

How I can Fixed Tableview to Screen

I added Tableview on xib file(you can see on image).tableview is loading well . But last cell is out of screen so when I Swipe Up last index is showing. When I get off my hand, last cell is not appear. I don't change any height of tableview . Why not fixed to my screen ?
I am using reveal menu like facebook in this project : https://github.com/mikefrederick/MFSideMenu
Also You can see problem on movie.
https://www.dropbox.com/s/usfwdhl5w9znkl6/IMG_0006.MOV
viewcontroller.h
#property(nonatomic,retain)IBOutlet UITableView * tableview;
viewcontroller.m
-(void)viewDidAppear:(BOOL)animated
{
self.tableview.backgroundColor=[UIColor colorWithRed:(28/255.0) green:(28/255.0) blue:(28/255.0) alpha:1];
[self.tableview setSeparatorColor:[UIColor blackColor]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0)
{
return 5;
}
if (section==1)
{
return 3;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =#"exampleCell";
NSLog(#"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 1)
return 40;
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.tableview.layer.cornerRadius = 10.0f;
tableView.layer.borderWidth = 2.0;
tableView.layer.borderColor = [UIColor redColor].CGColor;
return 60;
}
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor =[UIColor colorWithRed:(41/255.0) green:(41/255.0) blue:(41/255.0) alpha:1];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//end of loading
//for example [activityIndicator stopAnimating];
}
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor colorWithRed:(112/255.0) green:(112/255.0) blue:(112/255.0) alpha:1]];
UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
titleLabel.text = #"MÜŞTERİ ALANI";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:titleLabel];
return headerView;
}
In your video, this menu appears to be a slide out menu, presumably using some third party slide out menu controller library. So this view controller is contained within some container view from that. It is possible that the slide out menu controller isn't properly sizing your view controller to fit its container view.
One solution would be to check any example apps that come with the slide out controller you're using to see if they suffer from the bug and report this to the developer if it is the case. This would be good because other developers would benefit from the improvements. In fact it's possible the slide out controller you're using has fixed this bug with a newer version already that you don't have yet.
Another solution, assuming there's an example app that doesn't suffer from this, is to see how it is adding its slide out menu and see if you're failing to do something that they're doing.
Lastly, if there's no example app or you can't figure out why theirs is working differently, try adding the following to your view controller's viewDidAppear: method:
self.view.frame = self.view.superview.bounds;
This assumes that the container view they create is properly sized.
Your problem seems to be in your xib.
You have to set the size of your View to "Freeform" then add autosizing constraints on your view like you did on your tableView.
first you should select the CELL, not the TABLE VIEW. Next, go to the attribute inspector and click the separator, and you should select the custom inserts. Lastly you can adjust the separator line according to your wish. Hope this will help you and others. Thanks for the question too.

Resources