从UITableView网站解析图像
问题描述:
我想通过网站解析图像到UITableViewCell。我可以很容易地解析一个图像,但我试图解析不同的多个图像。我对如何完成这个有一些想法,但我想知道什么是执行这样的任务的最简单和最好的方式。我怀疑这有助于,但icon.jpg被命名为像1_icon,2_icon等...任何提示将不胜感激。从UITableView网站解析图像
- (void)viewDidLoad
{
mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://example.com/uploads/1_icon.jpg"]];
myimage = [[UIImage alloc] initWithData:mydata];
_imageToDisplay.image=myimage;
self.tableview.delegate = self;
self.tableview.dataSource = self;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryView = [[UIImageView alloc] initWithImage:myimage];
return cell;
}
updated.m
_imagesArray = [[NSMutableArray alloc] init];
int numberOfIcons=30;
for(int i = 1; i <= numberOfIcons; i++){
[_imagesArray addObject:[[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/uploads/%d_icon.jpg", i]]]]];
NSLog(@"%d",numberOfIcons);
更新的.h
NSMutableArray *_imagesArray;
NSArray*array;
答
可以预加载所有的图像在阵列中并引用它们在创建单元时。这会阻止您在每次创建单元格时从Web上加载图像。
在viewDidLoad中:
_imagesArray = [NSMutableArray alloc] init];
for(int i = 1; i <= numberOfIcons; i++){
[array addObject:[[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/uploads/%d_icon.jpg", i]]]]];
}
在的cellForRowAtIndexPath:
cell.accessoryView = [[UIImageView alloc] initWithImage:[_imagesArray objectAtIndex:indexPath.row]];
对不起,我是想实现这一点,它没有工作过well.I力量的误解你的代码。我更新了我的代码,我想知道你能否告诉我你的想法?真的很感激。 –
你遇到什么错误? – danielM
- [__ NSArrayM objectAtIndex:]:index 1 beyond bounds [0 .. 0]'我将值切换到numberoficons = 34和i = 34以确保对象不是零。我相信这是导致错误的那一行“cell.accessoryView = [[UIImageView alloc] initWithImage:[_ imagesArray objectAtIndex:indexPath.row]];” –