gpt4 book ai didi

ios - TableViewController 不显示表格信息

转载 作者:行者123 更新时间:2023-11-28 20:53:44 24 4
gpt4 key购买 nike

嗨,当我执行 for 循环以从集合中获取所有文档时。我将汽车对象添加到 NSMutablearray,当我调试时,我可以看到对象正在添加到数组中,但是当我希望它显示在表中并获取数组的计数时,我调用 [self.mycars count]获取计数但计数为零,我无法从 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 是否已初始化。

关于ios - TableViewController 不显示表格信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com