Reputation: 1777
I have a table view which loads another xib file and uses the content of that new xib file to display the header. In this new xib I have a UIImageView and a label. I want to be able to change the image and the label from view did load. I have linked up everything in interface builder properly. It just won't change the image or the text label.
////h file///
#import <UIKit/UIKit.h>
#import "MyProfile.h"
@interface MainMenu : UITableViewController
{
IBOutlet UIView *headerView;
UIImageView *imageview;
UILabel *label;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageview;
@property (nonatomic, retain) IBOutlet UILabel *label;
-(UIView *)headerView;
-(IBAction)fn:(id)sender;
@end
///// partial .m file////
-(UIView *)headerView
{
if(!headerView)
{
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
}
return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
return [self headerView];
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return [[self headerView] bounds].size.height;
}
-(id) init
{
self = [super initWithStyle:UITableViewStyleGrouped];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self navigationItem] setTitle:@"The Table"];
menuitems = [[NSMutableArray alloc] init];
[menuitems addObject:@"item one"];
[menuitems addObject:@"item two"];
fieldnames = [[NSMutableArray alloc] init];
[fieldnames addObject:@"Date of Birth"];
[fieldnames addObject:@"Nationality"];
label.text = @"new label value will not be displayed!";
}
Why won't the text label update?
I have set the header view's xib file's owner to be the main menu and I have connected the label in header view.xib up to the IBOutlet.
Thanks.
Upvotes: 2
Views: 3989
Reputation: 1777
Ok I solved it. I don't know If this is the way that you are supposed to do it but it works.
If you stick
label.text = @"new label value will not be displayed!";
in
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self headerView];
}
and then reload the xib inside viewdidload:
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
it works. Is this the way you are supposed to do it?
Thanks
Upvotes: 1
Reputation: 2144
Use this method to add header in table view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,10,tableView.frame.size.width,21)] autorelease];
UILabel *feelingDate;
feelingDate = [[UILabel alloc] init];
feelingDate.textAlignment = UITextAlignmentLeft;
feelingDate.textColor=[UIColor whiteColor];
feelingDate.font=[UIFont boldSystemFontOfSize:14];
feelingDate.text= [keyArray objectAtIndex:section];
feelingDate.frame=CGRectMake(10,-5,tableView.frame.size.width,21);
feelingDate.backgroundColor = [UIColor clearColor];
[headerView addSubview:feelingDate];
[feelingDate release];
return headerView;
}
Upvotes: 2