2012-02-07 51 views
1

我想添加一個不同的圖標圖像,即image1.png,image2.png等到以下UITableview陣列。真的需要某人的幫助。提前致謝。將圖標圖像添加到UITableview陣列

{ 
self = [super init]; 
if (self) { 
    // Custom initialization 
    self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",  @"County", @"City", @"District", 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; 
} 
+1

您創建的cellForRowAtIndexPath單獨的UITableViewCell實例:這是UITableViewDataSource協議的一部分。 – Rayfleck 2012-02-07 13:15:10

回答

4

使用的cellForRowAtIndexPath:

- (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:@"image1.png"]; 
    return cell; 
} 
4

你可以創建另一個數組保存圖片

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

你要顯示的順序,並在的cellForRowAtIndexPath的名字只是寫

cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]]; 
+0

非常感謝,我如何將上面的數組應用到我的代碼中,剩下的就剩下了。 – CraigBellamy 2012-02-07 13:29:58

+0

你只需要在頭文件中聲明數組併合成它就像tableData數組 – Radu 2012-02-07 14:56:41

+0

我將如何在頭文件中聲明數組?對不起,聽起來很愚蠢,但做得不好! – CraigBellamy 2012-02-07 17:40:29

1

您可以利用創建自定義類的oop樣式(例如DataItem)並使用DataItem元素初始化您顯示的數組。換句話說,您可以創建一個包含名稱和圖像元素的模型。

例如:

//.h 
@interface DataItem : NSObject 
{ 
    NSString* name; 
    NSString* thunbmail; 
} 

@property (nonatomic, copy) NSString* name; 
@property (nonatomic, copy) NSString* thunbmail; 

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail; 

@end 

//.m 
@implementation DataItem 

@synthesize name; 
@synthesize thunbmail; 

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail 
{ 
    if(self = [super init]) 
    { 
     name = [dName copy]; // release in dealloc!! 
     thunbmail = [dThunbmail copy]; // release in dealloc!! 
    } 
    return self; 
} 

// create dealloc here 

@end 

現在你可以初始化一個物品,並把它添加到陣列(也可能是最好有一個NSMutableArray)像下面這樣:

DataItem* di = [[DataItem alloc] initWithName:@"name" withThunbmail:@"image.png"]; 

NSMutableArray* arrData = [[NSMutableArray alloc] init]; 
[arrData addObject:di]; 

// add other object here 

self.tableData = arrData; 

// release memory... 

,然後在cellForRowAtIndexPath

- (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.. 

    DataItem* di = (DataItem*)[self.tableData objectAtIndex:[indexPath row]]; 
    cell.textLabel.text = di.name; 
    cell.imageView.image = [UIImage imageNamed:di.thunbmail]; 

    return cell; 
} 

這是一種優雅的方式來封閉你的cont在一個單一的班級模型。

希望它有幫助。

P.S.檢查代碼。我手寫的。

+1

這應該是接受的答案...! – Shailesh 2013-04-05 07:11:18

+0

@shailesh謝謝 – 2013-04-07 20:13:13