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
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
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:
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
you could add a custom cell view, and assign them to your own labels within that view.