Objective C UItableView header custom view black - ios

I am creating a custom view for my header and it only shows up as a black rectangle.
The code for the tableviewcontroller is:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
head* headerView = [[head alloc]init];
headerView.name=[sectionList objectAtIndex:section];
[headerView setNeedsDisplay];
return headerView;
}
where head is the custom view with a drawrect function:
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self setBackgroundColor:[UIColor colorWithRed:125 green:125 blue:125 alpha:.5]];
bounds = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGSize titleSize=CGSizeFromString(self.name);
CGRect titleRect={CGPointMake(10, 10),titleSize};
UILabel *title = [[UILabel alloc] initWithFrame:titleRect];
[title setTextColor:[UIColor blackColor]];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[title setText:self.name];
[self addSubview:title];
CGContextSetRGBFillColor(context, 80, 168, 206, 0.5);
CGContextAddEllipseInRect(context, CGRectMake(titleRect.origin.x, 0,20, 20));
}
Also when I try to set the name in the viewController (see above). The name remains null.
When I tried to do it without a custom UIView. This time it only showed a white view:
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width,40.0f)];
[headerView setBackgroundColor:[UIColor colorWithRed:71 green:71 blue:71 alpha:.5]];
UIImage *reps = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: #"test" ofType: #"png"]];
CGSize titleSize=CGSizeFromString([sectionList objectAtIndex:section]);
CGRect titleRect={CGPointMake(0, 0),titleSize};
UILabel *groupTitle = [[UILabel alloc]initWithFrame:titleRect];
[groupTitle setText:[sectionList objectAtIndex:section]];
[headerView addSubview:groupTitle];
UIImageView* repsView = [[UIImageView alloc]initWithImage:reps];
[headerView addSubview:repsView];
return headerView;
}
Thanks

You should create headerView by providing frame, for example:
head* headerView = [[head alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 40.0f)];
After that make sure you implemented table view delegate for table header height:
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40.0f;
}
You should consider create custom view without overriding drawRect: method because this is very CPU expensive.

Related

Unable to create section border in tableview and i want to represent the ui as per the attached image?

how to create section border in tableview and i want to represent the ui as per the attached image ? i displayed spaces between sections but cannot set the border line for section.
For this you need to add and import
#import <QuartzCore/QuartzCore.h>
After that follow below steps
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *section = [[UIView alloc]init];
section.frame = CGRectMake(0, 0, tableview.frame.size.width, height);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:#"imageName"]];
imageView.frame = CGRectMake(0, 0, YOUR_WIDTH, YOUR_HEIGHT); //set your frame
[section addSubview:imageView];
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);
[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = #"section title";
labelSection.text = name;
[labelSection setTextColor:[UIColor blackColor]];
[section addSubview:labelSection];
section.layer.borderWidth = 2.0f;
section.layer.cornerRadius = 1.0f;
section.layer.masksToBounds = YES;
section.layer.borderColor=[UIColor lightGrayColor].CGColor;
return section;
}
If you want to set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44; //It default size.If you want to change to other size you can change.
//OR
// return 90; //Set to your needed size
}

unwanted white space "under" tableView subview when tableView scrolled to its content limits

I have a UITableViewCell as a subview in a custom view controller. It works great except that when it scrolls to its top or bottom of its contentSize limit, it "keeps going" and leaves some white space exposed behind it. This is particularly irritating because I do have a UIView covering the entire screen behind the tableView, and that view is set to a non-white color. I also added a subview exactly underlaying the tableview with the same background color, again attempting to block the white. I also set the UIApplication Window background color to a non white color. None of this worked.
I would have thought that even if my TableView bounces around its origin frame, the "exposed" view should match the underlying view rather than be white. How can I fix my tableView so that it retains all its scroll properties but doesn't reveal white when it bounces around at the end of a scroll?
Here is a screen shot of the effect. The white appears the tableViewHeader and below a UISCrollView that occupies the top of the screen. This appears when I scroll the tableView all the way to one extreme. The white space appears at the bottom rather than the top of the tableView if I scroll all the way to the other end.
Here's the relevant code, quite vanilla I think:
#interface JudgeViewController () <UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate, UIViewControllerRestoration, UIScrollViewDelegate>
#property (nonatomic) UITableView *tableView;
#end
functions to set tableViewCells
#pragma mark - tableview appearance and actions
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StatsViewController *svc = [[StatsViewController alloc] init];
svc.user = self.object.answerUser[indexPath.row];
svc.fromJudge = YES;
[self.navigationController pushViewController:svc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.object.answerArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell ==nil)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode: UILineBreakModeWordWrap];
[label setMinimumFontSize:SMALL_FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE]];
[label setTag:1];
// [[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE] constrainedToSize:constraint];
if(!label)
label = (UILabel *)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, .8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, MAX(size.height, 44.0f))];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(.85*width, label.frame.size.height/2-2*CELL_CONTENT_MARGIN, .12*width, 20);
[button1 setTitle:#"UP" forState:UIControlStateNormal];
button1.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(upVoteA:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
[cell.contentView bringSubviewToFront:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(.85*width, label.frame.size.height/2+2*CELL_CONTENT_MARGIN, .12*width, 20);
[button2 setTitle:#"DOWN" forState:UIControlStateNormal];
button2.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(downVoteA:) forControlEvents:UIControlEventTouchUpInside];
CGFloat moduloResult = indexPath.row % 2;
if(moduloResult>0)
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.6];
}
else
{
cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.4];
}
cell.opaque = NO;
cell.alpha = 0.2;
[cell.contentView addSubview:button2];
[cell.contentView bringSubviewToFront:button2];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)keyboardToJudge
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row < [self.object.answerArray count])
{
NSString *text = self.object.answerArray[indexPath.row];
CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE-2.0f] constrainedToSize:constraint];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN)*2;
}
else
{
return 200.0f;
}
}
functions setting out layout:
-(void)viewDidLoad
{
...among other things setting up top scroll view (top part of view with gray background and orange text)...
if(self.navigationController.navigationBar.frame.size.height>0)
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, width, height*SCROLL_VIEW_OFFSET)];
}
else
{
self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight+5, width, height*SCROLL_VIEW_OFFSET)];
}
self.scroll.backgroundColor = BACKGROUND_COLOR;
self.scroll.contentSize =CGSizeMake(width, .5*height);
self.scroll.delegate = self;
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
[self.scroll setShowsHorizontalScrollIndicator:NO];
...adding buttons to self.scroll...
[self.view addSubview:self.scroll];
....
self.tableView = [[UITableView alloc] initWithFrame:frame];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableFooterView.backgroundColor = BACKGROUND_COLOR;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
self.tableView.tableHeaderView.backgroundColor = BACKGROUND_COLOR;
....tableView hidden state is changed to yes in another function if row count is zero but not usually...
self.tableView.hidden = NO;
[self.view addSubview:self.tableView];
...
}
Finally, I also call :
[self.tableView reloadData];
after reloading data from a webserver and depending on the results either set the tableView to hidden or not (not hidden if there are results to display). That should be every line of code that touches the tableView subview.
Add to your viewDidLoad
[self.tableView setBackgroundColor:BACKGROUND_COLOR];
Set the background color of your tableView and you'll be fine
Change this line:
self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
To this:
self.scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
You are adding a footer to your tableview with that line. The following image is from the IOS dev Library
Set the table view estimate to 0 (Uncheck Automatic) in the size inspector

