【问题标题】:TBxml parsing with tableview along string seperator沿字符串分隔符使用 tableview 解析 xml
【发布时间】:2012-07-17 04:45:49
【问题描述】:

我正在从一个 url 解析一个 xml 文件,它正确显示了内容。这是我的 xml 文件

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

现在它在单元格中显示每个内容,如下图所示

现在我需要在一个单元格中显示 22kgold 和 SGD $69.50 在一个单元格中。并且下一个元素也是相同的。还要在两个标签中打印这些值。

这是我在 .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];
        }      
    }

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

我的标签内容不显示,我需要用'-'分隔每个单元格中的字符串。请指导我..

提前致谢

【问题讨论】:

    标签: iphone xcode uitableview uilabel tbxml


    【解决方案1】:

    你的意思可能是:

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

    否则,您会将字符串“%@”分配到标签中,而忽略 botanicalNameelem_PLANT

    要将行分成两部分,您可以在 tableView:cellForRowAtIndexPath: 方法中使用以下内容:

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

    然后您可以:

    1. 使用UITableViewCellStyleSubtitleUITableViewCellStyleValue1UITableViewCellStyleValue1 单元格样式之一,并将itemprice 变量分配给cell.textLabel.textcell.detailTextLabel.text 属性,或者

    2. 您可以添加自定义单元格视图,并将它们分配给该视图中您自己的标签。

    【讨论】:

    • 我不确定这两个标签的意义是什么,因为它们看起来好像只会显示列表中的最后一项。他们是为了调试吗?您可能需要将 textForElement 与 elem_PLANT 一起使用以从中获取文本值。
    • 我尝试使用此代码 elem_PLANT = [TBXML nextSiblingNamed:@"price" searchFromElement:elem_PLANT]; NSString *plant=[TBXML textForElement:elem_PLANT];但仍然面临同样的问题..
    • 实际上,再看一遍,这将是预期的行为。此时(在循环之后),根据定义,elem_PLANT 必须为 nil,因此调用 textForElement: 可能会崩溃,或者至少为空白。如果这只是为了调试,为什么不直接删除代码呢?如果没有,请弄清楚您真正想要做什么,然后去做。干杯,马蒂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多