2009-06-01 64 views
0

我以编程方式创建了一个UITableView(不使用XIB)。它读取包含NSDictionary对象的NSMutableArray的* .plist文件。我遇到的问题是这样的:当我在UITableView中显示对象时,它从不显示位于零行的对象。我知道这是因为我得到以下的NSLog输出:总是在UITableView中缺少一行

2009-06-01 10:02:34.566 Color[784:20b] array count = 4 
2009-06-01 10:02:34.570 Color[784:20b] row = 3: Air 
2009-06-01 10:02:34.570 Color[784:20b] row = 2: Earth 
2009-06-01 10:02:34.571 Color[784:20b] row = 1: Water 
2009-06-01 10:02:34.571 Color[784:20b] row = 0: Fire 

我所看到的,在这种情况下,与水,土地和空气(但没有火)的表。如果我添加另一个对象,那么Fire将出现,但新对象不会。因此,总是对象在行0

下面是一些相关的代码:

@interface LoadViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> { 
    NSMutableArray *loadedObjects; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    self.loadedObjects = [self getCurrentData]; 
    NSLog(@"array count = %d", [self.loadedObjects count]); 
    [self.tableView reloadData]; 
    [super viewWillAppear:animated]; 
} 

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.loadedColors count]; 
} 

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    static NSString *Identifier = @"Identifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Identifier] autorelease]; 
     cell.showsReorderControl = YES; 
    } 
    NSUInteger row = [indexPath row]; 
    NSDictionary *obj = [self.loadedObjects objectAtIndex:row]; 
    cell.text = [obj objectForKey:@"Name"]; 
    NSLog(@"row = %d: %@", row, [obj objectForKey:@"Name"]); 
    return cell; 
} 

在代码片段还可以看到我的输出NSLog的。

当我从NIB填充UITableViews时,我从未遇到过这个问题。任何建议表示赞赏。

预先感谢您。

+0

我们是否应该假设单独的loadedObjects和loadedColors在这里是拼写错误? – 2009-06-01 17:28:09

+0

除了那个错字,这似乎是很好的代码。我首先将cell.text赋值为static,或者像“Cell%d”之类的东西。这样你可以将UITableView部件与loadedObjects分离,并查看哪个是问题。 – 2009-06-01 17:35:22

回答

1

我觉得有点哑,但也许这可能是别人有用......

我失败了,我描述说(因为我不认为这是相关的)是事实,我还实例化了一个UINavigationBar,在右上角有一个Edit按钮。简而言之,导航栏掩盖了表格中的第一条记录。

我之所以看不到,这是因为我这样做:

[self.tableView addSubview:navBar]; 

和什么做的是“粘”导航栏表,所以滚动不能透露缺少行。当我改变该行:

[self.appDelegate.window addSubview:navBar]; 

我可以从导航栏下拖出来的第一条记录,因为他们随即成为兄弟姐妹的意见。

对于错位问题,我表示歉意。