Set position of the header at the beginning of the TableView - ios

I have to set my headerView at the beginning of the table View.
It seems to be fixed please help.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,40)] autorelease];
headerView.backgroundColor = [Utility consumerLightColor];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 250, headerView.frame.size.height)];
headerLabel.text = #"Upcoming Appointments";
headerLabel.textAlignment = NSTextAlignmentLeft;
headerLabel.textColor = [UIColor whiteColor];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:15.0f];
}
else{
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:16.0f];
}
// // NSLog(#"title --> %#",[locationArray objectAtIndex:i]);
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (tableView == appointmentListtableView) {
CGFloat changeheight = 40.00;
return changeheight;
}
else{
return 0.0f;
}
}
All I want is to set the position of the header view (Upcoming Appointments)at the beginning of the table View and it should not be fixed.

Maybe you need to remove table header , not section header ?
To do this you need to set:
tableView.tableHeaderView = nil

See keep always in mind,
If you want to set Static header to table view then its good & best practice to set it as a ,
yourTableView.tableHeader = yourHeaderView;
If your headers are dynamic you don't know weather how much sections want to set, then go with method,
viewForHeaderInSection:tableView
That doesn't mean you can use only one at a time not at all, You can use both at a time also,
Now comes to your case, i think you are doing something wrong in the tableViewHader, set it to nil if you don't need anymore,
But i am curious about one thing even if you have set tableViewHeader still why does it show upper & lower side of your secion header,You are setting tableViewHeader or sectionFooter or showing the UITableViewCell

Related

Label in UICollectionReusableView displayed only in even indexes

I have run into a very odd problem with UICollectionReusableView. My footer's are displaying correctly but the problem is with my headers.
My headers are a subclass of UICollectionReusableView and contain:
A UILabel on the left side
A UILabel on the right side
Both labels are initialized the same way with very similar properties in the initWithFrame method which is being correctly called.
Here is an example code section:
- (id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
self.backgroundColor = [UIColor whiteColor];
titleLabel = [[UILabel alloc] initWithFrame:CGRectInset(frame, 10, 0)];
titleLabel.font = [UIFont boldSystemFontOfSize:24];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
[self addSubview:titleLabel];
dotCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * frame.size.width / 3 - 5, 8, frame.size.width / 3, frame.size.height)];
dotCountLabel.textAlignment = NSTextAlignmentRight;
dotCountLabel.textColor = [UIColor blackColor];
dotCountLabel.backgroundColor = [UIColor clearColor];
[self addSubview:dotCountLabel];
}
return self;
}
The second label is being displayed without any problem but the first one in odd indexed sections is displayed on top of the label in the next section. For any of view that would like to see how I create these views:
SaveHeader *header = (SaveHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:#"saveHeader" forIndexPath:indexPath];
Pattern *pattern = [saveModel.savedPatterns objectAtIndex:indexPath.section];
header.titleLabel.text = pattern.title;
header.dotCountLabel.text = [NSString stringWithFormat:#"%i dots", pattern.dotCount];
return header;
I have worked with these a bunch of times before and I have never run into anything like this. Anyone have an idea why this happening and how it could be fixed? Also note I have ran a debugger through it and the data object is returning the correct data. I am using iOS 7.1.
Before your initWithFrame calls, store the CGRect that you are about to init with into a variable and examine it. Look closely at the values. You'll likely find your bug there. Good luck!

UITableView extend different background color below cells

I have a UITableView that I have given a headerView that has a green background. I have also given the UITableView that same green background color so that when the tableView is pulled down it feels like a seemless color above the tableView. Works perfectly.
The problem is my tableView only has 4 rows, and below that it shows the green color again.
How can I show white below the rows instead so that I only see the green background color when pulling down the tableView?
You can add an extra top view, same as in this answer to UIRefreshControl Background Color:
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.backgroundColor = [UIColor greenColor];
[self.tableView insertSubview:bgView atIndex:0];
You could add a fifth cell with no content, and make it, say 400 points high, and limit the size of the table's contentView so that you can't scroll to the bottom of that cell.
-(void)viewWillLayoutSubviews {
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, 500);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat h = (indexPath.row == 4)? 400:44;
return h;
}
Putting the contentSize setting in viewWillLayoutSubviews ensures that it will be reset to that value if you rotate your device.
as I said in these comments, in this way the headerView will be under the navigationBar and if you pull down the tableview you will see the headerView green. So you can set white color for tableView:
(i tested it and work)
-(void) viewDidLoad {
self.tableView.backgroundColor = [UIColor whiteColor];
headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor greenColor];
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, 320, 120)];
lab.backgroundColor = [UIColor greenColor];
[headerView addSubview:lab];
self.tableView.tableHeaderView = headerView;
}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 80;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
Same as the accepted answer - in Swift 5:
var frame : CGRect = self.tableView.bounds
frame.origin.y = -frame.size.height
let bgView : UIView = UIView.init(frame: frame)
bgView.backgroundColor = UIColor.green
self.tableView.insertSubview(bgView, at: 0)
Also add the following code (right after the above code) to make sure it works for other sized devices:
bgView.translatesAutoresizingMaskIntoConstraints = true
bgView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth]

