2012-07-18 45 views
0

我從URL解析XML文件,並將其顯示內容correctly.here是我的xml文件TBXML與實現代碼如下分析沿字符串分隔符

<gold> 
<price> 
<title>22 K Gold - SGD $69.50</title> 
</price> 
<price> 
<title>24 K Gold - SGD $62.50</title> 
</price> 
</gold> 
現在

其顯示在每個單元格的內容如下圖

image

現在我需要在細胞和69.50新元$在cell.and顯示22kgold也是下一個元素same.And也以兩個標籤打印這些值。

這裏是我在.m文件代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSMutableArray *tableArray = [[NSMutableArray alloc]init]; 

    NSData *xmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.p41tech.com/img/application/android/server/kamala/goldprice.xml"]]; 
    tbxml = [[TBXML alloc]initWithXMLData:xmlData]; 


    TBXMLElement * root = tbxml.rootXMLElement; 

    if (root) 
    { 
     TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"price" parentElement:root]; 

     while (elem_PLANT !=nil) 
     { 
      TBXMLElement * elem_BOTANICAL = [TBXML childElementNamed:@"title" parentElement:elem_PLANT]; 
      NSString *botanicalName = [TBXML textForElement:elem_BOTANICAL]; 
      [tableArray addObject:botanicalName]; 
      elem_PLANT = [TBXML nextSiblingNamed:@"price" searchFromElement:elem_PLANT]; 
     }  
    } 

    [email protected]"%@",botanicalName; 
    [email protected]"%@",elem_PLANT; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [tableArray count]; 
} 

- (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.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"download.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 
    cell.textLabel.backgroundColor=[UIColor clearColor]; 
    cell.textLabel.text = [tableArray objectAtIndex:indexPath.row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    tableView.backgroundColor=[UIColor clearColor]; 
    return cell; 
} 

我的標籤內容不顯示,我需要在每個單元的分隔字符串「 - 」。在請指導我..

謝謝推進

回答

1

你大概的意思是:

label1.text = [NSString stringWithFormat:@"%@", botanicalName]; 
label2.text = [NSString stringWithFormat:@"%@", elem_PLANT]; 

否則你分配ST響鈴「%@」放入標籤中,並忽略botanicalNameelem_PLANT

對行分成兩個部分,你可以用你的tableView:cellForRowAtIndexPath:方法裏面如下:

NSArray *parts = [[tableArray objectAtIndex:indexPath.row] componentsSeparatedByString:@" - "]; 
NSString *item = [parts objectAtIndex:0]; 
NSString *price = [parts objectAtIndex:1]; 

然後,您可以:

  1. 用途之一UITableViewCellStyleSubtitleUITableViewCellStyleValue1,或UITableViewCellStyleValue1細胞樣式,並分配itemprice變量到cell.textLabel.textcell.detailTextLabel.text屬性或

  2. 你可以添加自定義單元格圖,並將它們分配到該視圖中自己的標籤。

+0

謝謝您reply..it作品,但對於LABEL2它得到BAD_ACCESS線程.. – Dany 2012-07-18 08:41:30

+0

我不知道這兩個標籤的一點是,因爲它看起來好像他們只是要顯示列表中的最後一項。它們是否用於調試?您可能需要使用帶有elem_PLANT的textForElement來獲取文本值。 – 2012-07-18 08:45:38

+0

我試圖通過使用此代碼elem_PLANT = [TBXML nextSiblingNamed:@ 「價格」 searchFromElement:elem_PLANT]; 的NSString *植物= [TBXML textForElement:elem_PLANT]; 但仍面臨着同樣的問題.. – Dany 2012-07-18 08:55:15