简体   繁体   English

UITableView不显示来自responseObject的数据

[英]UITableView doesn't show data from responseObject

I'm trying to show a responseObject in a TableView , but nothing shows. 我正在尝试在TableView显示responseObject ,但是什么也没显示。 I can print out all the responseObject 's values, but nothing shows in the TableView when running the app. 我可以打印出所有responseObject的值,但是运行该应用程序时, TableView什么也没有显示。 In the .m file I have this in the ViewDidLoad: 在.m文件中,我在ViewDidLoad中具有以下内容:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"Info: %@", responseObject);
    dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    //[self.tableViewObject reloadData];
    self.tableViewObject.dataSource = self;
    self.tableViewObject.delegate = self;



    NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

If I uncomment [self.tableViewObject reloadData]; 如果我取消注释[self.tableViewObject reloadData]; it gives me this error: 它给了我这个错误:

libc++abi.dylib: terminating with uncaught exception of type NSException libc ++ abi.dylib:以类型为NSException的未捕获异常终止

And my tableView in the .m file looks like: .m文件中的tableView看起来像:

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}

The .h file looks like this: .h文件如下所示:

#import <UIKit/UIKit.h>

@interface MyCardsVC : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *dataArray;
}

@property (weak, nonatomic) IBOutlet UITableView *tableViewObject;



@end

I don't know what I'm doing wrong. 我不知道我在做什么错。 I should have connected it right... 我应该正确连接...

You have not assign any datasource and delegate and you are reloading tableView now You have to first assign dataSource and then reload TableView Like this 您尚未分配任何数据源和委托,并且现在正在重新加载tableView您必须首先分配dataSource然后再重新加载TableView。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(@"Info: %@", responseObject);
dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

self.tableViewObject.dataSource = self;
self.tableViewObject.delegate = self;

[self.tableViewObject reloadData]; 


NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

Hope it will help you. 希望对您有帮助。

First, you need to modify your request code as suggested by Muhammad Waqas Bhati . 首先,您需要按照Muhammad Waqas Bhati的建议修改请求代码。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"Info: %@", responseObject);
    dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    self.tableViewObject.dataSource = self;
    self.tableViewObject.delegate = self;

    [self.tableViewObject reloadData];

    NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Since you want to show only card1..cardn in your cell, you should modify your cellForRowAtIndexPath to be like this. 由于您只想在单元格中显示card1..cardn,因此应将cellForRowAtIndexPath修改为这样。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];

    /* if you want the label to follow card state, use the first cell.textLabel.text option
       if you prefer using table view row, use the second cell.textLabel.text
    */
    cell.textLabel.text = [NSString stringWithFormat:@"Card%d", [objectDict valueForKey:@"cardState"]];
    //cell.textLabel.text = [NSString stringWithFormat:@"Card%d", indexPath.row];
    return cell;
}

The next step is implementing didSelectRowAtIndexPath delegate. 下一步是实现didSelectRowAtIndexPath委托。

This delegate is used when you tap the cell, since you want to open new page, the new page must know which cell that is being tapped and the information it contains. 当您点击单元格时,将使用此委托,因为您要打开新页面,因此新页面必须知道正在点击的单元格及其包含的信息。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];
    NSLog(@"%@", objectDict);
    // Pass object to new page
}

If you aren't sure how to pass variable between view, this could help you. 如果你不知道如何视图之间传递变量, 可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM