【问题标题】:create NSMutableArray for store image from url in tableview从 tableview 中的 url 创建 NSMutableArray 以存储图像
【发布时间】:2013-03-31 09:35:12
【问题描述】:

我想在数组中存储来自 url 的 4 张图书图片(.php?p=1&b=1 显示第一本书的第一张图片)并运行该代码并在 tableview 中使用这些图像。

我还想在 tableview 中创建任何单元格并将第一个图像专用于第一个单元格(第二个图像用于第二个单元格等)

这是我的代码:

#import "CarTableViewController.h"
#import "CarTableViewCell.h"

@implementation CarTableViewController
{
    NSData *b;
}
@synthesize carImages;
@synthesize carModels; 
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *numberbook =[[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.101/mamal/book.php?all"]];

    NSInteger numbook = [numberbook integerValue];

    NSString *a;

    for (int i = 1; i <=numbook; i++) {
        a = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/book.php?info=1&b=%d",i]]];
        NSLog(@"%@",a); //names of book

        if(!carModels){
            carModels = [NSMutableArray array];
        }
        [carModels addObject:a];

        b = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/book.php?p=1&b=%d",i]]];
        NSLog(@"%@",b);
        if (!carImages) {
            carImages = [NSMutableArray array];
        }
        [carImages addObject:b];
    }
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"carTableCell";
    CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CarTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.modelLable.text = [carModels objectAtIndex:[indexPath row]];

    UIImage *carPhoto = [UIImage imageWithData:b];
    cell.carImage.image = carPhoto;

    return cell;
}

但是当运行此代码模拟器时,在 tableview 中显示 4 个具有 4 个不同名称的单元格(正确!我想要这个)和任何单元格的最后一个图像!!!!????呵呵

【问题讨论】:

    标签: objective-c uitableview uiimageview nsmutablearray


    【解决方案1】:

    这是因为您对所有单元格重复使用相同的 NSData *b 变量(因此它们显示 b 的最新状态,即最后一张图片)

    进行以下更改

    而不是

    UIImage *carPhoto = [UIImage imageWithData:b];
    

    UIImage *carPhoto = [[UIImage imageWithData:[carImage objectAtIndex:indexPath.row]];
    

    【讨论】:

      【解决方案2】:

      每次加载新图像时,保存图像数据的实例变量b 都会被覆盖,因此最后加载的就是数据所在的那个。

      b = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/book.php?p=1&b=%d",i]]];
      NSLog(@"%@",b);
      if (!carImages) {
          carImages = [NSMutableArray array];
      }
      [carImages addObject:b];
      

      配置单元格时,将图像设置为此数据,而不是访问carImages 数组。

      就像您对carModels 所做的那样: cell.modelLable.text = [carModels objectAtIndex:[indexPath row]];

      UIImage *carPhoto = [UIImage imageWithData:[carImages objectAtIndex:[indexPath row]]];
      cell.carImage.image = carPhoto;
      

      两个改进建议,如果可以的话:

      1. b 设为局部变量。无需将其作为实例变量。您已经看到,这可能会导致混乱,从而导致错误。
      2. 在加载数据后立即创建UIImage 并将该图像放入carImages 数组中。这样,您只需解析每个图像的数据一次,而不是每次显示单元格时。

        // ...
        NSData* b = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/book.php?p=1&b=%d",i]]];
        NSLog(@"%@",b);
        if (!carImages) {
            carImages = [NSMutableArray array];
        }
        UIImage *img = [UIImage imageWithData:b];
        [carImages addObject:img];
        //...
        

        然后,在配置单元格时,您可以这样做

        cell.carImage.image = [carImages objectAtIndex:[indexPath row]];
        

      【讨论】:

        猜你喜欢
        • 2016-12-01
        • 2016-09-27
        • 1970-01-01
        • 2017-01-14
        • 2012-05-31
        • 1970-01-01
        • 2022-09-27
        • 2014-05-25
        • 1970-01-01
        相关资源
        最近更新 更多