【问题标题】:Displaying Text Label from a combined Array (using APIs)显示组合数组中的文本标签(使用 API)
【发布时间】:2014-03-14 01:33:59
【问题描述】:

我有一个组合数组,就是组合API1和API2的新闻标题结果。如何从新组合数组中的每个对象中获取“标题文本”?

现在,当我一次只使用一个 API 时,我的应用程序运行良好。但是现在我将对象放在一个组合数组中,我无法弄清楚如何显示cell.headline.text,因为我从中提取的源不再来自同一个确切的 JSON(即可能是从API1 或可能从 API2 中提取)。

我正在使用 RestKit。

WebListViewController.m(仅使用 API1)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];

    F *fLocal = [hArray objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", fLocal.headline];
    cell.headlineLabel.text = headlineText;
}

WebListViewController.m(仅使用 API2)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];

    D *dLocal = [iArray objectAtIndex:indexPath.row];
    NSString *otherHeadlineText = 
    [NSString stringWithFormat:@"%@", dLocal.head.headline];
    cell.headlineLabel.text = otherHeadlineText;
}

WebListViewController.m(尝试结合 API1 + API2)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is where I need help.  Don't know if this line below even makes sense?
    WebListViewController *combinedArray = [array objectAtIndex:indexPath.row];
}

【问题讨论】:

    标签: ios arrays uitableview restkit


    【解决方案1】:

    您没有提及您的 RestKit 使用情况,使用核心数据可能对您来说是个好主意。您还应该考虑将所有对象映射到同一个目标对象/实体。

    无论如何,您可以合并您的数组以获得仅包含标题文本的列表:

    combinedArray = [hArray valueForKey:@"headline"];
    combinedArray = [combinedArray arrayByAddingObjectsFromArray:[iArray valueForKey:@"head.headline"]];
    

    (目前无法测试,第二行可能需要valueForKeyPath:

    【讨论】:

    • 关于我的 RestKit 使用情况,您希望我说明什么信息?
    • 首先,你可以使用Core Data吗?其次,您可以将一个实体用于两种“类型”的标题吗?需要先确定解决问题的适当方法...
    • 我不喜欢使用 Core Data,因为我不熟悉,除非它是唯一的解决方案。如果单个实体是可能的,您需要知道哪些信息?谢谢!
    • 你告诉我 ;-) 你的课程是什么,它们有何不同。我的回答是否为您提供了一系列字符串?你需要什么不同的?
    • 我想问题是如果我只得到一个标题数组,那么在包含标题的单元格中也会有与标题相关联的图像和日期等。您的建议是否会取消标题与日期/图像/等的关联?您认为最好的解决方案是什么?
    【解决方案2】:

    为什么要合并数组?

    F *fLocal = [hArray objectAtIndex:indexPath.row];
    D *dLocal = [iArray objectAtIndex:indexPath.row];
    
    NSString * combinedHeadlineText = [NSString stringWithFormat:@"%@%@", fLocal.headline, dLocal.head.headline];
    
    cell.headlineLabel.text = combinedHeadlineText;
    

    【讨论】:

    • 因为我不希望 Api1 标题和 Api2 标题合并。我要做的基本上是获取cnn头条和msn头条,然后将它们放在一个表格视图中。所以我不希望 ccn 标题和 msn 标题相互附加。有道理?谢谢!
    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多