2012-09-30 40 views
0

我有一個tableview,我使用的是JSON,但它使我擁有的值翻倍。我只有2個對象,但它在tableview中放4個。有人可以告訴我我做錯了什麼,以及爲什麼當只有2個時列出4個對象?我檢查了計數和數據模型,它們都只顯示2個項目。TableView中的更多單元格

這裏是我的代碼:

// 
// RootBeerTableViewController.m 
// BaseApp 
// 
// Created by Blaine Anderson on 9/27/12. 
// Copyright (c) 2012 UIEvolution, Inc. All rights reserved. 
// 

#import "RootBeerTableViewController.h" 
#import "GlobalData.h" 

@interface RootBeerTableViewController() 



@end 

@implementation RootBeerTableViewController 

@synthesize rootList; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; 

    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 

    tableView.delegate = self; 

    tableView.dataSource = self; 

    [tableView reloadData]; 



    self.view = tableView; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    rootList =[[GlobalData sharedData].mRootBeers getRootBeers]; 
    NSLog(@"RootList: %@", rootList); 
    // Return the number of sections. 
    NSLog(@"RootList count: %i", rootList.count); 
    return rootList.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     // Return the number of sections. 
    NSLog(@"RootList row count: %i", rootList.count); 
    return rootList.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    rootList =[[GlobalData sharedData].mRootBeers rootBeerList]; 
    NSLog(@"RootList Cell: %@", rootList); 
    RootBeers* mRootBeer = [rootList objectAtIndex:indexPath.row]; 


    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    NSLog(@"Cell Name: %@", mRootBeer.name); 
     // Configure the cell... 
    cell.textLabel.text = mRootBeer.name; 
    cell.detailTextLabel.text = mRootBeer.brewer; 



    return cell; 
} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 

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

    [[GlobalData sharedData].mViewManager pushView:DETAILVIEW animated:YES]; 



} 

@end 

回答

1

我相信這是爲什麼...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
rootList =[[GlobalData sharedData].mRootBeers getRootBeers]; 
NSLog(@"RootList: %@", rootList); 
// Return the number of sections. 
NSLog(@"RootList count: %i", rootList.count); 
return rootList.count; 
} 

改變它這一點。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

要添加部分在rootList每個對象,然後將所有rootlist的每個部分。所以只返回一個部分是最簡單的。

+0

完美!謝謝!解決了問題! – BlackHatSamurai