ios - 如何在 iOS 中以编程方式在 UITableViewCells 中添加图像

标签 ios iphone objective-c uitableview

我需要以编程方式在 UITableViewCells 中添加不同的图像。我怎样才能做到这一点。我正在尝试一些代码。但图像未显示在 UITableViewCells 中。 这是我的代码。

-(void)viewDidLoad
{
    arrImages=[[NSMutableArray alloc]initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier=@"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) 

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];

    }

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];

    cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];

    return cell;
}

最佳答案

这可能是您想要的:

- (void)viewDidLoad
{
    self.arrImages = [[NSMutableArray alloc] initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"Cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
    }

    cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]];

    return cell;
}

P.S.:下次,请努力格式化源代码,解释哪里出了问题,你尝试了什么,你想完成什么。谢谢

关于ios - 如何在 iOS 中以编程方式在 UITableViewCells 中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348994/

相关文章:

iphone - 将 NSArray 写回 plist

iphone - 如何将NSArray存储在辅助存储上?

iphone - UIWebView execCommand 剪切、复制、粘贴

ios - 如何通过成功将音频保存到文件来知道 AVAudioRecorder 何时停止?

iphone - 使用 SBJSON 解析 JSON 字符串时出现内存泄漏

ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类

iphone - ASIHTTPRequest zlib解压

ios - AWS iOS SDK -> 缺少必需的架构 x86_64

ios - 我们如何在IOS中将标签栏放在屏幕顶部

ios - 如何通过跨设备同步唯一标识 EKEvent