【问题标题】:How can I use addObject in Array?如何在数组中使用 addObject?
【发布时间】:2014-05-25 18:09:26
【问题描述】:

我有一个 NSMutableArray *arrayFavorite

在我看到的日志中

{      name1 = 6;
       name2 = "Paolo Rossi\nIt\U00e1lia";
       name3 = "cartaz-1982.jpg";
       photo = "copa8.jpg";
       name4 = 24;
   },
    {
       name1 = 6;
       name2 = "Oleg Salenko\nR\U00fassia";
       name3 = "cartaz-1994.jpg";
       photo = "copa5.jpg";
       name4 = 24;
   },

我需要改造一下

self.tableItems = @[[UIImage imageNamed:@"photo1.jpg"],
                       [UIImage imageNamed:@"photo2.jpg"],
                       [UIImage imageNamed:@"photo3.jpg"]];

从我的 *array 收藏夹中获取动态信息照片名称

我该怎么做?

谢谢

【问题讨论】:

  • 您是否在问如何从数组arrayFavorite 中获取值photo,然后使用它来填充+ (UIImage *)imageNamed:(NSString *)name 的名称参数?

标签: ios objective-c arrays nsmutablearray


【解决方案1】:

您需要做的就是枚举字典数组并使用photo keyValue 到+imageNamed:... 创建一个图像。

NSArray *imageInfoDicts = ...;
NSMutableArray *images = [NSMutableArray array];
[imageInfoDicts enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger idx, BOOL *stop) {
    [images addObject[UIImage imageNamed:dictionary[@"photo"]]];
}];
NSLog(@"%@", images);

【讨论】:

    【解决方案2】:

    由于您似乎在 tableView 中显示此数据,您可能应该获取特定行的数据,然后在该时间点获取图像以避免一次将所有图像加载到内存中。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<identifier> forIndexPath:indexPath];
    
      NSDictionary *item = self.items[indexPath.row];
    
      cell.imageView.image = [UIImage imageNamed:item[@"photo"]];
    
      return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多