我以编程方式创建了一个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时,我从未遇到过这个问题。任何建议表示赞赏。
预先感谢您。
我们是否应该假设单独的loadedObjects和loadedColors在这里是拼写错误? – 2009-06-01 17:28:09
除了那个错字,这似乎是很好的代码。我首先将cell.text赋值为static,或者像“Cell%d”之类的东西。这样你可以将UITableView部件与loadedObjects分离,并查看哪个是问题。 – 2009-06-01 17:35:22