申し訳ありませんが、これは新しい質問ですが、一日中検索しても答えが見つかりません。私は、アプリケーションを実行すると、ラベルとカウントだけの行が表示されますが、画像がテーブルビューのセルに表示されません。デフォルトでは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;
}
で画像を設定し、XIBからこれを扱うことができますあなたのセルにUIImageViewを何度も追加して再利用しているので、この行も 'cell.imageView.image = imv.image;'は不要で、おそらくあなたの問題の原因です –
あなたが持っているimageviewを使うだけですユーザーインターフェイスでイメージビューを作成するか、サブビューを追加します。 'UIImageView * imv = [[UIImageView alloc] initWithFrame:CGRectMake(10,5,50,25)]; imv.image = [UIImage imageNamed:@ "2.jpg"]; [cell.contentView addSubview:imv]; ' –