OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将图像添加到uitableviewcell [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:22
标题: ios - 如何将图像添加到uitableviewcell

抱歉,这是一个新问题,但我搜索了一整天仍然找不到答案。当我运行该应用程序时,它仅显示带有标签和计数的行,但图像未显示在表格 View 单元格上。

#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)tableViewUITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return imageNameArray.count;
}


- (UITableViewCell *)tableViewUITableView *)tableView  cellForRowAtIndexPathNSIndexPath *)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;

}



Best Answer-推荐答案


这里有一些非常基本的代码供您学习: 我正在使用基本类 UITableViewCell

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
} 

关于ios - 如何将图像添加到uitableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317095/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4