【问题标题】:UIImage is Not Displaying in UITableViewUIImage 未在 UITableView 中显示
【发布时间】:2014-12-28 13:34:47
【问题描述】:

我是 iOS 的新手.....我创建了一个表格视图以显示为联系人视图.....在这里我使用以下编码显示了姓名和号码,但我无法显示图像即使我使用了正确的代码.....请帮助我

names = [NSMutableArray arrayWithObjects:@"Karthick", @"Gopal", @"Suresh", @"Senthil", @"Guna",nil];

images = [NSMutableArray arrayWithObjects:@"per.png",@"per.png",@"per.png",@"per.png",@"per.png", nil];

num = [NSMutableArray arrayWithObjects:@"9568421301", @"8756324103", @"856472303", @"8796523565", @"9785858569",nil];

这是我的三个数组和

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [names count];
}



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

    contactTableViewCell *cell = (contactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[contactTableViewCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.name.text = [names objectAtIndex:indexPath.row];
    cell.number.text = [num objectAtIndex:indexPath.row];
    cell.conimage.image = [UIImage imageNamed:[names objectAtIndex:indexPath.row];
    return cell;
}

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview uiimageview


    【解决方案1】:

    我看到了可能的问题:

    UIImage +imageNamed: 仅适用于添加到主包的图像。检查,如果你这样做了。

    From the docs:

    声明

    + (UIImage *)imageNamed:(NSString *)name
    

    参数
    名称 文件的名称。如果这是第一次加载图像,该方法会查找具有指定名称的图像 在应用程序的主包中。

    另一个问题可能是 UIImageView conimage 从未创建。
    如果是这种情况,cell.conimage.image = [UIImage imageNamed:[names objectAtIndex:indexPath.row]]; 将与[nil setImage:aImage]; 相同,并且与 Java 等其他语言相比,这是一个有效的调用。发送到 nil 对象的任何消息都将导致 nil — 而不是异常。
    为了能够看到,如果确实如此,您将不得不解释(最好使用代码)您的自定义单元类的外观。

    【讨论】:

      【解决方案2】:

      您必须在cellForRowAtIndexPath 方法中设置 images 而不是 names,如下所示

          cell.conimage.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row];
      

      将cellForRowAtIndexPath中的行替换为上面的行

      【讨论】:

      • sry 老板......我在添加代码 sn-p 时犯了错误......但在实际代码中我只使用了图像......然后我也没有得到图片。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多