我想在数组中存储来自 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