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 #"";
}
Related
I have a tableView with 2 sections, the sections array is fixed with 2 elements. The headers for the sections showed fine as code shows below in Xcode 7. I just upgraded to Xcode 8, and the section headers don't show anymore, the code below doesn't get called anymore.
Any ideas?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:16]];
NSString *date =[sections objectAtIndex:section];
/* Section header is in 0th index... */
[label setText:date];
[view addSubview:label];
[view setBackgroundColor:[DeviceHelper Orange]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
Use this method as it is, you will see the header
NB : set the delegate and datasource of tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *reuseId = #"reuseid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
}
cell.textLabel.text = #"Test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:16]];
NSString *date = #"22nd Sept 2016";
/* Section header is in 0th index... */
[label setText:date];
[view addSubview:label];
[view setBackgroundColor:[UIColor orangeColor]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/25
return view;
}
Make sure you have heightForHeaderInSection in addition to your viewForHeaderInSection function
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
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;
}
}
I am having a table view, in that I am having title headers, they are working well, but the sum of the title header values are getting null, I want to hide that null title header in my table view. how can I do this process. please help me in coding.
- (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;
}
I am getting like above image,i ant to hide the null value title headers.
Make changes in heightForHeaderInSection is return 0 if no objects
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return 60.0;
else
return 0.0;
}
Also return view nil if not found any objects in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return nil;
else
{
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;
}
}
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.
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;
}