Ios 如何将图像添加到tableviewcell

Ios 如何将图像添加到tableviewcell,ios,objective-c,uitableview,uiimageview,Ios,Objective C,Uitableview,Uiimageview,抱歉,这是一个如此新的问题,但我搜索了一整天,仍然找不到答案。当我运行应用程序时,它只显示带有标签和计数的行,但图像不会显示在表格视图单元格中 #import "ViewController.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate> { NSMutableArray * imageNameArray; //__weak IBOutlet UIImageView *c

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

#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;
#导入“ViewController.h”
@界面视图控制器(){
NSMutableArray*imageNameArray;
//__弱IBUIImageView*单元;
__弱IBUITableView*表;
}
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[自排列设置];
}
-(无效)阵列设置{
imageNameArray=[NSMutableArray arrayWithArray:@[@“2.jpg”];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回imageNameArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*cellId=@“cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellId];
//cell.imageView.image=[UIImage ImageName:imageNameArray[indexath.row]];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellId];
}
cell.textlab.text=imageNameArray[indexath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@“%d”,(int)indepath.row+1];
UIImageView*imv=[[UIImageView alloc]initWithFrame:CGRectMake(10,5,50,25)];
imv.image=[UIImage ImageName:@“2.jpg”];
[cell.contentView addSubview:imv];
cell.imageView.image=imv.image;
返回单元;

}

默认情况下,
UITableViewCell
具有
UIImageView
。试试这个:

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

另一种方法是使用您自己的自定义TableViewCell。

在ViewDidLoad中添加数组:

arrayValue = [[ "userEmailType": "Home", "userImage": "image1.png"], [ "userEmailType": "Home", "userImage": "image2.png"], [ "userEmailType": "Work", "userImage": "image3.png"],]
自定义UITableViewCell代码:

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
}

请试一试。。。。谢谢

以下是一些非常基本的代码供您学习: 我使用的是基本类
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;
} 

必须使用单元格的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;

我知道这是一个很晚的回复,现在只有很少的人使用Objective C。下面是我的工作。请注意,我正在重复使用相同的tableView来填充通讯簿,并在单击每个通讯簿时加载其联系人。因此,您可以删除if条件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cells";
 UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
 
    
    if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
                    }
    
     if (_isPhoneBooksLoaded){
         cell.imageView.image =nil;
     }
    
    if (_isContactsLoaded){
        
        cell.imageView.image = [UIImage imageNamed:@"contact_default.png"];
    }
   
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    
    return cell;
}
重要:

  • 不要忘记使用您设置的相同单元格标识符

  • 如果出现以下情况,请删除:电话簿已添加,联系人已添加,请保留

  • cell.imageView.image=[UIImage ImageName:@“contact_default.png”]

  • 确保您使用的图像文件名正确,并且它存在于您的资源中

  • 我建议您创建自己的自定义单元格,并从xib或故事板处理此问题,因为在重复使用单元格时,您会在单元格中反复添加UIImageView,这一行也是
    cell.imageView.image=imv.image是不必要的,可能是问题的根源。请简单地使用您在用户界面中创建的imageview或添加子视图。”UIImageView*imv=[[UIImageView alloc]initWithFrame:CGRectMake(10,5,50,25)];imv.image=[UIImage ImageName:@“2.jpg”];[cell.contentView addSubview:imv];'你好,这对我不起作用。但是谢谢你的回复。这对我来说不起作用。它只显示了2.jpg和1。但是该图像从未出现。@ms_abc您在哪里将图像添加到捆绑或xcassets?您好,我的代码中已经有了该图像,但它不起作用。必须!我理解你的错误。您尚未为tableView设置数据源和委托。在UIViewController中使用tableView时,需要为tableView设置数据源和委托。编辑您的代码,请检查!