Hide TableViewHeaderView for a specific TableView section - iphone

So I have this Tableview with a few sections, (3) to be exact. I want there to be headers for sections 2 and 3, not for the first sections..
Heres what i've done:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
UIView *tempView;
tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
tempView.backgroundColor=[UIColor grayColor];
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,20)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
switch (section)
{
break;
case 1:
{
sectionName = NSLocalizedString(#"Information", #"Information");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
case 2:
{
sectionName = NSLocalizedString(#"Tools", #"Tools");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
}
return tempView;
}
I am confused on what needs to be done... Heres a pic of whats going on :

In the case of section == 0, you are still setting tempView to a new instance of UIView and returning that value, you just aren't setting the title of the label. Also as you learned you should return 0 for tableView:heightForHeaderInSection: for section 0.

So while I was writing this question I cam to a conclusion... but maybe its not the more efficient?
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerheight = 20.0;
switch (section)
{
case 0: { headerheight = 0.0; } break;
case 1: { headerheight = 20.0; } break;
case 2: { headerheight = 20.0; } break;
}
return headerheight;
}
If anyone has any suggestions on how I don't need to implement this tableview delegate method, please speak up. I feel like I just wouldnt need to return a view for the specified section rather than say a sections header is 0. But I'm not completely sure at the moment, the problem is SOLVED but maybe not solved correctly?
Heres a pic of the result of the solution.

Related

Activate View for header delegate for specific section in single table

I am developing an application in which I need header to customize and add my own button just for single section. I googled and done some code where I am able to add button, but I am facing two issue.
Titles of other's section is not showing.
Button not show properly because of tableview scroll size same after adding button.
Here is what I am doing.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
if(section==2){
float width = tableView.bounds.size.width;
int fontSize = 18;
int padding = 10;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(padding, 2, width - padding, fontSize)];
label.text = #"Texto";
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0,1);
label.font = [UIFont boldSystemFontOfSize:fontSize];
[headerView addSubview:label];
UIButton * registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[registerButton setImage:[UIImage imageNamed:#"P_register_btn.png"] forState:UIControlStateNormal];
[registerButton setFrame:CGRectMake(0, 0, 320, 150)];
[headerView addSubview:registerButton];
return headerView;
}
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0)
return #"Registration";
else if(section==1)
return #"Player Detail";
return nil;
}
Here is Image of my out put in which Texto text show but button is under that area where the end limit of table view scroll height and also section 0 and 1 title is not showing I also block code for first and second section in viewforheaderinsection. Thanks in advance.
The other header names don't appear because the viewForHeader method only answers for section 2. Once implemented, the datasource expects that method to be the authority on all headers. Just add some else logic for the other sections....
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView;
UILabel *label;
if (section==2) {
headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
// and so on
return headerView;
} else if (section == 0) {
label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width, 44)] autorelease];
label.text = #"Section 0 Title";
return label;
} else .. and so on
The header answered by this method looks to be 40px high (see initWithFrame), but the button being added is 150px high (see setFrame: for the button). That's the likely the root cause of the button issue. Try implementing:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 150.0 : UITableViewAutomaticDimension;
}

how to set text of titleForHeaderInSection to AlignmentCenter

i used the follow code to set the title at the group tableview title header,but default the text is AlignmentLeft,how to AlignmentCenter?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section==0){
return NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
return NSLocalizedString(#"more_titlehead_two", nil);
}else{
return NSLocalizedString(#"more_titlehead_three", nil);
}
}
Try like this,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel * sectionHeader = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = UITextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
switch(section) {
case 0:sectionHeader.text = #"TITLE ONE"; break;
case 1:sectionHeader.text = #"TITLE TWO"; break;
default:sectionHeader.text = #"TITLE OTHER"; break;
}
return sectionHeader;
}
set some default height for Header,
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
switch(section) {
case 0:
case 1:
default:return 20;
}
}
Use the below method to set the UIView item for the header in each section:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerVw = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
headerVw.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"title_bg.png"]]; // set color of header
UILabel *itemlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 320, 20)];
if(section==0){
itemlbl.text = NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
itemlbl.text = NSLocalizedString(#"more_titlehead_two", nil);
}else{
itemlbl.text = NSLocalizedString(#"more_titlehead_three", nil);
}
itemlbl.textAlignment = UITextAlignmentCenter;
itemlbl.backgroundColor = [UIColor clearColor];
itemlbl.textColor = [UIColor whiteColor];
itemlbl.font = [UIFont boldSystemFontOfSize:14];
[headerVw addSubview:itemlbl];
[titlelbl release];
return headerVw;
}
Best of luck
It is very simple use UILabel with its Alignment center. in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.text = #"Header Title";
return lbl;
}

Datasource for UITableViews in UIScrollView

