UITableView first section hidden by viewForHeaderInSection - ios

I have a UITableView which is a grouped table view. I am trying to add an image as the header of the table view (and only for the first section). The problem I'm having is that the image is covering the first section of the tableview.
Here's the code:
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 80)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.eventsforacausesf.com/uploads/8/7/3/0/8730216/3416277_orig.jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
imageView.image = [UIImage imageWithData:imageData];
[headerView addSubview:imageView];
if (section == 0) {
return headerView;
}
return nil;
}
For example, in the image below, the first section of the tableview is hidden behind the image. I need to move that section below the image.

What you're setting with that data source method is a header for a particular section of the table view, but it sounds to me like what you want is to set the tableViewHeader of the table view itself. Set your table view's tableViewHeader property to your header view instead of returning the header view as a section header. Then you'll get the standard iOS header vs. content layout.

You also need to implement tableView:heightForHeaderInSection: to return the height of the image view.

Related

How to show a footer view with custom text at the end of the UItableview

I have a UITableView to display search results. The user loads more results by scrolling to the end. When server has no more results, I want to show a text at the end of the tableview, that says "No more search results". I tried following code, but it is not working. Any suggestions on what is happening will be greatly appreciated.
UILabel *endLabel = [[UILabel alloc] init];
endLabel.text = #"No more results to display";
[endLabel sizeToFit];
self.tableView.tableFooterView = endLabel;
[self.tableView reloadData];
Following code may help you to solve out problem.
You can write this code to your viewDidLoad.
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
[footer addSubview:yourlabel];
[footer bringSubviewToFront:yourlabel];
yourlabel.text = #"whatever you want to write";
yourlabel.frame = CGRectMake(self.view.frame.size.width/2 - yourlabel.frame.size.width/2 , 30, yourlabel.frame.size.width, yourlabel.frame.size.height);
tbl_view.tableFooterView = footer;
However you need to increase the size of footer view of table.
I. Create a separate view in the xib of your viewController. By separate view I mean do not drag the view in the viewController's view as a subview, instead just drag it into the whitespace in your Interface Builder.
II. Now design the view i.e put a label in it that says no more data.
III. Now create an outlet of that view in your viewController.
#property(nonatomic, strong) IBOutlet UIView * tableFooterView;
IV. When there is no more data just write
self.tableView.tableFooterView = self.tableFooterView;
V. Set the width & height of the footer in viewDidLoad
self.tableFooterView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 40);
VI. And when you do have data and don't want to show the no more data thing then
self.tableView.tableFooterView = nil;
//custom header for footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UILabel *endLabel = [[UILabel alloc] init];
endLabel.text = #"No more results to display";
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 40)];
[footerView addSubview:endLabel];
return footerView;
}
//Method To Get Height For Header and Footer
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40.0;// your view height
}

Custom View with dynamic Height as UITableView Header ios xcode

In my project,i have a 1.MovieplayerView,2.a label with dynamic content,3.a tableView with variable number of rows.
I was doing this with all these views in scrollView.But i always have the issue with dynamic height of the label.it sometime overlaps the tableView.
I came to know that we can use customView as the tableView header.How this can be done with variable content Height and autolayout?I am new to iOS.Any suggestion ??
I know how to add a view as the header to a table.But when the contents in the view changes,it overlaps the contents of the tableView.
I went through
How to resize superview to fit all subviews with autolayout?,How do I set the height of tableHeaderView (UITableView) with autolayout?
Can some one give a simple example on how to do this?Or tell me if it is better to use a scrollview and add all these views as its subviews? any suggestion would be realy helpful.Thanks.
In viewDidAppear,
1. Get the height of the dynamic content, then set the height of your tableHeaderView accordingly.
2. Set the headerView again so the table view can refresh:
self.tableView.tableHeaderView = headerView
Try using following code is this what u want to achieve can u clearify me please
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 200, 40)];
NSString *string = [NSString stringWithFormat:#"/ %#",selectedCategory];
label.font = [UIFont fontWithName:#"Helvetica" size:15.0];
label.textColor = ThemeColor;
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];
return view;
}
Don't forget to place this in nib of UITableView
It's hard to interpret your question regarding your use of the scroll view. If what you want is a custom view as the table view header, you could override this method in your table view class:
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section

How can I insert an imageView behind a UITableView in a fixed position (no scrolling)?

I am trying to insert an image behind my table view using:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.tableView addSubview:imageView];
[self.tableView sendSubviewToBack:imageView];
This seems to work but the image scrolls with the table. Is there anyway to keep it fixed in the bottom right corner?
For creating a static tableview background, you have to do 2 things
1) set self.tableView.backgroundColor = [UIColor clearColor];
2) and then initialise the table background view with an UIImageView and set the image you want to show there.
[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]] ];
Your image scrolls because it has been added in the tableview content and when you scroll the tableview, all the content scrolls.
But you can add the image in the superview of your tableView, behind it.
For example, if the tableView is in self.view you can try :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.view addSubview:imageView];
[self.view bringSubviewToFront:self.tableView];
(Make sure that the tableView has a clear backgroundColor.)

How to dynamically set background color for UITableView section headers?

I'm working on implementing UITableView section headers similar to the Photos app on iPhone. The top header view should have a grey background and the other header views should have a white background. Based on that I have a couple of questions I need help with:
How do I find out which section header is on top (e.g. adjacent to the nav bar)? Note, that the navigation bar is translucent (meaning it displays cells below itself), so I can't go by finding visible cells and then retrieving section based on that.
What should be the approach to change background color of the top section header to a different color and then back to the original color when it's no longer a top one?
Make the property NSUInteger sectionPath and initialize it with 0 in viewDidLoad.
Create a headerView and for specific section change backgroundColor of that view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
headerLabel.textAlignment = NSTextAlignmentCenter;
headerLabel.text = [titleArray objectAtIndex:section];
if(section == sectionPath) {
headerLabel.backgroundColor = [UIColor grayColor];
}
else {
headerLabel.backgroundColor = [UIColor whiteColor];
}
[headerView addSubview:headerLabel];
return headerView;
}
To dynamically find the top-most header in UITableView during scrolling
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows]) {
sectionPath = [i indexAtPosition:0];
}
}

SegmentControl doesn't move with TableView scroll

The scene currently in view is "RunTestViewController". As you can see there is a segment control embedded.
Everything runs fine, the segmentcontrol works, however when I scroll the segment control stays put and end up on top of the cells.
Is there a way embed the segment controller into the tableview so it moves with the table?
Thanks,
**EDIT:
------------------**
I have added this code in the viewDidLoad section:
UIView *headerView = [[UIView alloc] init ];
[headerView addSubview:resultsSegment];
self.tableView.tableHeaderView = headerView;
This allows the segmentcontrol to move when the table view scrolls, which is great, however the segment control is now unclickable, any ideas?
You want to embed a view or control into a tableview so that it scrolls with the tableview? You should put the view or control into the tableview header.
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
[headerView addSubview:segmentControl];
self.tableView.tableHeaderView = headerView;

Resources