Making a TableViewCell Section Footer Transparent - ios

I am trying to make a section footer of a UITableViewCell transparent and this is what I am doing right now:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *background = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)background];
imageView.frame = CGRectMake(10,10,1,30);
return imageView;
}
But I am getting a problem from casting background to a UIImage. Is this possible in this way, is there a better way to cast it or am I simply going about this wrong? I am basically using the footer as a way to create a clear spacer between cells. Need some guidance on this.

This code will add clear footer to your section, where you can set the desired height to whatever you need. Below it has been set to 30, to match your code in the question.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}

Instead of type casting the colour into the UIImage, use the backgroundColor property :
Try this :
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,1,30)];
imageView.backgroundColor = [UIColor clearColor];

Related

customize UITableView Header Section at run time

I have set header back ground using the code below but I want to change the color of header back again on run time when I click on the button that is added as a subview on the header.
please provide me code, thanks in advance :)
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.bounds.size.width, 50)];
header.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homeCellHeaderBackGround.png"]];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
label.text = _array[section][#"name"];
label.textColor = [UIColor whiteColor];
[header addSubview:label];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(230, 10, 70, 35)];
[button addTarget:self action:#selector(onExpandButton:) forControlEvents:UIControlEventTouchUpInside];
//[button setTitle:#"Expand" forState:UIControlStateNormal];
[button setTag:section];
[header addSubview:button];
return header;
}
You can modify it in run time by:
first:
you can declare a global variable/propery like:
#property (nonatomic) UIView *tableHeader;
and set it under -(void)viewDidLoad
like
self.tableView.tableHeaderView = self.tableHeader;
or using the delagate:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.tableHeader;
}
and modify it anywhere you like, but dont forget to reload you table section header, probably [tableView reloadSections:NSIndexSet withRowAnimation:UITableViewRowAnimation]; will do.
But
In your case you can retreive/get the tableHeader made inside the delegate:
-(UIView *)tableView:tableView viewForHeaderInSection:section
by
UIView *tableHeaderView = [self tableView:yourTableView viewForHeaderInSection:yourSection];
then modify tableHeaderView..
or simply reassign tableHeader by:
yourTableView.tableHeaderView = tableHeaderView;
your by using header alone, since as i can see it's a global variable..
change/update it directly like:
header.backgroundColor = [UIColor yourNewColor];
hope i've helped you.. happy coding, cheers..
You can change the background color of the section header by following code:
- (IBAction)onExpandButton:(id)sender {
UIView *header = ((UIButton *)sender).superView;
header.backgroundColor = <Whatever UIColor you like>;
...
}
-(IBAction)onExpandButton:(UIButton *)sender
{
UIView *tmpView = [self.YourTableViewName headerViewForSection:sender.tag];
tmpView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homeCellHeaderBackGround.png"]];
}
-You got the view from your table view header for section method. After you can set your image view on view.

Footer view's color always darker than UITableView separator's color

In my UITableView, I set the separator color like this:
- (void)viewDidLoad {
...
self.tableView.separatorColor = [UIColor lightGrayColor];
...
}
And I set the color of the footer like this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc]
initWithFrame:(CGRect){0, 0, 320, 1}];
footerView.contentView.backgroundColor = [UIColor lightGrayColor];
return footerView;
}
However, the footer view's color is always darker than the separator's color, like this:
How do I get them to be the exact same color? Thanks.
From iOS 6.0 onwards, you can use the below mentioned UITableView's delegate method to change the color of the footer view's background-
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
//Set the background color of the View
view.tintColor = [UIColor blueColor];
}
None of the above worked for me. The only thing that worked was the solution from this thread:
White Separator Above Table Section Headers
Basically you need to manually add in the footer separator 1 px above your custom footer view.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UITableViewHeaderFooterView *versionView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, maxScreenWidth, 50)];
UIView *footerSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, -1, maxScreenWidth, 1)];
versionView.backgroundColor = [UIColor blackColor];
footerSeparator.backgroundColor = [UIColor darkGrayColor];//Your color
[versionView addSubview:footerSeparator];
return versionView;
}
The only problem I have with this is that the footer separator seems to disappear if you scroll away from it and then back to it in the table view.

White Separator Above Table Section Headers

I'm having a really weird issue with table view separators. I have set the separator color to a dark gray which works great below the cells, but for some reason, there is a white separator before my section header (see the screenshot, above November). When I set the separator style to none, the line disappears which indicates that it is a separator. How can I remove this white separator line?
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, -1, 320, 2)];
view2.backgroundColor = [UIColor blackColor]; // Your color
view1.backgroundColor = [UIColor blackColor]; // Your color
[view1 addSubview:view2];
return view1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01;
}
For me, this fixed it:
cell.backgroundColor = .clear
I was setting the cell’s backgroundView, but apparently the cell’s default color was shining through in some cases.

UIButton is not working in the header of my UITableView

