【问题标题】:How to display UIImage within UITableView in IOS如何在IOS的UITableView中显示UIImage
【发布时间】:2014-02-04 11:27:37
【问题描述】:

我有NSMutableArray *_items。具有 3 个属性。标签 (*text) 有标签 1000。图像 1001。

@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) BOOL checked;
@property (nonatomic, copy) UIImage *image;

问题是我无法显示图像。

_items = [[NSMutableArray alloc] initWithCapacity:20];
ChecklistItem *item;
item = [[ChecklistItem alloc] init];
item.text = @"Walk the dog";
item.checked = NO;
item.image = [UIImage imageNamed:@"img1.png"];

其余代码

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
    ChecklistItem *item = _items[indexPath.row];
    UILabel *label = (UILabel *)[cell viewWithTag:1000];
    UIImage *image = (UIImage *)[cell viewWithTag:1001];
    image.image = item.image;
    label.text = item.text;
    [self configureCheckmarkForCell:cell atIndexPath:indexPath];
    return cell;
}

【问题讨论】:

    标签: ios objective-c xcode ios7


    【解决方案1】:

    在您的 cellForRowAtIndexPath: 实现中,您使用 UIImage 就好像它是一个视图,但事实并非如此。您可能打算使用 UIImageView。

    【讨论】:

      【解决方案2】:

      您需要创建自定义单元类并在那里连接您的属性 然后在 cellForRowAtIndexPath 中你可以设置这些属性

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      CustomCell *cell = (AddListTableViewCell *)[tableView
                                                                  dequeueReusableCellWithIdentifier:@"CustomCell"];
      if(cell==nil)
      {
          cell = (CustomCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
      }
      
      cell.myText.text = @"String1"
      cell.myUmage.image = [UIImage imageNamed:@"one.png"];
      
      
      
      return  cell;
      
      }
      

      【讨论】:

        【解决方案3】:

        您需要使用UIImageView 来显示图像,而不是UIImage

        @property (nonatomic, strong) UIImageView *imageView;
        

        还为这些属性使用strong 属性属性,而不是copy

        【讨论】:

          【解决方案4】:

          添加这一行:

          cell.imageView.image = [UIImage imageNamed:@"Your Image Here"];
          

          【讨论】:

            【解决方案5】:

            你为什么要保存 UIImage ?只需将该属性更改为 NSString 并保存图像名称即可。

               @property (nonatomic, copy) NSString *imageName;
            

            然后在您的委托方法中,您可以使用该图像名称创建 UIImage 并在 UIImageView 中设置图像。

            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    static NSString *CellIdentifier = @"ChecklistItem";
                    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            
                    // Configure the cell...
                    ChecklistItem *item = _items[indexPath.row];
                    UILabel *label = (UILabel *)[cell viewWithTag:1000];
                    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1001];
                    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",item.imageName]];
                    label.text = item.text;
                    return cell;
                }
            

            【讨论】:

              猜你喜欢
              • 2014-12-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-13
              • 2018-03-08
              • 2021-12-31
              相关资源
              最近更新 更多