I'm working on the project using several UITableViews being put vertically next to each other. I got to show those tables, but got stuck when I tried to show different table contents for each table.
This is the code:
-(void)setTheTable{
int numberOfTables = 5;
//height of the table
CGFloat height = self.view.bounds.size.height;
CGRect tableBounds = CGRectMake(0.0f, 0.0f, self.widthOfTheScreen, height- 30);
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, self.widthOfTheScreen, height - 30.0f)];
self.scrollView.contentSize = CGSizeMake(self.widthOfTheScreen * numberOfTables, height-30);
self.scrollView.pagingEnabled = YES;
self.scrollView.bounds = tableBounds;
[self.view addSubview:self.scrollView];
//put five tables
CGRect tableFrame = tableBounds;
tableFrame.origin.x = 0;
for (int i = 0; i < numberOfTables; i++) {
UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
switch (i) {
case 0:
tableView.backgroundColor = [UIColor yellowColor];
tableView.tag = 0;
cell.textLabel.text = #"section = 0 行0";
break;
case 1:
tableView.backgroundColor = [UIColor blueColor];
tableView.tag = 1;
break;
case 2:
tableView.backgroundColor = [UIColor brownColor];
tableView.tag = 2;
break;
case 3:
tableView.backgroundColor = [UIColor greenColor];
break;
case 4:
tableView.backgroundColor = [UIColor cyanColor];
break;
default:
break;
}
[self.scrollView addSubview:tableView];
tableFrame.origin.x += self.widthOfTheScreen;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
int num = tableView.tag;
if(num == 1) {
if(indexPath.row == 0) {
cell.textLabel.text = #"section = 0 行0";
} else if(indexPath.row == 1){
cell.textLabel.text = #"section = 0 行1";
} else {
cell.textLabel.text = #"section = 0 行2";
}
} else {
NSString *str = [[NSString alloc] initWithFormat:#"section = %d col %d",indexPath.section,indexPath.row];
cell.textLabel.text = str;
}
return cell;
}
I thought tableview.tag would help but gave me 5 same tables.
Could you guys give me some ideas to solve this problem?
Thank you.
First of all, see this answer about adding a UITableView to a UIScrollView, Apple specifically warns against it (though they did not include the warning in the UITableView docs, not your fault). Add your tables as subviews to a UIView NOT a UIScrollView (I recommend getting rid of that entriely).
Second, you only create a UITableViewCell in the tableView:cellForRowAtIndexPath: method. Remove all references to any UITableCell from your for-loop entirely (those are not even being used, it is work for nothing). After you have set the table's data source and delegate, call [tableView reloadData] on each able, the data source methods will be called automatically.
Since you are working with multiple tables, things get a bit more tricky. You will need to keep a reference to all your table views in your class (either in an array, but ideally properties for each table view).
You will also need to know what table is being loaded at any given time, e.g.:
-(UITableViewCell*)tablView:tableView cellForRowAtIndexPath:indexPath {
UITableViewCell *cell;
if ( tableView == self.tablView1 ) {
// build your cells for table view 1
}
else if ( tableView == self.tableView2 ) {
// build cells for table view 2
}
return cell;
}
With multiple tables, you would probably want to consider a specific delegate for each table, especially if the data models behind each table is vastly different, and/or each the UI for each table are different.
I do not know your exact needs, but also consider a table with multiple sections instead of multiple tables.
I also encourage you to read the iOS TableView Programming Guide, and this on Delegates and Data Sources.

sectionName TableView - What am I doing wrong?

I'm having some trouble changing the colour and font on a TableView that I have split into 4 sections with names etc., I can;t seem to get it to work I'm not sure what I am doing wrong?
- (NSString *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:#"Date"];
break;
case 1:
sectionName = [NSString stringWithString:#"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:#"City"];
break;
case 3:
sectionName = [NSString stringWithString:#"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionName;
}
you should return the view instead of the string...
This is what you are returning
return sectionName;
And this is what you should return..
return sectionHeader;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
is the right method for presenting a view in header of UITableView. Here you can return your UILabel object.
You are trying to return a NSString object instead of UILabel. Also the return type of method is wrong. It should be of the type UIView.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:#"Date"];
break;
case 1:
sectionName = [NSString stringWithString:#"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:#"City"];
break;
case 3:
sectionName = [NSString stringWithString:#"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionHeader;
}
The code shows you how to implement viewForHeaderInSection delegate method of TableViewCOntroller which you need to implement to accomplish what you need. your code returns NSString, which is supposed to return UIView.

UITableView custom section header appears below the cells

I've a custom section header in my UITableView and I can't figure out why they are appearing bellow the UITableViewCell of the table. See the screenshots:
This is the code that creates the section header:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
return [LojaInfoHeaderView lojaInfoHeaderForSection:section withTitle:sectionTitle opened:[self sectionIsOpen:section] andDelegate:self];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return [LojaInfoHeaderView viewHeight];
}
And the section's cell are inserted or deleted when the user touches the section header:
- (void)lojaInfoHeader:(LojaInfoHeaderView *)lojaInfoHeader sectionDidOpen:(NSInteger)section {
NSArray *indexPathsToInsert = [self indexPathsForSection:section];
[self setSection:section open:YES];
[_tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop];
}
- (void)lojaInfoHeader:(LojaInfoHeaderView *)lojaInfoHeader sectionDidClose:(NSInteger)section {
NSArray *indexPathsToDelete = [self indexPathsForSection:section];
[self setSection:section open:NO];
[_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
}
How can I make the section header appears above the cells? How to fix it?
Update to show how things are created
These are the class methods I'm using:
+ (CGFloat)viewHeight {
return 44.0;
}
+ (LojaInfoHeaderView *)lojaInfoHeaderForSection:(NSInteger)section withTitle:(NSString *)title opened:(BOOL)isOpen andDelegate:(id<LojaInfoHeaderDelegate>)delegate {
LojaInfoHeaderView *newHeader = [[[LojaInfoHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
newHeader.section = section;
[newHeader setTitle:title];
newHeader.delegate = delegate;
[newHeader setOpen:isOpen animated:NO];
return newHeader;
}
I found the problem. I was setting the backgroundColor using alpha (yeah, I can't believe I miss this).
Wrong code in initWithFrame:
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
Correct code:
self.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.89 alpha:1.0];
Try to change your whole table style to be grouped instead of plain. Or change your section view to be opaque. Whatever is the design requirement.