2012-08-07 78 views
-1

我是新來的iphone開發人員,我使用NSXML解析並能夠在tableview中顯示數據,我還需要顯示圖像以及tableview中的文本。之前提交這個問題我已經搜索在互聯網上,沒有發現任何解決方案,如何在iPhone中使用xmlparsing在tableview中顯示圖像

以下是我使用的解析和對錶視圖顯示數據的代碼

NSMutableString *currentNodeContent; 
NSXMLParser *parser; 
ImageView *currentImage; 
bool isStatus; 

ViewController *xmlParser; 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 

-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    ImageView *currentImage = [[xmlParser templates]objectAtIndex:indexPath.row]; 

    UITableViewCell *cell = [UITableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

    CGRect nameLabelFrame = CGRectMake(55, 2, 250, 20); 
    UILabel *namelabelF = [[UILabel alloc] initWithFrame:nameLabelFrame]; 
    namelabelF.tag = 0011; 
    namelabelF.font = [UIFont boldSystemFontOfSize:12]; 
    namelabelF.text = @"Template Name :"; 
    namelabelF.textColor = [UIColor orangeColor]; 
    [cell.contentView addSubview:namelabelF]; 

    CGRect nameFrame = CGRectMake(165, 2, 250, 20); 
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:nameFrame]; 
    nameLabel.tag = 0012; 
    nameLabel.font = [UIFont boldSystemFontOfSize:12]; 
    [cell.contentView addSubview:nameLabel]; 

    CGRect imageFrame = CGRectMake(105, 20, 250, 13); 
    UILabel *imageLabel = [[UILabel alloc]initWithFrame:imageFrame]; 
    imageLabel.tag = 0014; 
    imageLabel.font = [UIFont systemFontOfSize:12]; 
    [cell.contentView addSubview:imageLabel]; 

    CGRect categoryFrameCreated = CGRectMake(45, 35, 250, 10); 
    UILabel *categoryFrameCreatedLabel = [[UILabel alloc] initWithFrame:categoryFrameCreated]; 
    categoryFrameCreatedLabel.tag = 0015; 
    categoryFrameCreatedLabel.font = [UIFont boldSystemFontOfSize:10]; 
    categoryFrameCreatedLabel.text = @"Category ;"; 
    categoryFrameCreatedLabel.textColor = [UIColor grayColor]; 
    [cell.contentView addSubview:categoryFrameCreatedLabel]; 

    CGRect categoryFrame = CGRectMake(105, 35, 250, 10); 
    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:categoryFrame]; 
    categoryLabel.tag = 0016; 
    categoryLabel.font = [UIFont systemFontOfSize:10]; 
    [cell.contentView addSubview:categoryLabel]; 

    } 

    UILabel *templateLabel = (UILabel *)[cell.contentView viewWithTag:0012]; 
    templateLabel.text = [currentImage templateName]; 

    UILabel *imgLabel = (UILabel *)[cell.contentView viewWithTag:0014]; 
    imgLabel.text = [currentImage imgPath]; 


    UILabel *categoryLabel = (UILabel *)[cell.contentView viewWithTag:0016]; 
    categoryLabel.text = [currentImage category]; 

    return cell; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 55; 
} 


#pragma mark - 
#pragma mark Table view delegate 


-(id) loadXMLByURL:(NSString *)urlString 
{ 
    templates  = [[NSMutableArray alloc] init]; 
    NSURL *url  = [NSURL URLWithString:urlString]; 
    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
    parser   = [[NSXMLParser alloc] initWithData:data]; 
    parser.delegate = self; 
    [parser parse]; 

    return self; 

} 

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    if ([elementname isEqualToString:@"template_maste"]) 
    { 
     currentImage = [ImageView alloc]; 
     isStatus = YES; 
    } 

    if ([elementname isEqualToString:@"Template_name"]) 
    { 
     currentImage = [ImageView alloc]; 
     isStatus = YES; 
    } 


} 

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 

    if ([elementname isEqualToString:@"Template_name"]) 
    { 
     currentImage.templateName = currentNodeContent; 
    } 

    if ([elementname isEqualToString:@"img_path"]) 
    { 
     currentImage.imgPath = currentNodeContent; 
    } 
    if ([elementname isEqualToString:@"Category"]) 
    { 
     currentImage.category = currentNodeContent; 
    } 
    if ([elementname isEqualToString:@"template_maste"]) 
    { 
     [self.templates addObject:currentImage]; 
     currentImage = nil; 
     currentNodeContent = nil; 
    } 

} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    xmlParser = [[ViewController alloc] loadXMLByURL:@"http://www.okok.net/services/contacts.aspx?type=gettemplates1&category="]; 
} 

* - xml文件 - * **

<template_maste> 
<img_id>249</img_id> 
<img_path>http://www.okok.net/Templatehtml/small/249.jpg</img_path> 
<html_path>/templatehtml/249.html</html_path> 
<Added_date>2011-12-28T00:00:00-08:00</Added_date> 
<Template_name>Happy New year 4</Template_name> 
<Category>Seasonal</Category> 
<Template_Status>1</Template_Status> 
</template_maste> 

我想要在tableview中

的任何解決方案,將不勝感激,顯示從img_path圖像..,

+0

你是否在任何地方下載圖像? – Eyal 2012-08-07 12:04:46

+0

@ Eyal-不,我沒有下載圖像,我試圖直接將圖像加載到tableview。 – kiran 2012-08-07 12:07:35

+1

http://stackoverflow.com/questions/11532820/xml-parsed-image-wont-work-in-uitableviewcell/11534509#11534509類似的問題 – Nina 2012-08-07 12:28:03

回答

0

我不知道你的努力出現在您的表視圖什麼cell,但是如果你想要顯示圖像,那麼你需要使用UIImageView(現在你只需使用一個標籤來顯示圖像的URL)
BTW ImageView是你的模型,所以它不是一個好主意,叫它「查看」我會將其更改爲imageData並添加一個UIImage屬性來容納實際的i法師。 所以你添加一個標籤,你的細胞以同樣的方式,添加圖像視圖:

UIImageView *imageView = nil; 
if(cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     imageView = [[UIImageView alloc] initWithFrame:someFrame]; 
     imageView.tag = IMAGE_VIEW_TAG; 
     [cell.contentView addSubview:imageView]; 

     // ... 
    } 

    // now you need to configure this cell 
    // first thing is to get the model for this cell 
    ImageData *imageData = [[xmlParser templates]objectAtIndex:indexPath.row]; 

    if (!imageData.image) { 
     // we don't have the image yet in our model, so we need to fetch it from the 
     // internet according to the image path. 
     // do it in a separate thread to not block the UI 
     dispatch_queue_t imgDownloaderQueue = dispatch_queue_create("imageDownloader", NULL); 
     dispatch_async(imgDownloaderQueue, ^{ 
      NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imageData.imgPath]]; 
      UIImage *image = [UIImage imageWithData:imageData]; 
      [imageData release]; 
      dispatch_queue_t main_queue = dispatch_get_main_queue(); 
      dispatch_sync(main_queue, ^{ 
      imageData.image = image; 

      // update UI 
      [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
              withRowAnimation:UITableViewRowAnimationNone]; 
      }); 
     }); 
     dispatch_release(imgDownloaderQueue); 
    } 
    else { 
     // we already fetched the image, so we just update the image view 

     imageView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_VIEW_TAG]; 
     imageView.image = imageData.image; 
    } 
} 
0

我會用​​

利用這一點,你可以設置一個佔位符圖像顯示,直到圖像已被下載,一旦圖像被下載,它將取代單元格的圖像。

相關問題