UITableView section header appearing over table header view - objective-c

I'm creating a custom view for both a section header and a table header ofa UITableView. The problem is that the section view (in green) is appearing over the table header view (in red)
The code for the table header view is
self.headerImageView = [[UIView alloc] init];
self.headerImageView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = self.headerImageView;
and i set its frame here
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.headerImageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 100);
}
The code for the section header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor greenColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
What am I doing wrong?
cheers

Just put the code all in -(void)viewDidLoad, like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.headerImageView = [[UIView alloc] init];
_headerImageView.backgroundColor = [UIColor redColor];
_headerImageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 100);
self.tableView.tableHeaderView = _headerImageView;
}
I found that -(void)viewDidLayoutSubviews will be called many times while scrolling the table. Maybe this is the problem.

Related

iOS7 UITableViewStyleGrouped section header font style name?

All the answers I've seen so far are basically for pre-iOS7 (using systemFontStyle or boldSystemFontStyle). That doesn't correspond to the default capitalized section title font in UITableViewStyleGrouped.
I've had to implement the UITableViewDelegate method that return the custom section header view and want to know the font Apple uses to capitalize section titles.
For iOS7+, the default grouped table header view uses a UILabel with its text capitalized and systemFontSize:15.
The header height is 50.
How to recreate default grouped headers:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 200, 20)];
[label setText:[[self tableView:tableView titleForHeaderInSection:section] uppercaseString]];
[label setFont:[UIFont systemFontOfSize:15]];
[headerView addSubview:label];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}

Set color of UITableView Section Header

How can I set the section header of all my section to redColor, without setting them to have all the same header string, and set the font and font color.
I tried this but it gets rid of my section headers
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
if (section == 1)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
I would really appreciate some sample code.
thanks in advance
If you are simply looking to change the background and font color s of a header view, a simpler approach may be to just use
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
There is a very nice example/explanation here.
Try customizing your section header view. You can extend this approach.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
//Put your common code here
// lets say you need all background color to be white. do this here
[headerView setBackgroundColor:[UIColor redColor]];
//Check the section and do section specific contents
if (section == 1)
[headerView setText:#"Section 1"];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
CGRect headerFrame = header.frame;
header.textLabel.frame = headerFrame;
[header.contentView setBackgroundColor:[UIColor blueColor]];
}

set Background Image to UITableView Section Header

