2017-07-26 8 views
2

申し訳ありませんが、これは新しい質問ですが、一日中検索しても答えが見つかりません。私は、アプリケーションを実行すると、ラベルとカウントだけの行が表示されますが、画像がテーブルビューのセルに表示されません。デフォルトではtableviewcellに画像を追加する方法

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> { 
NSMutableArray * imageNameArray; 
//__weak IBOutlet UIImageView *cell; 
__weak IBOutlet UITableView *Table; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a  nib. 
[self arraySetup]; 


} 



-(void) arraySetup { 
imageNameArray = [NSMutableArray arrayWithArray:@[@"2.jpg"]]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { 
return imageNameArray.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *cellId = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 
//cell.imageView.image = [UIImage imageNamed:imageNameArray [indexPath.row]]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; 
} 


cell.textLabel.text = imageNameArray [indexPath.row]; 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1]; 

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)]; 
imv.image=[UIImage imageNamed:@"2.jpg"]; 
[cell.contentView addSubview:imv]; 
cell.imageView.image = imv.image; 

return cell; 

}

+1

で画像を設定し、XIBからこれを扱うことができますあなたのセルにUIImageViewを何度も追加して再利用しているので、この行も 'cell.imageView.image = imv.image;'は不要で、おそらくあなたの問題の原因です –

+0

あなたが持っているimageviewを使うだけですユーザーインターフェイスでイメージビューを作成するか、サブビューを追加します。 'UIImageView * imv = [[UIImageView alloc] initWithFrame:CGRectMake(10,5,50,25)]; imv.image = [UIImage imageNamed:@ "2.jpg"]; [cell.contentView addSubview:imv]; ' –

答えて

0

UITableViewCellUIImageViewを持っています。これを試してください:

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

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; 
} 


cell.textLabel.text = imageNameArray [indexPath.row]; 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1]; 

cell.imageView.image = [UIImage imageNamed:@"2.jpg"]; 
return cell; 
} 

もう1つの方法は、独自のカスタムTableViewCellを使用することです。

+0

これは私には役に立たなかった。それはただ2.jpgと1を示した。しかし、イメージは決して現れなかった。 –

+0

@ms_abcバンドルする画像やxcassetsをどこに追加しましたか? –

0

のviewDidLoadの配列を追加:おかげでここ

0

はあなたがから学ぶことのためのいくつかの非常に基本的なコードです....それを試してみてください

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellIdentifier", for: indexPath) as! CustomCell 
    // Static image 
    cell.checkedImageObj!.image = UIImage.init(named: "resort_selected") 

    //Want round Image.... 
    cell.contactImage.layer.cornerRadius = cell.contactImage.frame.size.width/2 
    cell.contactImage.layer.masksToBounds = true 

    // Set array of Image 
    cell.contactImage.image = (arrayValue[indexPath.row] as! NSDictionary).value(forKey: "userImage") as? UIImage 
} 

arrayValue = [[ "userEmailType": "Home", "userImage": "image1.png"], [ "userEmailType": "Home", "userImage": "image2.png"], [ "userEmailType": "Work", "userImage": "image3.png"],] 

カスタムUITableViewCellのコードを: 私は基本クラスを使用していますUITableViewCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.textLabel.text = @"Your table row text"; 
    cell.imageView.image = [UIImage imageNamed:@"image.png"];// Here you specify your image 
    cell.detailTextLabel.text = @"Your descriptive text"; 
    return cell; 
} 
+0

こんにちは、私はすでに私のコードでこれを持っており、それは動作しませんでした。 –

+0

あなたの間違いを犯しました。あなたは、あなたのtableViewのためのデータソースと委任を設定していません。 UIViewControllerでデータソースを使用する場合は、データソースを設定してtableViewのデリゲートを行う必要があります。チェックして、あなたのコードを編集! – Prashant

0

あなたのようなセルのinitメソッドでImageViewのを初期化する必要があります。

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; 
    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)]; 
    imv.tag = 10; 
    [cell.contentView addSubview:imv]; 
} 

、あなたは私はあなたがあなた自身のカスタムセルを作る示唆

cell.textLabel.text = imageNameArray [indexPath.row]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1]; 
    UIImageView *imv = (UIImageView *)[cell.contentView viewWithTag:10]; 
    imv.image=[UIImage imageNamed:@"2.jpg"]; 
    return cell; 
+0

こんにちは、これは私のために働いていませんでした。しかし、返信いただきありがとうございます。 –

関連する問題