2011-08-26 78 views
1

這可能是一個愚蠢的問題,但我現在真的一直在努力。我創建了一個UITableView以編程方式獲得所有數據完美流動,現在我只想在表格頂部添加一個靜態圖像,但似乎無法做到這一點:-(我知道這可能是非常基本的,但我真的在TableView頂部添加圖片

回答

0

所以通常你會創建一個UITableViewController,它將委派表的工作方式(這是numberOfSectionsInTableView,numberOfSectionsInTableView等)的方法,使單元本身不同於默認的u需要創建的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 
} 

然後回到你的UITableViewController:的cellForRowAtIndexPath方法ü應分配烏爾自定義單元格

- (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; 
} 
+0

您好,非常感謝您的回覆,其實我根本不想要的選項卡內的圖像,我的表在Y30px開始,所以我有在上面一個空的空間,這就是我想要的放一個靜態圖像。 –

+0

我猜想另一種方法是如何在座標(0,0,320,30)上添加圖像? –

+0

你試過使用界面生成器嗎?我知道我的一個應用程序是我做的,我所做的只是在桌子上放置了一個UIPictureView(從內存中不記得圖片視圖的確切名稱),並調整大小以適合然後選擇您的圖像。我認爲我錯過了一些東西,因爲你聽起來很熟練,而且這是一個非常簡單的方法 –

4

嗯,我結束了在代碼做它,就是這樣。

- (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; 
} 
0
-(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; 
} 
+0

從實際答案複製並粘貼 – scott