【发布时间】:2022-01-13 06:27:50
【问题描述】:
您好,我正在尝试将数据从 TableViewController_My_boards 传递到 ViewController_Update_Boards,但我不知道该怎么做 所以首先我用来自 firestore 的数据创建了一个字典
- (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;
_db = [FIRFirestore firestore];
objNSDictinoaryListOfBoards =[[NSMutableDictionary alloc]initWithCapacity:5];
[[self.db collectionWithPath:@"boards"]
getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error getting documents: %@", error);
} else {
for (FIRDocumentSnapshot *document in snapshot.documents) {
NSString *key = document.documentID;
NSDictionary *boardDetails = document.data;
[self->objNSDictinoaryListOfBoards setObject:boardDetails forKey:key];
//NSLog(@"%@ => %@", document.documentID, document.data);
}
NSLog(@"self->objNSDictionaryListOfBoards= %@", self->objNSDictinoaryListOfBoards);
[self.tableView reloadData];
}
}];}
之后,我在表格视图单元格中显示每个项目的名称
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleIdentifier1" forIndexPath:indexPath];
// Configure the cell...
NSString *boardNo =[objNSDictinoaryListOfBoards allKeys][indexPath.row];
NSString *boardName =objNSDictinoaryListOfBoards [boardNo][@"name"];
cell.textLabel.text=boardName;
return cell;
}
然后,当我单击具有 didSelectRowIndexPath 的单元格时,我想将数据传递给下一个视图控制器 这是我所拥有的,但我从这里失去了。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];
vc.modalTransitionStyle= UIModalTransitionStyleFlipHorizontal;
//[self presentViewController:vc animated:YES completion:NULL];
[self.navigationController pushViewController:vc animated:TRUE];
}
提前致谢
【问题讨论】:
-
UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"];,这里是vc,你知道它的类吗?所以转换它:MyViewController *vc = (MyViewController *)[sb instatiante...];你在那个方法中有indexPath,你知道如何检索boardNo,然后是vc.boardNo = boardNo,或者你想要的任何其他属性。现在,[objNSDictinoaryListOfBoards allKeys],这里的订单不能保证。您应该为您的数据源使用 NSArray 而不是 NSDictionary。 -
感谢您这么快回答。我不明白我应该怎么做,我对目标 c 很陌生。我不知道如何找回 boardNo
-
@Juan : 和你在 cellForRow 中做的一样
-
@Ptit Xav 谢谢我正在尝试这样,但我在'UIViewController *'类型的对象上找不到属性'boardName'。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = (UIViewController *)[sb instantiateViewControllerWithIdentifier:@"ViewController_Update_Boards"]; NSString *boardNo =[objNSDictinoaryListOfBoards allKeys][indexPath.row]; NSString *boardName =objNSDictinoaryListOfBoards [boardNo][@"name"]; vc.boardName=boardName;
-
您需要将 vac 从情节提要中返回到正确的视图控制器类(您使用所需接口定义的那个)
标签: objective-c uitableview uiviewcontroller