2010-04-22 24 views
0

我想使用cell.imageView setImage:方法添加圖像。但是,根據數組中的字段(以NSString的形式並使用[狗類型]調用),我需要單元格左側的圖像更改爲反映狗類型的圖像。使用IF語句更改UITableView細胞圖像?

我該如何去做這件事?謝謝。

回答

0

我認爲你可以去死的可以做你想做的事情。

通過的cellForRowAtIndexPath方法chacking條件:

if(indexPath.row == 0) // for first cell of tableView 

if(indexPath.row == 1) // for second cell of tableView 

希望你能理解......

即使任何問題發表評論......如果你有解決標記爲正確的...

1

正如你所知道的填充單元格的內容,你必須使用委託方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

所以,你必須把你的IF放在這個方法中,並使用indexPath.row來理解你正在繪製的女巫行。 這裏一個簡單的例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease]; 
    } 
    UIImage *a,*b; //Remeber to init with your images:) 
    if(indexPath.row == 1) 
    { 
     [cell.imageView setImage:a]; 
    } 
    else 
    { 
     [cell.imageView setImage:b]; 
    } 
    return cell; 
} 

我希望這是你在找什麼:)

+0

你可以標記答案解決? thx :) – Cesar 2010-04-23 04:03:29

+0

嗨,它實際上並沒有解決我的問題,但:)我需要做的是使用IF語句,但如果它匹配[狗類型]顯示圖像。例如,信息從服務器下載,並被解析成[狗類型]。然後,我需要的是如果狗類型= collie然後顯示collie.png類的東西! – Graeme 2010-04-23 05:48:36