1

我想在数组中存储来自 url 的 4 本书图像(.php?p=1 & b=1显示第一本书的第一个图像)并运行该代码并在 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 个不同名称的单元格(正确!我想要这个)和任何单元格的最后一个图像!!!!????XD

4

2 回答 2

0

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]];
    
于 2013-03-31T16:28:48.443 回答
0

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

进行以下更改

代替

UIImage *carPhoto = [UIImage imageWithData:b];

UIImage *carPhoto = [[UIImage imageWithData:[carImage objectAtIndex:indexPath.row]];
于 2013-03-31T16:30:09.723 回答