从firebase数据库中检索数据并在目标c中的UITable中显示

时间:2017-01-26 07:52:30

标签: ios objective-c uitableview firebase firebase-realtime-database

我在Firebase中有以下数据库, enter image description here

我正在尝试检索数据并在UITable视图中显示它们。首先,我想从数据库中检索协议。目前只有一份协议记录,但将来会有很多记录。我尝试了下面的代码

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    //self.clearsSelectionOnViewWillAppear = NO;

    [[_firebaseDatabaseRef child:@"krib-60229"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *_Nonnull snapshot){
        self.allSnapshot = [NSMutableArray array];
        NSLog(@"%@",self.allSnapshot);
        NSLog(@"%s","TestTestTest");
        for(snapshot in snapshot.children){
            [self.allSnapshot addObject:snapshot];
        }
        [self.tableView reloadData];
    }];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.allSnapshot count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    FIRDataSnapshot *snapShot = [self.allSnapshot objectAtIndex:indexPath.row];

    NSString *testText = snapShot.value[@"landlordDisplayName"];


    cell.textLabel.text = testText;

    // Configure the cell...

    return cell;
}

我没有得到任何检索或任何错误。我尝试过安装NSLog,但它们也没有打印。我是目标C开发的IOS的新手。任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

请尝试以下行。

 FIRDatabaseReference *rootRef= [[FIRDatabase database] referenceFromURL:[NSString stringWithFormat:@"%@/agreements",<database-url>]];
    [rootRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot)
    {
        if (snapshot.exists)
        {
            NSLog(@“%@”,snapshot.value);            
        }
    }];

答案 1 :(得分:0)

@属性FIRDatabaseReference * ref;

“在上方声明此ref属性”

import React from 'react';
import { BrowserRouter as Router, Route , Switch} from 'react-router-dom';
import Home from '../home';
import NowPlaying from '../Movies/NowPlaying';
import TopRated from '../Movies/TopRated';
import Popular from '../Movies/popular';
import Search from '../Search/searchMovies';
import Movie from '../Movies/Movie';

const Routes = () => (
<Router>
<Switch>
    <Route exact path='/' component={Home} />
    <Route path="/now_playing" component={NowPlaying}/>
    <Route path="/top_rated" component={TopRated}/>
    <Route  path="/popular" component={Popular}/>
    <Route forceRefersh={true} path="/search" component={Search}/>
    <Route path="/movie/:id" component={Movie}/>
</Switch>
  </Router>

)
export default Routes;

这对我有用!