2011-10-27 23 views
1
  1. 我已經能夠使用TBXML解析器解析xml url。如何在UItableview中顯示解析的xml數據?

  2. 解析的數據將被放入名爲recordsNSMutableArray

  3. 下面是代碼....

    if ([[TBXML elementName:element] isEqualToString:@"order"]) { 
        NSLog(@"xml element checking"); 
        TBXMLElement *id = [TBXML childElementNamed:@"id" parentElement:element]; 
        TBXMLElement *status = [TBXML childElementNamed:@"status" parentElement:element]; 
        TBXMLElement *date = [TBXML childElementNamed:@"date" parentElement:element]; 
        TBXMLElement *by = [TBXML childElementNamed:@"by" parentElement:element]; 
        TBXMLElement *total = [TBXML childElementNamed:@"total" parentElement:element]; 
    
    
        [records addObject:[NSArray arrayWithObjects: 
             [TBXML textForElement:id], 
             [TBXML textForElement:status], 
             [TBXML textForElement:date], 
             [TBXML textForElement:by], 
             [TBXML textForElement:total],nil]]; 
    
  4. 現在這些records數據將在的tableview單元顯示,所以行數將是作爲records數組大小相同。

  5. 現在在我的tableview有五個UILabel。每個標籤都會顯示records數組值的屬性值。

    6.i可以第一次顯示屬性的值,直到滾動tableview,但我的問題是當我開始滾動我的UITableview然後值開始顯示在錯誤的單元格中。

  6. 這裏實現代碼如下代碼...

    - (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 redColor]; 
    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 greenColor]; 
    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 grayColor]; 
    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 yellowColor]; 
    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 cyanColor]; 
    totalLabel.layer.borderColor = [UIColor grayColor].CGColor; 
    totalLabel.tag = 4; 
    
    [cellView addSubview:idLabel]; 
    [cellView addSubview:statusLabel]; 
    [cellView addSubview:orderDate]; 
    [cellView addSubview:byLabel]; 
    [cellView addSubview:totalLabel]; 
    } 
        x = [records count]; 
        //NSLog(@" total number of row: %d",x); 
        if(y<x){ 
    if(y == indexPath.section){ 
        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]; 
    
        NSLog(@"the value of y: %d",y); 
        NSLog(@"the value of indexpath.section: %d",indexPath.section); 
        idLabel.text = [NSString stringWithFormat:@"Order Id: %@",[[records objectAtIndex:y] objectAtIndex:0]]; 
        statusLabel.text = [NSString stringWithFormat:@"Status: %@",[[records objectAtIndex:y] objectAtIndex:1]]; 
        orderDate.text = [NSString stringWithFormat:@"Date: %@",[[records objectAtIndex:y] objectAtIndex:2]]; 
        byLabel.text = [NSString stringWithFormat:@"By: %@",[[records objectAtIndex:y] objectAtIndex:3]]; 
        totalLabel.text = [NSString stringWithFormat:@"Total: %@",[[records objectAtIndex:y] objectAtIndex:4]]; 
        y++; 
    } 
    } 
    return cell; 
    } 
    

    那意思是,當表的負載則值顯示在右邊的單元格,但是當我開始滾動值不正確顯示電池第一次。

過去幾天我正在被淡化了這個問題,所以請任何人知道什麼是解決方案,然後儘快通知我的...

在此先感謝。

+0

有人幫我請 – Emon

+0

如何將解析的數據添加到MutableArray中? –

回答

1

我有我的回答是很無聊的問題......可能是它會有人樂於助人......

這裏是代碼...我只是改變的tableview委託..

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

    //here is my changed of code 
    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]];