在UITableView中总是缺少一行

时间:2009-06-01 17:25:42

标签: iphone uitableview sdk

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

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时,我从未遇到过这个问题。任何建议都表示赞赏。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我觉得有点愚蠢,但也许这对其他人有用......

我在描述中没有提到的(因为我认为它不相关)是我还实例化了一个UINavigationBar,在右上角有一个Edit按钮。简而言之,导航栏模糊了表格中的第一条记录。

我无法看到这个的原因是因为我这样做了:

[self.tableView addSubview:navBar];

这样做是为了将“导航棒”“粘贴”到桌面上,因此滚动无法显示丢失的行。当我将该行更改为:

[self.appDelegate.window addSubview:navBar];

我可以将第一条记录从navBar下拖出来,因为它们会成为兄弟视图。

我为错误的问题道歉。