How to change the Size of SectionIndexTitles in UITableView? - iphone

Can any one tell me, is it possible to change the default size of sectionIndexTitles in UITableView? If Yes then how?

for(UILabel *aView in [tableView subviews])
{
NSLog(#"frame:%#",aView);
if([[[aView class] description] isEqualToString:#"UITableViewIndex"])
{
aView.font=[UIFont fontWithName:#"Helvetica-Bold" size:18.0];
}
}
You can customise this as per your needs.

You need to implement
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
in your UITableViewDelegate as described here
Here's an example:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
UILabel * headerLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)];
[headerLabel setBackgroundColor:HEXCOLOR(0x952039FF)];
[headerLabel setTextColor:[UIColor whiteColor]];
[headerLabel setShadowColor:[UIColor grayColor]];
CGRect frame = headerLabel.frame;
frame.origin = CGPointMake(20,frame.origin.y);
headerLabel.frame = frame;
[headerLabel setText:[self sectionTitles:section]];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
sectionTitles refers to a simple function returning the section titles.

Related

add textfield to tableviewheader

I am trying to add textfield to header view. I could not figure out why I can't see my textfield. When I use a label it works perfectly.
Following is the code:
-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(40, 0, self.view.frame.size.width - 70, 30)];
UITextField *sectionTitleTF1 = [[UITextField alloc] initWithFrame:CGRectMake(58, 0, 500, 30)];
sectionTitleTF1.backgroundColor = [UIColor whiteColor];
[sectionTitleTF1 becomeFirstResponder];
[tableHeaderView addSubview:sectionTitleTF1];
return tableHeaderView;
}
Thanks
Try this,
You can adjust view, textfield frame based on your device ipad or iphone
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30.0;
}
-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(40, 0, self.view.frame.size.width - 70, 30)];
tableHeaderView.backgroundColor =[UIColor grayColor];
UITextField *sectionTitleTF1 = [[UITextField alloc] initWithFrame:CGRectMake(58, 0, 500, 30)];
sectionTitleTF1.backgroundColor = [UIColor whiteColor];
[sectionTitleTF1 setBackgroundColor:[UIColor whiteColor]];
[sectionTitleTF1 setFont:[UIFont boldSystemFontOfSize:15]];
[sectionTitleTF1 setBorderStyle:UITextBorderStyleLine];
[sectionTitleTF1 setTextAlignment:UITextAlignmentCenter];
[sectionTitleTF1 setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[sectionTitleTF1 becomeFirstResponder];
[tableHeaderView addSubview:sectionTitleTF1];
return tableHeaderView;
}
Have you implemented this method of the datasource? It is a must if you want your section header to show.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Also, if you are unaware, you can return the UITextField directly rather than adding as a subview. However you may want it as a subview, depending on what you want to achieve.
EDIT: Datasource not Delegate
just add this
sectionTitleTF1.borderStyle = UITextBorderStyleRoundedRect;
then you will see it

Center vertically UILabel text in table view section header

How to change position of the text in tableview section? I use viewForHeaderInSection:Section and i create there somwthing like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Gotham-Bold" size:17.0f];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.comlpetedFetchedResultsController sections] objectAtIndex:section];
label.text = [#" " stringByAppendingString:[sectionInfo name]];
return label;
}
It doesnt matter what size is the parameters in UILabel frame, i always get the same result:
How can i center text in UILabel vertically?
Thanks
Try
[yourLabel setNumberOfLines:0];
[yourLabel sizeToFit];
[yourLabel setContentMode:UIViewContentModeCenter];
Put the label in a UIView then return the view:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
[view addSubview:label];
return view;
Make sure you implement the following method:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
otherwise your view will simply be on top of the cells.
Go through this link: NSTextAlignment
Also code snippet given below, may help you
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:#"Abc"];
[myLabel setTextAlignment:NSTextAlignmentCenter];

changing the title of subviews of headerView

I have the following code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frameWidth, 24.0)];
[header setBackgroundColor:[UIColor grayColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.view.frameWidth - 100, header.frameHeight)];
label.backgroundColor= [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:17.0];
[header addSubview:label];
if (section == kEditInformationSection){
label.text = [AHConstants kEditInformation];
UIButton *editInformationButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frameWidth - 50, 0, 50, header.frameHeight)];
[editInformationButton setTag:1000];
[editInformationButton setBackgroundColor:[UIColor grayColor]];
[editInformationButton setTitle:[AHConstants kEdit] forState:UIControlStateNormal];
[editInformationButton addTarget:self action:#selector(switchToEditMode:) forControlEvents:UIControlEventTouchUpInside];
self.editInformationButton_ = editInformationButton;
[header addSubview:self.editInformationButton_];
self.tableHeaderView_ = header;
return self.tableHeaderView_;
} else if (section == kNotificationIntervalInfo){
label.text = [AHConstants kNotificationInterval];
return header;
}
return nil;
}
and on the button action, I wanted to change the button title so I did the following:
[self.editInformationButton_ setTitle:[AHConstants kDone] forState:UIControlStateNormal];
Why doesn't this work?
Use reloadSections:withRowAnimation
//provide your index for section here
[[self yourTableView]reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
Just handle these method with new title:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

How can we change the font of tableview header?

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.

Selected state of UIButton in UITableView

I have UIButton in each cell of UITableView. When I touch it, its state is set to selected. But when I scroll table view so the button isn't visible, the state is set to normal. How can I do it that UIButton remain selected?
Thank you.
Edit: Here is the cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row < [messagesArray count]) {
Zprava *msgObj = [messagesArray objectAtIndex:indexPath.row];
int width = 0;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
width = 320;
} else {
width = 480;
}
CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);
CGSize requiredSize = [msgObj.mmessage sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
UIButton *background = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 300, requiredSize.height + 20)];
[background setBackgroundImage:[[UIImage imageNamed:#"balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal];
[background setBackgroundImage:[[UIImage imageNamed:#"selected-balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateSelected];
[background addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
background.tag = msgObj.msgID;
[[cell contentView] addSubview:background];
[background release];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 285, 15)];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setTextColor:[UIColor lightGrayColor]];
[dateLabel setNumberOfLines:0];
[dateLabel setTextAlignment:UITextAlignmentRight];
[dateLabel setText:msgObj.mdate];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setOpaque:NO];
[[cell contentView] addSubview:dateLabel];
[dateLabel release];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 275, requiredSize.height + 5)];
[messageLabel setFont:[UIFont systemFontOfSize:17]];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setNumberOfLines:0];
[messageLabel setText:msgObj.mmessage];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setOpaque:NO];
[[cell contentView] addSubview:messageLabel];
[messageLabel release];
}
return cell;
}
Save button states somewhere in your model separately from table view and set buttons state in cellForRowAtIndexpath: method again.