2013-05-30 37 views
1

我想通過網站解析圖像到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; 

回答

0

可以預加載所有的圖像在陣列中並引用它們在創建單元時。這會阻止您在每次創建單元格時從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]];  
+0

對不起,我是想實現這一點,它沒有工作過well.I力量的誤解你的代碼。我更新了我的代碼,我想知道你能否告訴我你的想法?真的很感激。 –

+0

你遇到什麼錯誤? – danielM

+0

- [__ NSArrayM objectAtIndex:]:index 1 beyond bounds [0 .. 0]'我將值切換到numberoficons = 34和i = 34以確保對象不是零。我相信這是導致錯誤的那一行「cell.accessoryView = [[UIImageView alloc] initWithImage:[_ imagesArray objectAtIndex:indexPath.row]];」 –