2011-08-24 16 views
19

私はUITableViewを持っています。これはサーバーから情報を取得し、そのデータをテーブルビューに公開します。各セルの内部には、サーバーからの情報があります。この例の目的のためにプログラムでTableViewセルに画像を追加

、我々は、サーバから取得した情報は、数値であるとしましょう:私は何をしたいか1、2、3、4

は、画像を追加している(場合があるプログラムであるため文など)をセルの左側に、テキスト(1,2など)をセルの左側に配置します。

は基本的に、私は、各セルは次のようになりたい:私の粗製のイラストを言い訳

_____________________________________ 
| (IMAGE) (TEXT)      | --CELL 0 
-------------------------------------- 
_____________________________________ 
| (IMAGE) 2       | --CELL 1 
-------------------------------------- 

してください。 :)

答えて

19

あなたが行く:

cell.imageView.image = [UIImage imageNamed:@"image.png"]; 

スウィフト:

cell.imageView?.image = UIImage(named: "image.png") 
+0

問題は、私の中に固定する必要があること2015年9月24日の回答の編集 –

3

UITableViewCellオブジェクトにはimageViewプロパティがあります。この画像ビューの画像を設定するだけで、自動的に画像が適切な場所に表示され、タイトルの横にタイトルが表示されます。

8

あなたが画像などのようなあなたのUITableViewControllerのサブクラス内のUITableViewCellにテキストの両方を追加することができます。

- (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] autorelease]; 
    } 

    [[cell imageView] setImage:anImage]; 
    [[cell textLabel] setText:[NSString stringWithFormat:@"%d",[indexPath row]]; 

    return cell; 
} 

少数の「ビルトイン」があるあなたはの利点を取ることができることのUITableViewCellの性質は、 imageView、textLabel、およびdetailTextLabelとして指定します。詳細については、Table View Programming Guideをご覧ください。ここ

0
// create a rectangle box for image view 

UIImageView *iconImage = [[UIImageView alloc] init]; 
iconImage.frame = CGRectMake(12,4,53.5,53.5); 

// Create a label for cells 
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)]; 
cellName.font = [self fontForBodyTextStyle]; 

NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]]; 

// Select path row tab actions 
switch (indexPath.row) { 
    case 0: 

     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"condition.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 

     break; 

    case 1: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"videos.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 2: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"provider.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 3: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"info.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    default: 
     NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath"); 
     break; 
} 
5

の最初の答えのスウィフト2.0バージョンです:

cell.imageView?.image = UIImage(named: "image.png") 
関連する問題