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!
Related
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
I am building an app which should run on iOS7 and on iOS6. For the iOS6 version I'm trying to achieve a similar look and feel than the iOS7 version.
Now I want to get the headerView of a section on iOS6 look similar to the one on iOS7.
With the approach below its looks ok on iOS6.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont startProduktView_landLabelFont];
label.frame = CGRectMake(5, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor darkGrayColor];
label.shadowColor = [UIColor clearColor];
label.shadowOffset = CGSizeMake(0.0, 0.0);
label.text = sectionTitle;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 21)];
[view addSubview:label];
return view;
}
However, the issue I am facing now is that this is also applied to the iOS7 version. But in the iOS 7 version the header should appear as standard. So basically it would be great if there would be some way that this delegate method won't be called at all under iOS7. Is this possible?
You can't make a particular version of iOS not call a standard delegate method that you have implemented. You could check the OS version and then supply an OS6 or OS7 specific class as the delegate, with different implementations for the methods. Alternately you can just have your code run and not worry that you are explicitly creating the section header labels.
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;
}
I want to get all the views of a grouped table view to change the label color and to set the background color.
I found the answer, it's not possible to get the header view of a table view section. But you can implement the delegate tableView:viewForHeaderInSection: to recreate the header view and the label. The following code will give you the same header view and the exact label.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 5.5f, 300.0f, 30.0f)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16.5];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[view addSubview:label];
return view;
}
That's great you figured out your solution.
A couple of suggestions:
Don't hardcode the CGRect for the width of your frame, but rather use self.view.size.width for the width (e.g. in case you're in landscape orientation or if Apple ever introduces an iPhone with a different screen size);
You probably want to use autoresizingMask for both the label and the view that holds the label, so that they'll resize as the screen orientation changes, or make sure you invoke [self.tableview reloadData] on orientation changes; and
This is obviously a single line label ... if that works for you great, otherwise you'd want to use sizeWithFont:constrainedToSize:lineBreakMode to determine the height, both for creating the label/view as well as responding to tableView:heightForHeaderInSection:.
You also need to add the textColor:
label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];
A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like to change the background color of said label, but no matter what I do the color does not change. The code in question:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabel subview has not yet been created.
Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.
This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.
If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
for (UIView *views in views.subviews)
{
UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
temp.textColor = [UIColor whiteColor];
temp.shadowColor = [UIColor blackColor];
temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}