如果JSON响应为“true”,则不在“表视图”中显示结果

时间:2013-05-09 21:16:21

标签: ios json uitableview

我收到了这个JSON:

{
    "timestamp": "2013-05-03T22:03:45Z",
    "resultsOffset": 0,
    "status": "success",
    "resultsLimit": 10,
    "breakingNews": [],
    "resultsCount": 341,
    "feed": [{
        "headline": "This is the first headline",
        "lastModified": "2013-05-03T21:33:32Z",
        "premium": false,
        "links": {
            "api": {

并使用它在UITableView中加载它:

@property (strong, nonatomic) NSArray *headlinesArray;

- (void)viewDidLoad
{
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWithFormat:@"/now/?leafs=%@&teas=%@&apikey=xxxxx", leafAbbreviation, teaID] usingBlock:^(RKObjectLoader *loader) {
            loader.onDidLoadObjects = ^(NSArray *objects){

                premiumArray = objects;

                [_tableView reloadData];

            };
            [loader.mappingProvider setMapping:[Feed mapping] forKeyPath:@"feed"];
            loader.onDidLoadResponse = ^(RKResponse *response){
                //NSLog(@"BodyAsString: %@", [response bodyAsString]);
            };
        }];
     }

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

    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", feedLocal.headline];
    cell.textLabel.text = headlineText;

    return cell;
}

标题类模型:

@property (strong, nonatomic) NSString *headline;
@property (strong, nonatomic) Links *linksHeadline;

有没有办法检查JSON中的premium是否为true,是否在UITableView中没有显示标题?

编辑1 我添加了@property (strong, nonatomic) NSArray *premiumArray;,其中提取了与premium相关联的正确数据,所以现在我需要帮助查看该数组以查找TRUE所以我的UITableView赢了“的链接t显示premium = TRUE的任何标题。

编辑2 我在上面发布了viewDidLoad代码。

编辑3

Feed.h

@property (nonatomic, strong) NSString *headline;
@property (nonatomic, strong) NSString *premium;

Feed.m

+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"headline", @"headline",
         @"premium", @"premium",
         nil];
    }];
    return objectMapping;
}

修改

I added this per some answers, but still couldn't get it working, any thoughts?
@property (strong, nonatomic) NSArray *premiumArray;
@property (strong, nonatomic) NSMutableArray *myMutable;

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWithFormat:@"/now/?leagues=%@&teams=%@&apikey=5qqpgrsnfy65vjzswgjfkgwy", leagueAbbreviation, teamID] usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *objects){

            //sports = objects;
            premiumArray = objects;

            [_tableView reloadData];

        };
        [loader.mappingProvider setMapping:[Feed mapping] forKeyPath:@"feed"];
        loader.onDidLoadResponse = ^(RKResponse *response){
            //NSLog(@"BodyAsString: %@", [response bodyAsString]);
        };
    }];

self.myMutable = [[premiumArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"premium = YES"]] mutableCopy];

2 个答案:

答案 0 :(得分:2)

您需要创建某种UITableView数据源。使用UITableView处理这比简单地设置数据结构然后将数据传递到UITableView数据源要困难得多。

NSMutableArray可以很好地满足您的需求。无论您使用什么JSON解析器工具包,您都可能会得到一个阵列响应,它看起来像存储在headlinesArray中,每个都包含上面的示例代码。

你只需要通过headlinesArray枚举和IF [post objectForKey:@“premium”] == TRUE然后将它添加到NSMutableArray。

将所有这些放在viewDidLoad中,以便在构建UITableView之前处理它,然后在TableView中只需访问新构建的数组。

.h
@interface YourClass: YourClassSuperclass
{
   NSMutableArray *a;
}

.m

//In ViewDidLoad

a = [NSMutableArray array]; //Allocs and Inits a new array.

for (Feed *f in headlinesArray) //For all feeds in the headlines array. (f is the local var)
{
  //check if premium == TRUE. If yes, add to a.
}

//Then in your data source methods you just use the array named 'a'

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

    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Feed *feedLocal = [a objectAtIndex:indexPath.row]; //Replaced headlinesArray with a
    NSString *headlineText = [NSString stringWithFormat:@"%@", feedLocal.headline];
    cell.textLabel.text = headlineText;

    return cell;
}

答案 1 :(得分:1)

在表格视图数据源中,您需要NSMutableArray。获取数据后,请使用:

NSArray *someFeedArray = ...;

self.mutableArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in someFeedArray)
{
    BOOL isPremium = [[[(NSArray *)dict[@"feed"] objectAtIndex:0] objectForKey:"premium"] boolValue] isEqualToString:@"true"]; //Assuming stored as string

    if (!isPremium) [self.mutableArray addObject:dict];
}

numberOfRowsInSection方法中,您应该这样做:

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

你已经完成了。