2013-04-11 80 views
1

作爲Xcode初學者,我想在我的應用程序的表格單元格中顯示縮略圖。現在,我有了解析JSON數據並將其發佈到單元格的標題和副標題中的代碼。如何在xCode中將縮略圖圖像顯示到表單元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"receta"]; 
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"koha"]; 

    return cell; 
} 

如何顯示單元格右側的縮略圖?

謝謝。

+0

創建一個自定義單元,您可以使用您自己的佈局。在Google上搜索自定義UITableViewCells。 – Popeye 2013-04-11 14:58:14

+0

Popeye,你會介意幫助我嗎?我搜索了,但我沒有找到對我有用的東西,因爲我對Xcode有一些瞭解,這是我第三次今天打開它。 – 2013-04-11 15:00:27

+0

我會推薦一些教程比。這裏是一個很好的教程http://blog.spritebandits.com/2012/03/13/creating-custom-uitableviewcells-from-xibs-step-by-step-tutorial/ – Popeye 2013-04-11 15:05:11

回答

3

如果您不想製作自定義單元格,您可以創建一個帶有所需圖像的UIImageView,並將其設置爲單元格的accessoryView,該單元格將顯示在單元格的右邊緣。您還需要確保單元格的高度足夠適合圖像的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"some-image-name"]]; 

    return cell; 
} 

您可以返回一個常數(我選擇100)如果圖像將永遠是一個固定的大小,也可以檢查UIImagesize和返回類似size.height + 10一些額外的填充。

+0

我會傾向於說這不是什麼'accessoryView'是爲了,但它會完成這項工作。 +1用於出箱思考。 – Popeye 2013-04-12 07:06:33

+0

如果Amar不想製作自定義單元格,這是一種方法 – jszumski 2013-04-12 12:23:35

相關問題