Why my button in a UItableView didn't respond?

I've a UItableView with multiple section, In Each section, I've a View which contains a button, and in each of these sections one row is located but I've hided that row.
In short, my tableview is a collection of sections which in turns contains buttons on each section, when I click on the button it didn't respond but once I scroll ,my table from bottom to top the buttons are responded. What is the issue here?
Thanks!
Here is the code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
sheettable.scrollsToTop=YES;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)];
CGRect rect= CGRectMake(0, 0, 320,40);
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame =rect;
maskLayer.path = maskPath.CGPath;
headerView.layer.mask = maskLayer;
headerView.backgroundColor=[UIColor greenColor];
UITextField *label1 = [[UITextField alloc] init] ;
label1.delegate=self;
label1.frame = CGRectMake(5, 1, 200, 35);
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = [[result2 objectAtIndex:section] objectForKey:#"cat_desc"];
UIButton *btn=[[UIButton alloc]init];
btn.frame=CGRectMake(5, 1, 200, 35);
btn.tag=section;
btn.backgroundColor=[UIColor clearColor];
[headerView addSubview:label1];
[btn addTarget:self action:#selector(selectedsections) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:btn];
cameracategory=section;
return headerView;
}
-(void)selectedsections
{
if(cameracategory==0)
{
NSLog(#"At Index");
}
if(cameracategory==1)
{
NSLog(#"At Index");
}
if(cameracategory==2)
{
NSLog(#"At Index");
}
if(cameracategory==3)
{
NSLog(#"At Index");
}
if(cameracategory==4)
{
NSLog(#"At Index");
}
}
I tested your code, it works, but I created the UITableView myself. You should check that. Also try to write more readable code :) it will be more easier in the future :)
You have some misaligned frames (subviews are bigger than parent).
- (void)sectionButtonAction:(UIButton *)sender {
NSLog(#"Section button tapped: %i", sender.tag);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect tableRect = tableView.frame;
CGRect headerRect = CGRectZero;
headerRect.size.width = tableRect.size.width;
headerRect.size.height = 22;
CGRect bezierRect = CGRectZero;
bezierRect.size.width = tableRect.size.width;
bezierRect.size.height = 44;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:bezierRect];
[maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bezierRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)].CGPath];
UIView *headerView = [[UIView alloc] initWithFrame:headerRect];
[headerView setBackgroundColor:[UIColor greenColor]];
[[headerView layer] setMask:maskLayer];
CGRect labelRect = CGRectZero;
labelRect.origin.x = 5;
labelRect.origin.y = 1;
labelRect.size.width = 200;
labelRect.size.height = 35;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:labelRect];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16]];
[headerLabel setText:#"asd"];
[headerView addSubview:headerLabel];
CGRect buttonRect = labelRect;
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[headerButton setFrame:buttonRect];
[headerButton setTag:section];
[headerButton addTarget:self action:#selector(sectionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:headerButton];
return headerView;
}

Create custom UITableView header with white text and red background

I have a UITableView that needs a simple custom header that has a background color and a white label. This is my code:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *titleString = #"Title";
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 45)];
[headerView setBackgroundColor:[UIColor redColor]];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerView.frame];
headerLabel.text = titleString;
headerLabel.textColor = [UIColor whiteColor];
[headerView addSubview:headerLabel];
return headerView;
}
I can change the background color without any problem, but for some reason the label always shows gray text color. I'm also using a custom category to change all fonts to a custom one that looks like this (I don't think this is the problem but anyway..):
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:#"MyFont" size:self.font.pointSize]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:#"MyFont" size:self.font.pointSize]];
}
return result;
}
Any idea why could this be happening?

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