這可能是一個愚蠢的問題,但我現在真的一直在努力。我創建了一個UITableView以編程方式獲得所有數據完美流動,現在我只想在表格頂部添加一個靜態圖像,但似乎無法做到這一點:-(我知道這可能是非常基本的,但我真的在TableView頂部添加圖片
1
A
回答
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;
}
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
相關問題
- 1. 圖片在的tableview的頂部
- 2. 如何在另一張圖片的頂部添加圖片?
- 3. ios:我想在tableview頂部和底部添加邊框
- 4. ios:如何在tableview中添加頂部和底部邊框?
- 5. 在導航欄固定頂部之前添加標題圖片
- 6. 在添加項目的同時滾動tableview至頂部
- 7. 如何在UIPopoverController頂部添加視圖
- 8. 在CATiledLayer頂部添加UIImage
- 9. 在JPanel頂部添加JLabel
- 10. UITableView在頂部添加行
- 11. 如何在列表視圖的頂部添加添加數據
- 12. 在按下按鈕時在TableView單元格上添加圖片
- 13. 在Swift中添加圖片到Firebase中的TableView單元格
- 14. 添加在圖片
- 15. 如何在TableView部分添加UILabel
- 16. 將div背景圖片放在頂部
- 17. IE-7圖片不在頂部
- 18. CSS把圖片放在頁面頂部
- 19. 在運行時在圖像視圖頂部添加按鈕
- 20. 在列表視圖頂部添加「正在載入」圖像
- 21. 如何在現有標題圖片頂部添加URL和按鈕
- 22. 如何將標題添加到tableview的頂部和每個部分
- 23. MPAndroidChart - 添加偏移頂部
- 24. 添加節點頂部
- 25. C#winforms圖片框,背景圖片放大在頂部?
- 26. Android在這張圖片的頂部繪製圖片(動態)
- 27. 滾動視圖在彼此頂部添加視圖
- 28. 如何在視圖頂部添加模糊視圖?
- 29. 如何在UINavigation欄頂部添加橫幅/圖像視圖
- 30. 如何在多尺度圖像頂部添加圖形?
您好,非常感謝您的回覆,其實我根本不想要的選項卡內的圖像,我的表在Y30px開始,所以我有在上面一個空的空間,這就是我想要的放一個靜態圖像。 –
我猜想另一種方法是如何在座標(0,0,320,30)上添加圖像? –
你試過使用界面生成器嗎?我知道我的一個應用程序是我做的,我所做的只是在桌子上放置了一個UIPictureView(從內存中不記得圖片視圖的確切名稱),並調整大小以適合然後選擇您的圖像。我認爲我錯過了一些東西,因爲你聽起來很熟練,而且這是一個非常簡單的方法 –