This probably a dumb question but i've really been struggling with it for a minute now. I created a UITableView programmatically got all the data flowing perfectly, now i just want to add a static image on top of the table and can't seem to be able to do 开发者_运维技巧it:-( I know this is probably very basic but i really need somebody to help me out with it.
Well i ended up doing it in code and here it is.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImage *myImage = [UIImage imageNamed:@"ranking_bar.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,320,30);
return imageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}
so normally u would create a UITableViewController that will delegate how the table works (this is where numberOfSectionsInTableView, numberOfSectionsInTableView, etc.) methods are. to make the cells themselves different then default u need to create a subclass of UITableViewCell
customcell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
UILabel *primaryLabel;
UIImageView *myImageView;
}
@property (nonatomic, retain) UILabel *primaryLabel;
@property (nonatomic, retain) UIImageView *myImageView;
@end
customcell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize primaryLabel,myImageView;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:20];
primaryLabel.backgroundColor = [UIColor blackColor];
primaryLabel.textColor = [UIColor redColor];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:myImageView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 300, 40);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[myImageView release];
[primaryLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
}
then back to your UITableViewController : cellforrowatindexpath method u should allocate ur custom cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSInteger row = [indexPath row];
NSString *pathString= [snowBoardArray objectAtIndex:row];
NSString *string2= @"Cell";
pathString = [pathString stringByAppendingString:string2];
cell.primaryLabel.text=[snowBoardArray objectAtIndex:row];
NSString *filePath = [[NSBundle mainBundle] pathForResource:pathString ofType:@"png"];
UIImage *theImage = [[UIImage alloc] initWithContentsOfFile:filePath];
cell.myImageView.image = theImage;
[theImage release];
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"urImg.png"]] autorelease];
imageView.frame = CGRectMake(5,5,320,30);
return imageView;
}
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
精彩评论