繁体   English   中英

UITableView不显示来自responseObject的数据

[英]UITableView doesn't show data from responseObject

我正在尝试在TableView显示responseObject ,但是什么也没显示。 我可以打印出所有responseObject的值,但是运行该应用程序时, TableView什么也没有显示。 在.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);
}];

如果我取消注释[self.tableViewObject reloadData]; 它给了我这个错误:

libc ++ abi.dylib:以类型为NSException的未捕获异常终止

.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;
}

.h文件如下所示:

#import <UIKit/UIKit.h>

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

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



@end

我不知道我在做什么错。 我应该正确连接...

您尚未分配任何数据源和委托,并且现在正在重新加载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);
}];

希望对您有帮助。

首先,您需要按照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);
}];

由于您只想在单元格中显示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;
}

下一步是实现didSelectRowAtIndexPath委托。

当您点击单元格时,将使用此委托,因为您要打开新页面,因此新页面必须知道正在点击的单元格及其包含的信息。

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

如果你不知道如何视图之间传递变量, 可以帮助你。

暂无
暂无

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

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