【问题标题】:Adding UI for CarPlay audio app为 CarPlay 音频应用添加 UI
【发布时间】:2017-08-17 04:13:28
【问题描述】:

我正在我的 CarPlay 音频应用程序中添加一个列表(表格视图)。在 AppDelegate.m 中,我有

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ..........

    [MPPlayableContentManager sharedContentManager].dataSource = self;
    [MPPlayableContentManager sharedContentManager].delegate = self;

    return YES;
}

我还在 AppDelegate.m 中实现了 MPPlayableContentDataSource 方法:

- (NSInteger)numberOfChildItemsAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:
            return 3;

        case 1: 
            return 2;

        default:
            return 4;
    }
}

- (MPContentItem *)contentItemAtIndexPath:(NSIndexPath *)indexPath
{
    MPContentItem *contentItem = [[MPContentItem alloc] initWithIdentifier:@"container"];
    .................

    return contentItem;
}

但是,应用程序在切换 (indexPath.row) 时崩溃并显示“与 UITableView 一起使用的索引路径无效。传递给表视图的索引路径必须恰好包含两个指定节和行的索引。如果可能,请使用 UITableView.h 中 NSIndexPath 上的类别。我在这里做错了吗?提前致谢。

【问题讨论】:

  • 您是手动创建indexPath吗?
  • 没有。我没有手动创建 indexPath。我使用数据源方法中的 indexPath -numberOfChildItemsAtIndexPath:.
  • 你有一个 tableView 并且你正在使用这个 indexPath 吗?
  • 没有。我还没有列表(tableView)。现在我想我看到了问题:因为我的 CarPlay 应用程序中还没有列表(tableView),所以数据源方法中的 indexPath 无效(长度为零且路径为空)。那么我应该如何在 CarPlay 应用程序中创建列表(tableView)?非常感谢。

标签: ios user-interface audio carplay


【解决方案1】:

MPPlayableContentDataSource 使用 NSIndexPath 与 UITableView 不同。 要让indexPath.row 工作,indexPath 必须恰好包含 2 个元素,但 numberOfChildItemsAtIndexPath: 可以使用包含 0 到 5 个元素的 indexPath 调用 - 这就是您的代码崩溃的原因

numberOfChildItemsAtIndexPath:通常应该描述您的导航树 - 为导航中的特定节点提供 indexPath,它应该返回您可以从该节点导航到的节点数

修复代码的一些方法是:

- (NSInteger)numberOfChildItemsAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.length == 0) {
        // In a root of our navigation we have 3 elements
        return 3;
    }
    if (indexPath.length == 1) {
        switch ([indexPath indexAtPosition:0]) {
            case 0:
                // After tapping first item on our root list we see list with 3 elements
                return 3;
            case 1:
                // for second one we get list with 2 elements
                return 2;
            default:
                return 4;
        }
    }
    return 0;
}

我建议观看 WWDC 视频“为 CarPlay 启用您的应用”,尤其是来自 6:00 的视频,其中展示了很好的示例。

【讨论】:

  • 感谢您的 cmets,Secansoida。上个月我确实使用了和你一样的方法,而且效果很好。还是谢谢你。
  • 您是否碰巧看到“正在播放”选项卡?你是怎么做到的?
  • @Secansoida,我无法使用 contentItemAtIndexPath 正确列出项目。那么如何分配 MPContentItem 在数据源中的位置并委托返回 MPContentItem 呢?
  • @DineshKumar 我不确定我是否听懂了你的问题。我认为您需要做的是创建一些树状结构,该结构将从一个节点数组开始,这些节点对应于在 IndexPath 上显示的长度为 0 的项目。这些节点中的每一个都可以保留对其子节点的引用,而子节点又将保留对他们的孩子等等。因此,要为特定节点查找 MPContentItem,您只需遍历树即可。
  • @Secansoida 我解决了这个问题并使用 MPContentItemDataSource 列出了项目...
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多