2012-02-07 82 views
-4

我需要某人的幫助聲明如下數組: -如何聲明數組

self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil]; 

爲: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

// Configure the cell. 
cell.textLabel.text = @"cell text"; 
cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]]; 
return cell; 
} 

我怎麼做我申請它的.h和.m文件?我對此很新穎。

在此先感謝:)

PS。當我申請以下我得到「pic1.png」重複,沒有數組。

{ 
self = [super init]; 
if (self) { 
// Custom initialization 
self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",  @"County", @"City", @"District", nil]; 
self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil]; 
CGRect frame = self.view.bounds; 
frame.size.height -= 100; 
self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped]; 
[self.tableView setBackgroundColor:[UIColor clearColor]]; 
[self.tableView setDataSource:self]; 
[self.tableView setDelegate:self]; 
[self.tableView setScrollEnabled:NO]; 

[self.view addSubview:self.tableView]; 
} 
return self; 
} 
+2

等待之後,有什麼問題嗎? – 2012-02-07 21:26:08

+0

數組看起來不錯,請檢查下面的.h&.m下面的@beryllium答案。但是,你的意思是沒有細胞被輸​​出?如果是的話,你已經實現了UITableView數據源,並有這些方法: - numberOfSectionsInTableView: - tableView:numberOfRowsInSection:? – jodm 2012-02-07 21:33:28

回答

1

.H

@property (nonatomic, retain) NSArray *tablePicture; 

.M

@synthesize tablePicture; 

... 

-(void)dealloc{ 
    [tablePicture release]; 
    [super dealloc]; 
} 
+0

這是偉大的,但我似乎只獲得第一個「pic1.png」爲每個相應的表單元格重複 – CraigBellamy 2012-02-07 21:50:32

+0

感謝它的工作原理:) – CraigBellamy 2012-02-07 22:05:09

0

聲明您數組的屬性在視圖控制器的.h文件中:

@property (nonatomic, retain) NSArray *tablePicture; 

設置的值你的數組屬性在您的視圖控制器的.m文件中方法:

@synthesize tablePicture; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil]; 
} 
+0

感謝它的一半作品,但不重複只是「pic1.png」重複自己。 ..見上面的編輯 – CraigBellamy 2012-02-07 21:56:43

+0

你的tableView中有多個部分嗎?如果對'numberOfSectionsInTableView:'和'numberOfRowsInSection:' – jonkroll 2012-02-07 22:06:52

+0

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {0}返回self.tableData.count; (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { } return 1; } – CraigBellamy 2012-02-07 22:36:34

0

聽起來這個問題可能是你的數組初始化之外。

你爲什麼不嘗試添加:

NSLog(@"%@",self.tablePicture); 

"self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];" 
+0

我試過了,但還是隻有pic1.png重複自己:( – CraigBellamy 2012-02-07 22:27:59