I am trying to display data in a UITableView, and the first row will be a label to identify the type of data. How can I keep the first row as a label when scrolling down?
Thanks!
You can do that with the TableView header. Here is an example:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"blablabla", #"");
headerLabel.backgroundColor = [UIColor clearColor];
[header addSubview:headerLabel];
myTable.tableHeaderView = header;
Use the header's of table views to do this specifically. All the functionality is already built into the UITableView class. In the UITableViewDataSource, just implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
I think one way you can implement it is by using the tableHeaderView property of the UITableView. Make the tableHeaderView look like the first row of your table and begin the table from the 2nd row.
Related
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
}
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 do I fix the problem illustrated in the image?
The section header for the tableview is missing an inset.
You probably set the separator insets to 0, either in code or in the Interface Builder (can be found in the Attributes inspector:
This also causes the titles to have no inset. The default values are 15 for left and 0 for right.
[Could you post your code of UITableViewDelegate?
In UITableView, there is no API for you to set this insets in section header, so you could return a custom UIView in tableView:viewForHeaderInSection: then set the layout you want.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [UIView alloc] init];
UILabel *headerLabel = [UILabel alloc] init];
headerLabel.text = #"xxx";
[headerLabel sizeToFit];
headerLabel.frame = CGRectMake(20, 0, CGRectGetWidth(headerLabel.frame), CGRectGetHeight(headerLabel.frame));
[headerView addSubview:headerLabel];
headerView.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), CGRectGetHeight(headerLabel.frame));
return headerView;
}
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];
}
}
I'm fairly new to the native app dev world and predominately a Front-End Dev/designer - I have built a IOS7 app in Xcode 5 - which contains a UITable with several custom cells. I would like to add margin of approx 8px to each of the cells (to look like the image below) - and change the colour of the link arrow which appears via a push segue - but have no idea how to do either despite a good web search / book read - theres doesnt seem to be the relevant options on the storyboard.
Can anyone advice if possible?
the code goes like this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for add margin of each cell (this code add margin only to top of each cell):
UIView *separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 8)];
separatorLineView.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:separatorLineView];
and for color of arrow you have to create an image and insert it with this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 7, 11)];
[label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourimage.png"]]];
cell.accessoryView = label;
You can include a margin to the cell by resizing the table itself. Instead of the standard 320 pixels width, change the width of the table to 312. That will give you an overall margin without having to meddle with the internals of the table view.
CGFloat margin = 8;
self.tableView.frame = CGRectMake(0, 0, self.view.frame.width-margin, self.view.frame.height);
In order to change the color of the arrow, you have to change what's called the accessoryView of the UITableViewCell. It can be any UIView.
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coloredArrow.png"]];