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>
現在
其顯示在每個單元格的內容如下圖
現在我需要在細胞和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;
}
我的標籤內容不顯示,我需要在每個單元的分隔字符串「 - 」。在請指導我..
謝謝推進
謝謝您reply..it作品,但對於LABEL2它得到BAD_ACCESS線程.. – Dany 2012-07-18 08:41:30
我不知道這兩個標籤的一點是,因爲它看起來好像他們只是要顯示列表中的最後一項。它們是否用於調試?您可能需要使用帶有elem_PLANT的textForElement來獲取文本值。 – 2012-07-18 08:45:38
我試圖通過使用此代碼elem_PLANT = [TBXML nextSiblingNamed:@ 「價格」 searchFromElement:elem_PLANT]; 的NSString *植物= [TBXML textForElement:elem_PLANT]; 但仍面臨着同樣的問題.. – Dany 2012-07-18 08:55:15