WidgetKit:编辑小组件时如何在Intent处理中对数据分区?
在WidgetKit的Intent编辑界面实现选项分区的方法
要给WidgetKit的Intent编辑选项添加分区,你可以通过INObjectCollection搭配INSection来实现分组,具体操作步骤如下:
准备分组数据
先把你的任务按分类整理成对应数组,每个任务用INObject封装:let personalTasks = [ INObject(identifier: "visit-grandma", display: "Visit grandma"), INObject(identifier: "buy-milk", display: "Don't forget milk"), INObject(identifier: "buy-socks", display: "Buy socks"), INObject(identifier: "pick-laundry", display: "Pick up laundry") ] let workTasks = [ INObject(identifier: "reply-email", display: "Reply email"), INObject(identifier: "3pm-meeting", display: "Meeting at 3pm") ]创建分区并组装集合
用INSection为每个分类创建带标题的分区,再把分区整合进INObjectCollection:let personalSection = INSection(identifier: "personal", title: "Personal", items: personalTasks) let workSection = INSection(identifier: "work", title: "Work", items: workTasks) let groupedTasks = INObjectCollection(sections: [personalSection, workSection])在Intent处理器中返回分区集合
在你的Intent处理类(遵循INIntentHandlerProviding协议)里,重写对应参数的选项提供方法,返回刚才组装好的集合。比如你的Intent是TaskSelectionIntent,对应方法如下:func provideTaskOptionsCollection(for intent: TaskSelectionIntent, with completion: @escaping (INObjectCollection<NSString>?, Error?) -> Void) { // 把上面的分组代码放在此处 completion(groupedTasks, nil) }
最后要确保你的Intent定义文件中,对应参数的类型设置为Enum或Custom,并且勾选了Supports dynamic options,这样才能正常加载动态分组的选项列表。
内容的提问来源于stack exchange,提问作者Cheok Yan Cheng
相关产品推荐
相关产品推荐

