2017-07-26 66 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

设定的图像,我建议你做你自己的自定义单元格,并从厦门国际银行办理本或故事板,因为你重复添加一个UIImageView在你的单元格重用时,也是这行'cell.imageView.image = imv.image;'是不必要的,也许是你的问题的根源 –

+0

只需使用imageview你有在用户界面中创建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; 
} 

另一种方法是使用你自己的Custom TableViewCell。

+0

这不适合我。它只显示2.jpg和1.但图像从未出现过。 –

+0

@ms_abc您将图像添加到捆绑包或xcassets的位置? –

0

附加数组中的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 
} 

请您尝试一下....谢谢

0

这里是供您学习一些非常基本的代码: 我正在使用基本类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

你好,这不适合我。但谢谢你的回复。 –