2012-04-28 15 views
0

我想在tableview中添加圖像。 我創建了一個UITableViewCell對象。在.h文件中如何在數組中添加圖像tableview

@interface MainView1 : UITableViewCell{ 

IBOutlet UILabel *cellText; 

IBOutlet UIImageView *productImg; 

IBOutlet UILabel *cellText1; 

} 

- (void)LabelText:(NSString *)_text; 

- (void)LabelText1:(NSString *)_text; 

- (void)ProductImage:(NSString *)_text; 

@end 

和.m文件

- (void)LabelText:(NSString *)_text;{ 

    cellText.text = _text; 
} 

- (void)LabelText1:(NSString *)_text;{ 

    cellText1.text=_text; 
} 


    - (void)ProductImage:(NSString *)_text;{ 

productImg.image = [UIImage imageNamed:_text]; 
    } 

和主文件

//頁面包含表

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MainView1 *cell = (MainView1 *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; 
if(cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"MainView1" owner:self options:nil]; 
    cell = tableCell1; 
    [cell LabelText:[arryList objectAtIndex:indexPath.row]]; 
[cell ProductImage:[imgNameArray objectAtIndex:indexPath.row]]; 

    [cell LabelText1:[yesArray objectAtIndex:indexPath.row]]; 

} 

圖像陣列從數據庫 未來像這

NSData *imgdata=[[NSData alloc]initWithBytes:sqlite3_column_blob(cmp_sqlstmt,0)  length:sqlite3_column_bytes(cmp_sqlstmt,0)]; 

[imgNameArray addObject:dataImage]; 

的問題是隻有圖像不顯示 錯誤顯示爲 無法識別的選擇發送到實例0x4e53940'

如何解決呢 問候 KL白駒

+0

方法聲明,這是什麼 - (空)ProductImage:(的NSString *)_文本; {}怎麼可能u能夠運行此代碼。 – freelancer 2012-04-28 07:16:50

+0

檢查此鏈接http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-add-uiimage-and-uilabel-in-the-uitableview/ – akk 2012-04-28 10:52:45

回答

5

從數據庫中,你得到的圖像形式爲NSData。你將它保存在你的數據源數組中。

但是,你把它作爲NSString

[cell ProductImage:[imgNameArray objectAtIndex:indexPath.row]];

其中

- (void)ProductImage:(NSString *)_text;{ 

productImg.image = [UIImage imageNamed:_text]; 

} 

你基本上需要的方法參數更改爲NSData

- (void)ProductImage:(NSData *) imageData{ 

    productImg.image = [UIImage imageNamed:_text]; 

} 

但這仍然會工作。因爲[UIImage imageNamed:]方法試圖從主包中獲取命名圖像。但在你的情況下,你需要從你有數據的圖像。所以你的最終方法應該是這樣的。

- (void)ProductImage:(NSData *) imageData{ 

     productImg.image = [UIImage imageWithData: imageData]; 

} 

也別忘了在你的頭文件更改爲

- (void)ProductImage:(NSData *) imageData;

相關問題