繁体   English   中英

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

[英]If JSON response is “true”, don't show results in Table View

我得到这个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就不会显示任何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];

您将需要创建某种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;
}

在表视图数据源中,您将需要一个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;
}

这样就完成了。

暂无
暂无

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

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