I can't figure out why this UIButton is not working in the header of my UITableView. It's appearing there but the touch is not working.
The NSLog statement isn't triggering either. It's almost like it's beneath another view or something so that you can see it but the press action doesn't work.
thanks for any help with this
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor clearColor];
[sectionHeader addSubview:self.topMapView];
// add map launch button
mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapLaunchButton addTarget:self
action:#selector(mapButtonTouch:)
forControlEvents:UIControlEventTouchDown];
[mapLaunchButton setTitle:#"ggggggg" forState:UIControlStateNormal];
mapLaunchButton.frame = CGRectMake(0, 0, 300, 90);
mapLaunchButton.backgroundColor = [UIColor redColor];
[sectionHeader addSubview:mapLaunchButton];
// [sectionHeader bringSubviewToFront:mapLaunchButton];
self.tableView.tableHeaderView = sectionHeader;
return sectionHeader;
}
- (void)mapButtonTouch:(id)sender {
NSLog(#"map button was touched");
}
I can see more than one mistake -
If your table has only one section (to verify this check for numberOfSectionsInTableView delegate) then remove this line -
self.tableView.tableHeaderView = sectionHeader;
Set sectionHeader.frame to something appropriate (not CGRectNull). The benefit of setting section header (versus table header view) is that when user will scroll the table rows then the section header will stick on top (float) and will not go away. (Plain style table)
Still the problem is not resolved, then please verify his method -
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50; // depending upon the height of the sectionHeader
}
As pointed out by other poster, to capture touch event on UIButton UIControlEventTouchUpInside is preferred event.
EDIT - (as table header view implementation)
If you want to scroll it up then make it as table header (not section header). So remove all this from viewForHeaderInSection and put it inside viewDidLoad of your view controller class. Keep this line (don't remove it in this case) -
self.tableView.tableHeaderView = sectionHeader;
However, above points 2 and 4 still holds true.
If you change UIControlEventTouchDown to UIControlEventTouchUpInside?
One thing is the section header, and another is the table header. You are assigning the same view as both. I'm not sure this is the cause of your problem, but it's something to get you started.
Instead of implementing that method, on your viewDidLoad, create that view and assign it as the self.tableView.tableHeaderView
Additionally, the most common user experience is associated with the UIControlEventTouchUpInside (the action will not execute until the finger is lifted and still inside the button). However this likely has nothing to do with your issue. It's just a matter of when the action is called.
If this doesn't fix your issue let me know and I'll try to check if there's something else I've missed
Cheers!
If you add a view
self.tableView.tableHeaderView = ({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
you can add new components: UIImageView, UIlabel...
Its works for me!
I tested.
The final code:
self.tableView.tableHeaderView = ({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.image = [UIImage imageNamed:#"avatar.jpg"];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 50.0;
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 3.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
UIButton *mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapLaunchButton addTarget:self action:#selector(mapButtonTouch:) forControlEvents:UIControlEventTouchDown];
[mapLaunchButton setTitle:#"ggggggg" forState:UIControlStateNormal];
mapLaunchButton.frame = CGRectMake(0, 0, 300, 90);
mapLaunchButton.backgroundColor = [UIColor redColor];
[view addSubview:imageView];
[view addSubview:mapLaunchButton];
view;
});
I had the same problem. Changing UIXXXXX from UILabel to UIView in the following line worked for me:
UIView *sectionHeader = [[UIXXXXX alloc] initWithFrame:CGRectNull];
and I didn't use:
self.tableView.tableHeaderView = sectionHeader;

UITableView center a UIImage and UILabel in viewForHeaderInSection

I wanna center vertically and horizontally an image inside of HeaderSection and a label inside of the image. But I don't have a clear idea about how make that. My code is:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
UIImageView *sectionHeaderBG;
sectionTitle.text = #"Trial"; //[[tableDataSource objectAtIndex: section] objectForKey: #"Title"];
sectionTitle.textAlignment = NSTextAlignmentCenter;
sectionTitle.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
sectionTitle.textColor = [UIColor whiteColor];
sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
sectionTitle.shadowOffset = CGSizeMake(1, 1);
sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
sectionHeaderBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width/2, image.size.height)];
sectionHeaderBG.image = [UIImage imageNamed:#"bg_title_category"];
[headerView addSubview:sectionHeaderBG];
[headerView addSubview:sectionTitle];
return headerView;
}
But this code not center anything... Thanks in advance!
The problem is that you're creating headerView with origin (0,0) and it's size the same one as your image; then you're adding all of your views to headerView. When the table view adds this header view it will not be centered, rather it'll placed at (0,0).
What I'd suggest you to do is to create headerView with the same origin (0,0) but with the width of your tableView and the height depening on you. Let's just assume for now the height will be the same as your image plus 10px at the top and bottom just to give it some margin. Then you can add your UIImageView and UILabel inside headerView and center them with respect to it. It'd be something like this:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *image = [UIImage imageNamed:#"bg_title_category"];
// Create your headerView with the same width as the tableView and a little taller than the image
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width, image.size.height + 20)]; //10px top and 10px bottom. Just for illustration purposes.
// Create the image view
UIImageView *sectionHeaderBG = [[UIImageView alloc] initWithImage:image];
// Now center it and add it to headerView
sectionHeaderBG.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
[headerView addSubview: [sectionHeaderBG autorelease]];
// Now it's turn of the label. Again I suggest using the tableView's width
UILabel *sectionTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
// Now center it. You could even do this when creating it's frame
sectionTitle.center = CGPointMake(headerView.bounds.size.width/2, headerView.bounds.size.height/2);
// do the rest of the configuration for your label...
// and add it to headerView
[headerView addSubview: [sectionTitle autorelease]];
return [headerView autorelease];
}
Hope this helps!

Resources