Setting header for only one uitableview - ios

I need help. I have three UITableViews in one UIView. However, I only want one UITableView to have a header. How can I say it in code? I already have the following code. Thanks guys.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == Table1) {
return #"My Header Text";
} else if (tableView == Table2) {
return nil;
} else if (tableView == Table3) {
return nil;
}
}

The code you provided will be add a header view for a certain section only. So If you have two or more section then you need to write extra logic to set the header view for each section.
Here it is an easy way to set the tableHeaderView for single table is.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = #"my table header";
table1.tableHeaderView = label;

If that's not working you could also try to return zeroes for header's heights as well:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (tableView == Table1)
{
return 50.0; // or what's the height?
}
else
{
return 0.0;
}
}

Try this
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view addSubview:label];
return view;
}

Related

why view for header in section does not appear properly when the tableview style is grouped in iOS, objective c

I'm working with a tableview which has four sections. for each section I have implemented a header view. when I use the tableview style as Plain it works properly. but if I use the tableview style as Grouped it looks wired.
this is how I implement the tableview delegate methods.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
}
else if(section == 1)
{
return 1;
}
else if (section == 2)
{
return 3;
}
else
{
return 1;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 50)];
UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, screenSize.width-80, 50)];
if (section == 0)
{
titlelabel.text = #"Flight Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if(section == 1)
{
titlelabel.text = #"Price Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if (section == 2)
{
titlelabel.text = #"Traveller Details";
[headerView addSubview:titlelabel];
return headerView;
}
else
{
titlelabel.text = #"Book Now";
[headerView addSubview:titlelabel];
return headerView;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"checkcell"];
return cell;
}
this is the screenshot when I use tableview style as Plain
link to image : image with plain style
this is the screenshot when I use tableview style as Grouped
link to the image : image with grouped stye
I have no idea what is going on, hope your help with this.
It requires to set the section header height. Try the below way, Hope this helps you.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50
}

How to hide header if string/object is null in UITableView

I want to hide header and its space if if string/object is null in UITableView. Here is my coding. In this code, if object is null, header space remains there, but it shouldn't.
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
NSString *compname;
if ([compObj.compName isEqualToString:#""]) {
[self NochangeHeight];
return nil;
} else {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, tableView.frame.size.width, 36)];
if ([compObj.clientList count] != 0) {
compname = compObj.compName;
[label setFont:[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:25.0f]];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[label setText:compname];
label.backgroundColor = [UIColor orangeColor];
[view setBackgroundColor:[UIColor orangeColor]];
[view addSubview:label];
[self changeHeight];
}
return view;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return headerHeight;
}
- (void)changeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 50.0;
[_clientListGroupTableView endUpdates];
}
- (void)NochangeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 0.0;
[_clientListGroupTableView endUpdates];
}
You have to return a different value in the heightForHeaderInSection: method instead.
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}
You can make the height 0 if it is "", rather than controlling in viewForHeaderInSection.
The method viewForHeaderInSection arrange the view of header according to what you are returning. But it does arrange on to the existing header. So header is always there, and you are just making labels, texts etc. onto that header. As a consequence, when you say it to be "nil", since it is just what should be on the header, programme will understand it as "There is nothing to put on to the header". Thus, header will just be an empty space.
In order not to see header, you should change the height, within the method heightForHeaderInSection. So if you implement also that method as:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}

How can i round corners of container table view?

I have a main view controller. It contains a table view controller ( in a container ) , i want to round out the tableview so it shows similarly to the facebook login one.
Code i have used in the child view controller ( which is a tableview controller ) in viewDidLoad :
self.tableView.layer.cornerRadius = 10.0f;
self.tableView.layer.masksToBounds = YES;
self.tableView.clipsToBounds = YES;
self.tableView.backgroundColor = [UIColor clearColor];
Result :
As you can see , when the field is selected , the corners SEEM rounded , but the white space remains. How can i make them rounded even when it isn't selected ?
Using : XCODE 5 , IOS 7
As you requested me to do, here is the code you wanted:
for (CALayer *subLayer in self.tableView.layer.sublayers)
{
subLayer.cornerRadius = 10;
subLayer.masksToBounds = YES;
}
Try this
#interface CustomtableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{
UITextField * username;
UIButton * submit;
}
#implementation CustomtableViewController
- (void)viewDidLoad
{
UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(10, 70, 300, 45)];
submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[submit setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[submit setTitle:#"Login" forState:UIControlStateNormal];
[submit.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[submit setFrame:CGRectMake(10.0, 15.0, 280.0, 44.0)];
[newView addSubview:submit];
[self.tableView setTableFooterView:newView];
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//self.tableView.contentOffset = CGPointMake( 10, 320);
[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
username = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
username.adjustsFontSizeToFitWidth = YES;
username.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
username.placeholder = #"example#gmail.com";
username.keyboardType = UIKeyboardTypeEmailAddress;
username.returnKeyType = UIReturnKeyNext;
cell.textLabel.text = #"Username";
username.clearButtonMode = YES;
}
else {
username.placeholder = #"minimum 6 characters";
username.keyboardType = UIKeyboardTypeDefault;
username.returnKeyType = UIReturnKeyDone;
username.secureTextEntry = YES;
cell.textLabel.text = #"Password";
username.clearButtonMode = UITextFieldViewModeAlways;
}
username.backgroundColor = [UIColor whiteColor];
username.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
username.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
username.textAlignment = NSTextAlignmentLeft;
username.tag = 0;
username.clearButtonMode = UITextFieldViewModeAlways; // no clear 'x' button to the right
[username setEnabled: YES];
[cell.contentView addSubview: username];
}
// Configure the cell...
return cell;
}
Here, i've created just two textfields for username and password. You can use the else if condition to insert any no of textfields in each of the successive rows according to your needs.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:#"User Login"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return #"";
}
So, my code here is just used for creating a login page with two textfields(Username and Password) and a Login button. You can modify my code according to your needs. Cheers!

ViewForHeaderInSection code works only on header dequeueing

I have custom tableView header, but the code where I specify header text color and separator line works only when the headers are dequeued, when I scroll them off the screen. Only then they return with white text color and visible separator. What is wrong with my code that the headers do not return in the correct state right on the first load?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return #"FIRST HEADER";
} else if (section == 1) {
return #"SECOND HEADER";
} else {
return #"THIRD HEADER";
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *myHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"header"];
if (!myHeaderView) {
myHeaderView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:#"header"];
}
myHeaderView.textLabel.textColor = [UIColor whiteColor];
CGFloat leftMargin = 0.0;
CGFloat lineWidth = 1.0;
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, myHeaderView.frame.size.height - lineWidth, myHeaderView.frame.size.width - leftMargin, lineWidth)];
separatorLine.backgroundColor = [UIColor whiteColor];
[myHeaderView addSubview:separatorLine];
return myHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
}
}
The reason is that your header's frame is equal to CGRectZero before dequeueing. It means, that header will be resized in future and you need to set appropriate autoresizing mask to separator. Then, separator will also be resized.
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, 0, 0, lineWidth)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[header.contentView addSubview:separatorLine];

how to set Section with String #"XYZ" as first section in tableview

I am populating my TableView with data from coreData.
I want to set an Section with a special String as always the first Section in the TableView.
How can i do that?
Implement this method when using with coreData:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
if(section == 0) {
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 0, 300, 40);
label.text = #"XYZ";
[headerView addSubview:label];
[label release];
}
return headerView;
}
Implement this method if you are using the built-in view from UITableView:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0) {
return #"XYZ";
}
return #"";
}

Resources