1
我從服務器加載XML數據(包括圖像,文本等),並通過tableview顯示該數據。如何在ios中預加載圖像
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"user_id"])
{
Myxml.str_user_id = currentNodeContent;
}
else if([elementName isEqualToString:@"username"])
{
Myxml.str_username = currentNodeContent;
}
else if([elementName isEqualToString:@"avatar"])
{
Myxml.str_avatar=currentNodeContent;
Myxml.imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentNodeContent]];
}
else if([elementName isEqualToString:@"full_name"])
{
Myxml.str_full_name=currentNodeContent;
}
else if([elementName isEqualToString:@"cups"])
{
Myxml.str_cups=currentNodeContent;
}
//--------Record End-------
else if([elementName isEqualToString:@"record"])
{
[RecordArray addObject:Myxml];
[Myxml release];
Myxml = nil;
}
[currentNodeContent release];
currentNodeContent = nil;
currentNodeContent = [[NSMutableString alloc]init];
}
現在,在主視圖控制器我收集在名爲 「LabelArray」 的NSMutableArray所有數據在init方法
Connection=[[NetworkConnection alloc]initConnection];
if (Connection.isReachable)
{
XmlDetail=[[TopPickerXML alloc]init];
LabelArray=[[NSMutableArray alloc]initWithArray:XmlDetail.RecordArray];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Network connection unavailable..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
並且在的cellForRowAtIndexPath表視圖,我顯示這樣的圖像
img_BuddyAvtar=[[UIImageView alloc]init];
img_BuddyAvtar.frame=CGRectMake(10, 5, 120, 105);
img_BuddyAvtar.image=[UIImage imageWithData:TopPicker_record.imageData];
img_BuddyAvtar.backgroundColor=[UIColor clearColor];
[View_TableCell addSubview:img_BuddyAvtar];
現在,我的問題是當我加載此視圖需要時間加載,然後顯示包括我的文本和圖像的所有數據。如何預加載圖像,以便加載我的表視圖需要更少的時間?
添加以下代碼將用於圖像的循環此代碼的工作? –
是的,它會。你只需要傳遞URL和佔位符圖像。而已 :) – ivarun