簡體   English   中英

iPhone應用程序中UITableview中的xml數據

[英]xml data in UITableview in iPhone app

如何根據xml數據的屬性字段在UITableView中顯示xml數據?

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
[currentElementValue appendString:string];

}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(@"elementName %@ %@", elementName, currentElementValue);
currentElementValue=nil;
[currentElementValue release];
}

這是一些代碼,我在xml文件中獲取元素名稱。現在我想在UITableView單元上顯示這些數據。

如果我無法表達問題,請敲...。

注意:如果有人給我源代碼,那對我會更好,因為它對我來說非常緊急。

提前致謝。

每次您解析xml數據時,都將該值放入一個數組中,然后將其顯示在tableview中。

#pragma mark tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

int sectionCount = [records count];
NSLog(@"section cout: %d",sectionCount);
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
    cell.accessoryView = imageView;
    cell.accessoryType = UITableViewCellSelectionStyleNone;
    tableView.separatorColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
    cellView.backgroundColor = [UIColor clearColor];
    cellView.tag =10;
    [cell.contentView addSubview:cellView];

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
    imgView.image = [UIImage imageNamed:@"productbox.png"];
    imgView.layer.borderColor = [UIColor blackColor].CGColor;
    imgView.layer.borderWidth = 2.0;
    imgView.tag = 5;
    [cellView addSubview:imgView];

    CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
    idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
    idLabel.textAlignment = UITextAlignmentLeft;
    idLabel.textColor = [UIColor blackColor];
    idLabel.font = [UIFont systemFontOfSize:12];
    idLabel.backgroundColor = [UIColor clearColor];
    idLabel.layer.borderColor = [UIColor grayColor].CGColor;
    idLabel.tag = 0;

    CGRect statusRect = CGRectMake(65, 22, 190, 22);
    statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
    statusLabel.textAlignment = UITextAlignmentLeft;
    statusLabel.textColor = [UIColor blackColor];
    statusLabel.font = [UIFont systemFontOfSize:12];
    statusLabel.backgroundColor = [UIColor clearColor];
    statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
    statusLabel.tag = 1;

    CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
    orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
    orderDate.textAlignment = UITextAlignmentLeft;
    orderDate.textColor = [UIColor blackColor];
    orderDate.font = [UIFont systemFontOfSize:12];
    orderDate.backgroundColor = [UIColor clearColor];
    orderDate.layer.borderColor = [UIColor grayColor].CGColor;
    orderDate.tag = 2;

    CGRect byRect = CGRectMake(65, 75, 190, 22);
    byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
    byLabel.textAlignment = UITextAlignmentLeft;
    byLabel.textColor = [UIColor blackColor];
    byLabel.font = [UIFont systemFontOfSize:12];
    byLabel.backgroundColor = [UIColor clearColor];
    byLabel.layer.borderColor = [UIColor grayColor].CGColor;
    byLabel.tag = 3;

    CGRect totalRect = CGRectMake(65, 98, 190, 22);
    totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
    totalLabel.textAlignment = UITextAlignmentLeft;
    totalLabel.textColor = [UIColor blackColor];
    totalLabel.font = [UIFont systemFontOfSize:12];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
    totalLabel.tag = 4;

    [cellView addSubview:idLabel];
    [cellView addSubview:statusLabel];
    [cellView addSubview:orderDate];
    [cellView addSubview:byLabel];
    [cellView addSubview:totalLabel];
}


cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
idLabel.text = [NSString stringWithFormat:@"Order Id: %@",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:@"Status: %@",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:@"Date: %@",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:@"By: %@",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:@"Total: %@",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//Get the selected country
NSString *selectedSection = [ NSString stringWithFormat:@"%@",[[records objectAtIndex:indexPath.section] objectAtIndex:0] ];
dvController = [[OrderDetailsViewController alloc] initWithNibNameAndCurrentTweetUser:@"OrderDetailsViewController" bundle:nil:selectedSection];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
 }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 130;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
[v setBackgroundColor:[UIColor blackColor]];

UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(10,0, tableView.bounds.size.width,20)] autorelease];
hdr.textAlignment = UITextAlignmentLeft;
hdr.font = [UIFont fontWithName:@"Arial-BoldMT" size:12];
hdr.textColor = [UIColor whiteColor];
hdr.backgroundColor = [UIColor blackColor]; 
[v addSubview:hdr];

        hdr.text = [NSString stringWithFormat:@"Order #%@",[[records objectAtIndex:section] objectAtIndex:0]];
        return v ;
}





- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 20;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM