在Objective C中填充TableView

时间:2014-03-08 19:57:27

标签: ios objective-c uitableview

嘿伙计我在通过NSArray

传递JSON返回类型时遇到问题

.h档案:

@property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
@property (strong, nonatomic) NSArray *finishedGooglePlacesArray;

.m档案:

这是返回的部分:

NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:returnData //1

                          options:kNilOptions
                          error:&error];
self.googlePlacesArrayFromAFNetworking = [json objectForKey:@"original_request"];
[self.tableView reloadData];

现在从那里发送到这里:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

    cell.textLabel.text = [tempDictionary objectForKey:@"name"];

    if([tempDictionary objectForKey:@"location"] != NULL)
    {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Rating: %@ of 5",[tempDictionary   objectForKey:@"name"]];
    }
    else
    {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Not Rated"];
    }

    return cell;
}

但它打破了

NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

带有错误消息:

2014-03-08 13:53:26.772 Ripple[45593:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810
2014-03-08 13:53:26.776 Ripple[45593:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810'

我不确定发生了什么,建议或想法?

编辑:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.googlePlacesArrayFromAFNetworking count];
}

JSON

responseString: {"status":"ok","code":0,"message":"Testing this awesome application out!","from":"Admin Team","original_request":{"name":"david","location":"awesome","trait":"funny"}}

更新

错误的屏幕截图: error

2 个答案:

答案 0 :(得分:1)

根据JSON日志,[json objectForKey:@"original_request"] returns an NSDictionary`。所以首先你需要改变这一行:

@property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;

@property (strong, nonatomic) NSDictionary *googlePlacesArrayFromAFNetworking;

之后您收到错误是正常的,因为NSDictionary没有objectAtIndex选择器。您可以通过NSDictionary中的键和值进行枚举。

然后,将您的cellForRowAtIndexPath方法更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.googlePlacesArrayFromAFNetworking objectForKey:@"name"];

    if([self.googlePlacesArrayFromAFNetworking objectForKey:@"location"] != NULL)
    {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Rating: %@ of 5",[self.googlePlacesArrayFromAFNetworking objectForKey:@"name"]];
    }
    else
    {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Not Rated"];
    }

    return cell;
}

答案 1 :(得分:0)

看起来你不明白你从json那里得到了什么...

所以,试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //get the correct type
    NSDictionary *tempDictionary = (NSDictionary *)self.googlePlacesArrayFromAFNetworking;

    NSString *key = [[tempDictionary allKeys]objectAtIndex:indexPath.row];
    cell.textLabel.text = key;
    cell.detailTextLabel.text = [tempDictionary objectForKey:key];

    return cell;
}