0

我得到这个 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;

有什么方法可以检查是否premiumtrueJSON 中,而不是在 ? 中显示标题UITableView

编辑 1 我添加@property (strong, nonatomic) NSArray *premiumArray;了与 相关的正确数据premium,所以现在我只需要帮助查看该数组中的链接,TRUE这样我UITableView就不会显示任何溢价 = TRUE 的标题。

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

编辑 3

饲料.h

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

饲料.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];
4

2 回答 2

2

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

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

您只需要枚举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;
}
于 2013-05-09T21:40:20.873 回答
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;
}

你完成了。

于 2013-05-09T21:46:54.287 回答