对不起,这是一个新问题,但我整天搜索,仍然无法找到答案。当我运行该应用程序时,它只显示带有标签和计数的行,但图像未显示在表视图单元格中。如何添加图像到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;
}
设定的图像,我建议你做你自己的自定义单元格,并从厦门国际银行办理本或故事板,因为你重复添加一个UIImageView在你的单元格重用时,也是这行'cell.imageView.image = imv.image;'是不必要的,也许是你的问题的根源 –
只需使用imageview你有在用户界面中创建imageview或添加子视图。 'UIImageView * imv = [[UIImageView alloc] initWithFrame:CGRectMake(10,5,50,25)]; imv.image = [UIImage imageNamed:@“2.jpg”]; [cell.contentView addSubview:imv];' –