2011-10-18 48 views
1
  1. 我有一個NSMutableArray命名爲recordsNSArray檢索XML數據時,uitableview的numberOfRowsInSection返回類型是什麼?

  2. 現在我檢索XML屬性及其值,使用TBXML parser並在NSArray中插入屬性值。

  3. 這個NSArrayrecords的一個對象。

當我想在tableview單元格中顯示XML數據的值時,numerberOfRowsInSection是什麼?

這是我的數據檢索程序....

[records addObject:[NSArray arrayWithObjects: 
    [TBXML textForElement:id], 
    [TBXML textForElement:productNumber], 
    [TBXML textForElement:name], 
    [TBXML textForElement:availableStock], 
    [TBXML textForElement:image], 
    [TBXML textForElement:quantityOrderMin], 
    [TBXML textForElement:dateCreated], 
    [TBXML textForElement:dateUpdated],nil]]; 

我的XML數據....

<ProductData HASH="21106941"> 
    <id>1</id> 
    <productNumber>a91cc0f4c7</productNumber> 
    <name>Product 1</name> 
    <seoTitle>product-1</seoTitle> 
    <viewCount>0</viewCount> 
    <availableStock>100.0</availableStock> 
    <lowStock>0.0</lowStock> 
    <image>5e928bbae358c93caedf6115fa7d178b.jpg</image> 
    <quantityOrderMax>20.0</quantityOrderMax> 
    <dateCreated>2011-10-06T16:08:45</dateCreated> 
</ProductData> 
<ProductData HASH="409555632"> 
    <id>2</id> 
    <productNumber>d8287e2e51</productNumber> 
    <name>Product 2</name> 
    <seoTitle>product-2</seoTitle> 
    <description> 
    <p>Lorem ipsum dolor sit amet, consectetue...</p> 
    </description> 
    <viewCount>0</viewCount> 
    <availableStock>100.0</availableStock> 
    <image>8bbd8dfff3cdd28285d07810a4fe7c32.jpg</image> 
    <quantityOrderMin>1.0</quantityOrderMin> 
    <dateCreated>2011-10-06T16:08:45</dateCreated> 
</ProductData> 

我能夠在UITableviewCell檢索數據,但它顯示前兩個檢索到的數據NSArray,因爲records數組中的行數是兩個。但我想設置numberOfRowsInsection將根據NSArray檢索的屬性值的總數。

我有問題,我知道問題出在這裏:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     NSLog(@"The number of row in the object array: %d ",[records count]); 
     return [records count]; 
    } 

我怎樣才能解決呢?

+0

請有人幫我儘快。 – Emon

+0

你的問題不是很清楚,Emon。 –

+0

@MichaelDautermann:我現在編輯了我的問題,請幫助我。 – Emon

回答

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

有兩種方法可以解決這個問題。

  1. 正確的方法是看看有關解析的this TBXML tutorial。這將告訴您如何解析每個ProductData元素條目中的每個屬性。

  2. 蠻力方法(這是不是一個,我會做)會通過你的「記錄」陣解析,它看起來像這樣:

    @"<id>1</id>",@"<productNumber>a91cc0f4c7</productNumber>",@"<name>Product 1</name>",@"<seoTitle>product-1</seoTitle>",@"<viewCount>0</viewCount>", 
    @"<availableStock>100.0</availableStock>",@"<lowStock>0.0</lowStock>",@"<image>5e928bbae358c93caedf6115fa7d178b.jpg</image>", 
    @"<basePrice>10.0</basePrice>",@"<costPrice>0.0</costPrice>",@"<height>1.0</height>",@"<width>1.0</width>",@"<depth>1.0</depth>",@"<weight>2.0</weight>" 
    

    等通過此快速列舉

    for(NSString * elementText in records) 
    { 
        NSLog(@"%@", elementText); 
        // and search for "<" and ">" and strip out the name and the value using 
        // substring 
    } 
    
+0

請我again.i希望編輯的問題,現在你會明白的問題。 – Emon

+0

你檢查了我的問題,如果你不清楚然後告訴我然後我會再次簡要。現在請幫我 – Emon

+0

我有我的解決方案。如果我使用此... \t'switch(indexPath.row){ \t \t case 0: \t \t \t \t cell.textLabel.text = [NSString stringWithFormat:@「%@」,[[record objectAtIndex:indexPath.row] objectAtIndex:0]]; \t \t \t break;' – Emon

相關問題