I've searched online and on stackoverflow but couldn't find one good answer how to do it.
I've read about
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section`
but there's nothing there that can help me.
The closest thing to an image background for the header there is:
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myBackground.png"]];
but it shows the picture as a pattern with no way controlling its size.
If you want to set the image as section header use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *myImage = [UIImage imageNamed:#"loginHeader.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,300,100);
return imageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
You can set the background colour to a pattern with image:
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]
Sure you can. In the method -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section you will need to simply instantiate a UIImageView with an image, set the frame for the imageView object and return the imageView object.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
headerView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
headerView.layer.borderWidth = 1.0;
UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,headerView.frame.size.height-40, tableView.frame.size.width - 5, 30)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:22.0];
headerLabel.text = #"yourText";
headerLabel.textAlignment = NSTextAlignmentLeft;
UIImageView *headerImage = [[UIImageView alloc]initWithFrame:headerView.frame];
headerImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"image.jpg"]];
headerImage.contentMode = UIViewContentModeScaleAspectFill;
[headerImage setClipsToBounds:YES];
[headerView addSubview:headerImage];
[headerView addSubview:headerLabel];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}

How can I custom UITableViewCell like Twitter?

I found that the iPad twitter.app UITableViewCell's border have two pixel line, it looks beautiful and professional,how can I do that? Thank you!
Because UITableViewCell inherits from UIView, a cell has a content view. You can add your own subviews (the labels and textfields) to that contentView and lay them out programmatically or using the Interface Builder.
There are a lot of online tutorials on how to accomplish that. Just search with google for "uitableviewcell interface builder tutorial".
Check out this pretty good tutorial Custom UITableViewCell Using Interface Builder.
Finally,I customed UITableViewCell use code,and I think it looks well. : )
MenuViewController.m file:
- (id)initWithFrame:(CGRect)frame {
if (self = [super init]) {
[self.view setFrame:frame];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
[_tableView setBackgroundColor:[UIColor clearColor]];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
[_tableView setTableFooterView:footerView];
[footerView release];
[self.view addSubview:_tableView];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DoubleSeparatorCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[DoubleSeparatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSString *text;
UIColor *upperLineColor,*lowerLineColor,*viewColor;
upperLineColor = RGBA(255, 255, 255, 30);
lowerLineColor = RGBA(0, 0, 0, 50);
viewColor = RGBA(0,0,0,5);
if ([indexPath row] == 0) {
text = NSLocalizedString(#"...", nil);
} else if ([indexPath row] == 1) {
text = NSLocalizedString(#"...", nil);
} else if ([indexPath row] == 2) {
text = NSLocalizedString(#"...", nil);
} else {
text = NSLocalizedString(#"...", nil);
}
[cell.textLabel setText:text];
[cell.textLabel setTextColor:RGBA(170, 170, 170, 100)];
[cell.textLabel setShadowColor:[UIColor blackColor]];
[cell.textLabel setShadowOffset:CGSizeMake(1, 1)];
[cell.upperLine setBackgroundColor:upperLineColor];
[cell.lowerLine setBackgroundColor:lowerLineColor];
[cell.contentView setBackgroundColor:viewColor];
return cell;
}
DoubleSeparatorCell.h
#interface DoubleSeparatorCell : UITableViewCell {
UIView *upperLine;
UIView *lowerLine;
}
#property (nonatomic ,retain) UIView *upperLine;
#property (nonatomic ,retain) UIView *lowerLine;
#end
DoubleSeparatorCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.upperLine = [[UIView alloc] init];
self.lowerLine = [[UIView alloc] init];
[self.contentView addSubview:self.upperLine];
[self.contentView addSubview:self.lowerLine];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self.upperLine setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 1)];
[self.lowerLine setFrame:CGRectMake(0, self.contentView.frame.size.height - 1, self.frame.size.width, 1)];
}
Srikar has already showed you the right path. By the way i just want to add the following:
You can cutomize your cell programmatically which can be done by inheriting the native class UITableViewCell.
Then by, create the instance of table view cell class and add it to the UITableView.
Now the cell is yours.
Happy Coding,
Arun
I'd point out that those cells you screenshot appear to have a light gray top border of 1 point and a dark gray bottom border of 1 point (or maybe they are pixels - sorry my eyes aren't that good :-) ).
So it may be kind of a hack (go on, savage me people), but you could:
Create a UILabel topBorder with frame CGRect(0,0,cell.contentView.frame.size,width,1)
Create a UILabel bottomBorder with frame CGRect (0,cell.contentView.frame.size.height - 1,cell.contentView.frame.size.width,1)
Set color of topBorder to UIColor lightGrayColor (or tweak for exact colors)
Set color of bottomBorder to UIColor darkGrayColor (ditto)
Add both subViews to cell.contentView
Note that you do not have to subclass UITableCellView - simply add these steps to your tableView:cellForRowAtIndexPath: method and they will appear.
Enjoy,
Damien
Override drawRect method and draw lines as you need.
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [topColor CGColor]);
// border top
CGContextMoveToPoint(context, bounds.origin.x, bounds.origin.y);
CGContextAddLineToPoint(context, bounds.origin.x+bounds.size.width, bounds.origin.y);
CGContextStrokePath(context);
// border bottom
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [lowerColor CGColor]);
CGContextMoveToPoint(context, bounds.origin.x, bounds.origin.y+1);
CGContextAddLineToPoint(context, bounds.origin.x+bounds.size.width, bounds.origin.y+1);
CGContextStrokePath(context);
}

Unable to add header and footer for a table view controller

#property(nonatomic,retain) UIView *tableHeaderView;
// accessory view for above row content. default is nil. not to be confused with section header
#property(nonatomic,retain) UIView *tableFooterView;
// accessory view below content. default is nil. not to be confused with section footer
I have added a label in footer View do inner coding according to your requirements
No need to make any global variables.
- (UIView *)tableView:(UITableView *)tbleView viewForFooterInSection:(NSInteger)section
{
UILabel *label;
label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 30)]autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:15.0]];
[label setText:NSLocalizedString(#"Instructions Personal Profile",#"Instructions Personal Profile")];
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]autorelease];
[view setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[view addSubview:label];
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
}
For setting the height of footer and header use:-
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
}