ios - TableViewController 不显示表信息
问题描述
嗨,当我执行 for 循环以从集合中获取所有文档时。我将汽车对象添加到 NSMutablearray 中,当我调试时,我可以看到对象被添加到数组中,但是当我希望它显示在表中并获取数组的计数时,我调用 [self.mycars 计数]得到计数,但计数为零,我无法从 cell.textLabel.text = vehicle.name 获取信息;所以我做错了什么。
#import "TableViewController.h"
#import "Car.h"
#import "Vehicle.h"
@interface TableViewController ()
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.db = [FIRFirestore firestore];
self.myCars = [[NSMutableArray alloc] init];
[[[[self.db collectionWithPath:@"users"] documentWithPath:[FIRAuth auth].currentUser.uid]
collectionWithPath:@"cars"] getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) {
if (error != nil) {
NSLog(@"Error getting documents: %@", error);
} else {
for (FIRDocumentSnapshot *document in snapshot.documents) {
NSLog(@"%@ => %@", document.documentID, document.data);
Car *car = [[Car alloc] initWithCarName:[document valueForField:@"carname"] andCarStyle:[document valueForField:@"carstyle"]
andCarColour:[document valueForField:@"colour"] andCarYear:[document valueForField:@"caryear"]
andCarDoors:[document valueForField:@"doors"] andCarSeat:[document valueForField:@"seat"] andCarWheels:[document valueForField:@"wheels"]
andCarTankCapacity:[document valueForField:@"tankcapacity"] andCarHorsePower:[document valueForField:@"horsepower"] andCarModelName:[document valueForField:@"modelname"]];
NSLog(@"Car name:%@", car.name);
[self.myCars addObject:car];
}
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.myCars count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier" forIndexPath:indexPath];
// Configure the cell...'
Vehicle *vehicle = [self.myCars objectAtIndex:indexPath.row];
cell.textLabel.text = vehicle.name;
return cell;
}
@end
解决方案
您需要在更改/更新数据源(self.myCars
在这种情况下)后手动重新加载数据,这意味着您需要[self.tableView reloadData];
在 for 循环之后添加。如果它仍然无法正常工作,您可能需要检查是否self.myCars
已初始化。
推荐阅读
- spring - HttpServletRequest 参数映射间歇性为空
- url - 是否可以调用 URL 传递网站参数?
- git - 在分支上的一些工作和 git push origin(分支名称)之后,我做了'git checkout origin main',而不是
- python - 将python字典项写入单独的txt文件
- python - Django:存储 Q 查询对象以进行可重复搜索?
- java - 使用变量作为 SET 值时,“where 子句”中的未知列“”
- android - 构建颤振应用程序时发生错误
- spring - 运行配置问题 - 未看到导入的项目
- python-3.x - 将嵌套的 numpy 数组转换为二维数组
- android - 如何在 Android 11 中使用 ACTION_GET_CONTENT 获取文件对象?