这可能是一个愚蠢的问题,但我现在真的一直在与它斗争一分钟。我以编程方式创建了一个UITableView,所有数据都完美流动,现在我只想在桌面上添加一个静态图像,似乎无法做到:-(我知道这可能是非常基本但我真的需要有人来帮助我。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
通常你会创建一个UITableViewController,它将委托表的工作方式(这是numberOfSectionsInTableView,numberOfSectionsInTableView等)的方法。为了使单元格本身不同,默认情况下你需要创建一个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;
}
答案 2 :(得分: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;
}