I want to display two lots of text in two different footers in my table.
The text for section 3 uses titleForFooterInSection :
- (NSString *)tableView:(UITableView *)tv titleForFooterInSection:(NSInteger)section
{
if (section == 3)
return #"SOME TEXT FOR SECTION 3";
else
return #"";
}
and works great and is styled correctly.
I also want to add some more complicatedly formatted text (just left aligned really), so I create a label and add it to viewForFooterInSection, which works but is not padded (doesn't start at x = 20 & y = 20, starts hard up against the main windows edge) and is not styled like the other text.
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
if (section == 1)
{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 50)] autorelease];
label.text = #"SOME COMPLICATED TEXT FOR SECTION 1\r\n"
"\r\n"
"MORE TEXT HERE\r\n"
"AND HERE.\r\n"
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:14];
label.shadowColor = [UIColor colorWithWhite:0.8 alpha:0.8];
label.textColor = [UIColor blueColor];
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 0;
[label sizeToFit];
return label;
}
else
return nil;
}
-(CGFloat)tableView:(UITableView *)tv heightForFooterInSection:(NSInteger)section
{
if (section == 1)
return 180.0f;
else if (section == 3)
return 50.0f;
else
return 0.0f;
}
To get the padding add the label into another view before returning that as the footer view;
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
...
CGRect footerFrame = [tv rectForFooterInSection:1];
CGRect labelFrame = CGRectMake(20, 20, footerFrame.size.width - 40, footerFrame.size.height - 40);
UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
UILabel *footerLabel = [[UILabel alloc] initWithFrame:labelFrame];
[footer addSubview:footerLabel];
[footerLabel release];
...
return footer;
}
NB: You might want to scope *footer better to avoid a memory leak or use autorelease.
I'm not sure what the system defaults are for the label it creates to hold the text you supply as the title for footer. I guess you'll need to experiment when creating the footer label as Google hasn't thrown anything useful up.
EDIT: Rather than put all the code in the viewForFooterInSection method I use a custom UIView;
#interface CustomHeaderView : UIView {
NSString *headerTitle;
}
#property (nonatomic, retain) NSString *headerTitle;
#end
#implementation CustomHeaderView
#synthesize headerTitle;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// create frame with padding
CGRect labelFrame = CGRectMake(8, 8, frame.size.width - 16, frame.size.height - 16);
UILabel *headerText = [[UILabel alloc] initWithFrame:labelFrame];
[headerText setNumberOfLines:0];
[headerText setFont:[UIFont boldSystemFontOfSize:14.0]];
[self addSubview:headerText];
[headerText release];
}
return self;
}
- (void)setHeaderTitle:(NSString *)title {
[title retain];
[headerTitle release];
UILabel *label = [[self subviews] objectAtIndex:0];
[label setText:title];
[label sizeToFit];
CGRect viewFrame = [self frame];
CGRect labelFrame = [label frame];
viewFrame.size.height = labelFrame.size.height + 16;
[self setFrame:viewFrame];
[self setNeedsLayout];
headerTitle = title;
}
- (void)dealloc {
[headerTitle release];
[super dealloc];
}
#end
This allows me to set my title text and have the header resize for the content. I lazy load this in my view controller;
#interface MyViewController
{
UIView *headerView;
}
- (UIView *) headerView;
#end
#implementation MyViewController
- (UIView *) headerView {
if(headerView)
return headerView;
float w = [[self view] bounds].size.width;
// height irrelevant as long as non zero custom view will resize
CGRect headerFrame = CGRectMake(0, 0, w, 32);
headerView = [[CustomHeaderView alloc] initWithFrame:headerFrame];
return headerView;
}
- (UIView *) tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec {
return [self headerView];
}
- (void) viewWillAppear:(BOOL)animated {
[(CustomHeaderView *)[self headerView] setHeaderTitle:#"My Custom Header"]];
}
#end
Make sure u return proper height in - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; delegate method.
Related
I am developing an application in which I need header to customize and add my own button just for single section. I googled and done some code where I am able to add button, but I am facing two issue.
Titles of other's section is not showing.
Button not show properly because of tableview scroll size same after adding button.
Here is what I am doing.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
if(section==2){
float width = tableView.bounds.size.width;
int fontSize = 18;
int padding = 10;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(padding, 2, width - padding, fontSize)];
label.text = #"Texto";
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0,1);
label.font = [UIFont boldSystemFontOfSize:fontSize];
[headerView addSubview:label];
UIButton * registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[registerButton setImage:[UIImage imageNamed:#"P_register_btn.png"] forState:UIControlStateNormal];
[registerButton setFrame:CGRectMake(0, 0, 320, 150)];
[headerView addSubview:registerButton];
return headerView;
}
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0)
return #"Registration";
else if(section==1)
return #"Player Detail";
return nil;
}
Here is Image of my out put in which Texto text show but button is under that area where the end limit of table view scroll height and also section 0 and 1 title is not showing I also block code for first and second section in viewforheaderinsection. Thanks in advance.
The other header names don't appear because the viewForHeader method only answers for section 2. Once implemented, the datasource expects that method to be the authority on all headers. Just add some else logic for the other sections....
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView;
UILabel *label;
if (section==2) {
headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
// and so on
return headerView;
} else if (section == 0) {
label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width, 44)] autorelease];
label.text = #"Section 0 Title";
return label;
} else .. and so on
The header answered by this method looks to be 40px high (see initWithFrame), but the button being added is 150px high (see setFrame: for the button). That's the likely the root cause of the button issue. Try implementing:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 150.0 : UITableViewAutomaticDimension;
}
I have some weird behavior on the iPad that I am not getting on the iPhone.
I have a Grouped table view that has sections and headers for the sections, the problem is that on the iPad the top most section's header is not displayed, when scrolling down the table the sections header appears for a short while just before going of screen.
Before scrolling
http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing
After scrolling
http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing
The code for creating the headers of the sections:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil || [sectionTitle isEqualToString:#""]) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init] ;
label.frame = CGRectMake(12, 0, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIImage *img = [UIImage imageNamed:#"header"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
imgView.image = img;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
[view addSubview:imgView];
[view addSubview:label];
return view;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
return [self.groupHeadings objectAtIndex:aSection];
}
My TableView's code:
tableViewResult = [[UITableView alloc] initWithFrame:mainView.frame style:UITableViewStyleGrouped];
tableViewResult.separatorColor = [UIColor clearColor];
[mainView addSubview:tableViewResult];
I set the delegate and datasource in another method as I first do a web request before loading any data into the table, ie when the web request is done I do:
tableViewResult.delegate = self;
tableViewResult.dataSource = self;
[tableViewResult reloadData];
Everything works as expected except for the header of the top most section, and only on the iPad.
Any ideas what can cause this behavior?
What fixed the issue was changing this function to:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:#""])
return nil;
return [self.groupHeadings objectAtIndex:aSection];
}
I am using some background color for the tabelView and style is grouped. The text in the header for the sections is not clear so I need to modify the the text color so that the header text should be visible.
I want to know can we change the header text's color and size?
Adding to terente's answer:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0) {
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
//headerView.contentMode = UIViewContentModeScaleToFill;
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -5.0, 300.0, 90.0)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.text = #"Header";
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor blackColor];
//this is what you asked
headerLabel.font = [UIFont boldSystemFontOfSize:17];
headerLabel.shadowColor = [UIColor clearColor];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
headerLabel.numberOfLines = 0;
headerLabel.textAlignment = UITextAlignmentCenter;
[headerView addSubview: headerLabel];
[headerLabel release];
// Return the headerView
return headerView;
}
else return nil;
}
You can use [UIFont fontWithName:#"<name of your font>" size:24.0]; for other fonts
Just implement
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And return your custom view for header.
Edit:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *headerTitleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kSectionHeaderHeight)];
[headerTitleView setImage:sectionHeaderBackgroundImage];
UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 38) / 2, 5, 38, kSectionHeaderHeight - 10)];
sectionTitleLabel.textColor = [UIColor redColor];
sectionTitleLabel.backgroundColor = [UIColor clearColor];
sectionTitleLabel.textAlignment = UITextAlignmentCenter;
sectionTitleLabel.text = #"A";
sectionTitleLabel.font = [UIFont fontWithName:#"yourFont" size:13];
[sectionTitleLabel setAdjustsFontSizeToFitWidth:YES];
[headerTitleView addSubview:sectionTitleLabel];
return headerTitleView;
}
- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{
[((UITableViewHeaderFooterView) *view).textLabel setFont:(UIFont...)];
}
and you can set the text label from the other table view delegate method.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
[((UITableViewHeaderFooterView *) view).textLabel setFont:[UIFont fontWithName:#"Your-Font-Name" size:((UITableViewHeaderFooterView *) view).textLabel.font.pointSize]];
}
Notes:
This will set the font to a custom font but keep the pointSize the same.
Also works for willDisplayFooterView.
Don't forget to change Your-Font-Name to your font.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
[headerView.textLabel setFont:[UIFont fontWithName:#"Gotham Book" size:16.0f]];
}}
We can use header.textlabel object to change other "UILabel" properties for that label.
I am quite new to IOS development, so forgive me if this problem turns out to be trivial. The following is the case:
I want to be able to call the didSelectRowAtIndexPath method to trigger some action when a user interacts with a table view cell. However, it seems that this method is never called for some reason. I am fairly certain that my code is valid. I set the delegate and dataSource properties and I conform to the UITableViewDelegate and UITableViewDataSource protocols.
If someone could shed some light on this, I would be very grateful.
My code:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#class Employee;
#define kTelephoneRowIndex 0
#define kEmailRowIndex 1
#define kLeftLabelTag 4096
#define kRightLabelTag 4097
#interface PersonnelDetailsViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, MFMailComposeViewControllerDelegate> {
UITableView *contactDetailsTable;
Employee *employee;
}
#property (nonatomic, retain) UITableView *contactDetailsTable;
#property (nonatomic, retain) Employee *employee;
+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type;
#end
and my .m file
#import <QuartzCore/QuartzCore.h>
#import "PersonnelDetailsViewController.h"
#import "Employee.h"
#implementation PersonnelDetailsViewController
#synthesize contactDetailsTable, employee;
+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type {
return [[NSBundle mainBundle] pathForResource:fileName ofType:type];
}
- (void)loadView {
[super loadView];
// Create the header view for the table view
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 110)];
// Set the image, using rounded corners
UIImage *employeeImage;
if ([employee.image length] == 0)
employeeImage = [UIImage imageNamed:#"logoL1nda70by80"];
else
employeeImage = [UIImage imageNamed:employee.image];
UIImageView *employeeImageView = [[UIImageView alloc] initWithImage:employeeImage];
employeeImageView.frame = CGRectMake(10, 15, 70, 80);
employeeImageView.layer.masksToBounds = YES;
employeeImageView.layer.cornerRadius = 5.0;
employeeImageView.layer.borderWidth = 1.0;
employeeImageView.layer.borderColor = [[UIColor grayColor] CGColor];
[headerView addSubview:employeeImageView];
UILabel *nameView = [[UILabel alloc] initWithFrame:CGRectMake(95, 35, 180, 20)];
nameView.text = employee.name;
nameView.font = [UIFont boldSystemFontOfSize:18];
[headerView addSubview:nameView];
UILabel *functionView = [[UILabel alloc] initWithFrame:CGRectMake(95, 55, 140, 16)];
functionView.text = employee.function;
functionView.textColor = [UIColor grayColor];
functionView.font = [UIFont systemFontOfSize:14];
[headerView addSubview:functionView];
[employeeImage release];
[employeeImageView release];
[nameView release];
[functionView release];
contactDetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 370) style: UITableViewStyleGrouped];
contactDetailsTable.backgroundColor = [UIColor clearColor];
contactDetailsTable.tableHeaderView = headerView;
[headerView release];
contactDetailsTable.delegate = self;
contactDetailsTable.dataSource = self;
[self.view addSubview:contactDetailsTable];
}
- (void)viewDidUnload {
self.contactDetailsTable = nil;
self.employee = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[contactDetailsTable reloadData];
}
- (void)dealloc {
[contactDetailsTable release];
[employee release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;did
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *contactDetailIdentifier = #"contactDetailIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactDetailIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:contactDetailIdentifier] autorelease];
UILabel *leftLabel = [[UITextField alloc] initWithFrame:CGRectMake(5, 12, 70, 25)];
leftLabel.textAlignment = UITextAlignmentRight;
leftLabel.textColor = [UIColor grayColor];
leftLabel.tag = kLeftLabelTag;
leftLabel.userInteractionEnabled = NO;
[cell.contentView addSubview:leftLabel];
[leftLabel release];
UILabel *rightLabel = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 160, 25)];
rightLabel.font = [UIFont boldSystemFontOfSize:16];
rightLabel.tag = kRightLabelTag;
rightLabel.userInteractionEnabled = NO;
[cell.contentView addSubview:rightLabel];
[rightLabel release];
}
NSUInteger row = [indexPath row];
UILabel *leftLabel = (UILabel *)[cell viewWithTag:kLeftLabelTag];
UILabel *rightLabel = (UILabel *)[cell viewWithTag:kRightLabelTag];
switch (row) {
case kTelephoneRowIndex:
leftLabel.text = #"telefoon";
rightLabel.text = employee.phone;
break;
case kEmailRowIndex:
leftLabel.text = #"e-mail";
rightLabel.text = employee.email;
break;
default:
break;
}
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = indexPath.row;
if (row == 1) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setToRecipients:[NSArray arrayWithObject:employee.email]];
[mailController.navigationBar setTintColor:[UIColor blackColor]];
[self presentModalViewController:mailController animated:YES];
mailController.mailComposeDelegate = self;
[mailController release];
}
NSLog(#"Row selected is: %d", row);
}
#pragma mark -
#pragma mark MFMailCompose View Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissModalViewControllerAnimated:YES];
}
#end
Check the doc for tableView:willSelectRowAtIndexPath::
Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.
You are returning nil, basically saying the table view that you don't want that row selected. Therefore tableView:didSelectRowAtIndexPath: is not called.
When I had this problem I found the solution was to use willSelectRowAtIndexPath. In other places in the same project didSelectItemAtIndexPath is getting called fine.
#pragma mark - UITableViewDelegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// for some reason this is getting called instead of didSelectItemAtIndexPath
[self viewDetails:indexPath.row];
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// [self viewDetails:indexPath.row];
}
How can we change the size of the text in a picker view?
Anyone please help.
You can change the font size of UIPickerView using code :
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
}
retval.font = [UIFont systemFontOfSize:22];
return retval;
}
For completeness, yakub_moriss code lacks of one line, which was in the post signaled by indragie. Without a line as
retval.text = [pickerViewArray objectAtIndex:row];
the picker will be empty.
This is also a great way to change the alignment of the text in the component too, change the textAlignment of the retval label.
Also, the background for the label should be set to clear:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *retval = (id)view;
if (!retval) {
retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)];
}
retval.opaque = NO;
retval.backgroundColor = [UIColor clearColor];
retval.font = [UIFont systemFontOfSize:28];
if (component == 0) {
retval.textAlignment = UITextAlignmentRight;
retval.text = [NSString stringWithFormat:#"%d ", row];
} else {
retval.textAlignment = UITextAlignmentLeft;
retval.text = [NSString stringWithFormat:#" .%d", row];
}
return retval;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 100;
}
return 88;
}
I'm using a category for this. It relies on this other category I have: http://compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html.
Anyway, this one looks like this:
#import <UIKit/UIKit.h>
#interface UIPickerView (CustomViewForRow)
#property (nonatomic, strong) UIFont *customViewForRowFont;
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
#end
and the .m
#import "UIPickerView+CustomViewForRow.h"
#import "NSObject+AssociatedObjectSupport.h"
#implementation UIPickerView (CustomViewForRow)
static char keyForCustomViewForRowFont;
- (UIFont *)customViewForRowFont {
UIFont *retVal = [self associatedObject:&keyForCustomViewForRowFont];
if (!retVal)
retVal = [UIFont boldSystemFontOfSize:17];
return retVal;
}
- (void)setCustomViewForRowFont:(UIFont *)customViewForRowFont {
[self setAssociatedObject:customViewForRowFont forKey:&keyForCustomViewForRowFont];
}
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label;
if (!view) {
view = [[UIView alloc] init];
CGRect frame = CGRectZero;
frame.size = [self rowSizeForComponent:component];
view.frame = frame;
view.backgroundColor = UIColor.clearColor;
label = [[UILabel alloc] init];
label.font = self.customViewForRowFont;
label.backgroundColor = [UIColor clearColor];
frame = view.bounds;
frame.origin.x += 7;
frame.size.width -= 14;
label.frame = frame;
[view addSubview:label];
}
label = [view.subviews objectAtIndex:0];
#try {
NSString *text = [self.delegate pickerView:self titleForRow:row forComponent:component];
if (!text)
text = #"???";
label.text = text;
} #catch (NSException *e) {
label.text = #"???";
}
return view;
}
#end
Try this
https://github.com/kafejo/CCPicker
it's very easy picker :)