0

I'm parsing an xml file from an url and it displays the content correctly.here is my xml file

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

now its displays each content in cell like the image below

image

Now i need to display 22kgold in a cell and SGD $69.50 in a cell.and also the next element same.And also to print these values in two labels.

here is my code in .m file

- (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];
        }      
    }

    label1.text=@"%@",botanicalName;
    label2.text=@"%@",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;
}

My label contents are not displayed and i need to separate string in each cell by '-'.Please guide me..

Thanks in advance

Martin Kenny
  • 2,468
  • 1
  • 19
  • 16
Vishnu
  • 2,243
  • 2
  • 21
  • 44

1 Answers1

1

You probably meant:

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

otherwise you were assigning the string "%@" into the labels, and ignoring botanicalName and elem_PLANT.

To separate the rows into two parts, you can use the following inside your tableView:cellForRowAtIndexPath: method:

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

You can then either:

  1. Use one of the UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, or UITableViewCellStyleValue1 cell styles, and assign the item and price variables to the cell.textLabel.text and cell.detailTextLabel.text properties, or

  2. you could add a custom cell view, and assign them to your own labels within that view.

Martin Kenny
  • 2,468
  • 1
  • 19
  • 16
  • I wasn't sure what the point of the two labels was, as it looks as though they're only going to display the last item in the list. Are they for debugging? You would probably need to use textForElement with elem_PLANT to get a text value out of it. – Martin Kenny Jul 18 '12 at 08:45
  • i tried by using this code elem_PLANT = [TBXML nextSiblingNamed:@"price" searchFromElement:elem_PLANT]; NSString *plant=[TBXML textForElement:elem_PLANT]; but still facing same issue.. – Vishnu Jul 18 '12 at 08:55
  • Actually, looking at it again, that would be expected behaviour. At that point (after the loop), elem_PLANT must, by definition, be nil, so calling textForElement: with it is likely to crash, or at least be blank. If this is just for debugging, why not just remove the code; if not, figure out what you're really trying to do, then go about that. Cheers, Marty. – Martin Kenny Jul 18 '12 at 09:10