Is there a way to change the font color of all instances of UITableview's header

The new iOS 7 has changed the default font color of the section headers in tableview. The problem is that my background image makes the text hard to read. I know I could change my background but I would like to change the color of all the textviews. I have changed the uinavigationbar color in my apps delegate before. I would like to use something like that for tableview if possible. I have read this method:
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}else{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 8, 320, 16);
label.textColor = [UIColor whiteColor];
label.text = sectionTitle;
UIView *view = [[UIView alloc] init];
[view addSubview:label];
return view;
}
My problem with this is that I would have to implement it on a per tableviewcontroller basis. Also when using this I'm not sure how to prevent the text from going off the page and being unreadable.
Any suggestions would be great. Thanks in advance.
EDIT: I have decided to add a little clarification just for show using some suggested code here remains my problem with this solution.
EDIT: To accompany answer. I found that this is also needed to create the space for multiple lines for header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == ?) {
return 60;
}
else{
return 44;
}
}
You must use following method to change your headerview :
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,yourWidth,YourHeight)] ;
headerView.backgroundColor = [UIColor colorWithRed:0.5058f green:0.6118f blue:0.8078f alpha:1.0f];
tableView.sectionHeaderHeight = headerView.frame.size.height;
tableView.tableHeaderView.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13,320, 22)] ;
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
// Chnage your title color here
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.numberOfLines = 2 ;
[headerView addSubview:label];
return headerView;
}

Determine custom section header frame when tableView mode grouped

I provide a custom section header via the tableView's delegate method. It works fine in plane mode but I can't figure out the correct margin in grouped style.
Does anyone know how to make the section header aligned with the tableView cells?
You can create a custom label and adjust its frame accordingly.
For eg.
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] initWithFrame:aTableView.tableHeaderView.frame];
UILabel *label=[[UILabel alloc] initWithFrame: CGRectMake(//set frame of the label as you want)];
[label setFont:[UIFont fontWithName: size:]];
[label setTextColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[label setShadowColor:[UIColor redColor];
label.backgroundColor=[UIColor clearColor];
if(section==0)
{
[label setText://set your section0 title];
}
else if(section ==1)
{
[label setText://set your section1 title];
}
[view addSubview:label];
[label release];
return [view autorelease];
}
Hope this helps :)
Not tested in every flavor but a good starting point.
In
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
pass the tableView.style parameter to your own method providing the header.
Create a view a frame like
CGRect(0,0,CGRectGetWidth(tableView.frame), CGRectGetHeight(tableView.frame)
but add a subview with a frame
CGRectInset(frame, 30,0)
if you have a grouped tableView style. Set a the autoresizingMask to flexible width and it works.
Slightly modified in a way that I had to give an additional parameter to the creation method for the SectionHeaderView as the margin is different for presentationStyle FullScreen and Formsheet.
CGFloat margin = 0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
margin = style == UITableViewStyleGrouped ? modalStyle == UIModalPresentationFormSheet ? 28 : 38 : 0;
}
else {
margin = style == UITableViewStyleGrouped ? 5 : 0;
}
UIView *view = [[UIView alloc] initWithFrame:CGRectInset(frame, margin, 0)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:view];

Adding image next to a Title Header in UITableView

I have created a UITableView using Group-style. I only have a single group in my table.
Here are my two questions:
1) How do I display a picture next to my title in the top header? Basically I would like to put the logo of our company next to that title.
2) Can I change the background color (I don't like the grey one) in the header and can I adjust the height of that header?
Just return a custom view to your header this way:
//Draws a custom view for the header instructions
-(UIView*)tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger)section;
{
if ([tableView numberOfRowsInSection:section] != 0) // To handle nil entries
{
UIView* headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)] autorelease];
// set up whatever view you want here. E.g.
UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, headerView.frame.size.width - 20.0, headerView.frame.size.height)];
headerLabel.text = #"Tap items in the list to pin them.\nTap the Remix button to clear all unpinned items";
headerLabel.numberOfLines = 0;
headerLabel.font = [UIFont fontWithName:#"HelveticaNeueLTStd-Lt" size: 14];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed:kColorRed green:kColorGreen blue:kColorBlue alpha:1];
headerLabel.userInteractionEnabled = NO;
headerLabel.textAlignment = UITextAlignmentCenter;
headerView.backgroundColor = [UIColor clearColor];
[headerView setTag:010];
[headerView setAlpha:1];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
else {
return nil; // again to handle nil tables. You should be nice and at least show an error label on the view.
}
}
And this gives you something like this:

Resources