《Apple Watch 开发》WKInterfaceTable 列表控件小结
2015-08-17 23:22
676 查看
目标结果截图:
目前 watch OS 2.0 开放的接口非常有限
WKinterfaceTable 类包含的接口:
1. 首先,在Interface.storyboard 搭建好列表要显示的视图
UI
结构
2. 新建一个类TableCell继承自NSObject,然后将刚刚storyboard里面的Cell的类修改为新建的类,作为自定义Cell用。(记得要#import[b]<WatchKit/WatchKit.h>[/b]
)
注意Cell的Identifier,它就像UITableViewCell里面的重用ID
3. 将自定义Cell里面的视图组件通过IBOutlet 关联到
TableCell 类。
4. 填充列表数据,在InterfaceController类里面的awakeWithContext: 方法进行
5. 滚动到目标行
6. 删除目标行
7. 插入目标行
8.列表选中事件处理,实现方法table:didSelectRowAtIndex:
9. 页面跳转
WKInterfaceController 页面的跳转,通过Interface.storyboard
里面的控制器对应的Identifier来跳转。
10. 页面间传值
页面(控制器)之间的传值则通过跳转时设置的
context 参数,
- (void)pushControllerWithName:(NSString*)name
context:(nullableid)context;
- (void)presentControllerWithName:(NSString*)name
context:(nullableid)context;
- (void)presentControllerWithNames:(NSArray<NSString*>
*)names contexts:(nullableNSArray
*)contexts;
我们从以上3个页面跳转方法里面看到,context 参数是个id类型,也就是说它可以指向任何一种object类型变量,所以它可以传递数据到下一个页面。
那我们取值也是通过这个context,这就是为什么方法awakeWithContext:会有个context参数,这个就是上一个页面传递过来的数据,如果没有传值,则为nil。有个这个context,我们就可以将它转为上一个页面传递过的数据。
Demo: https://github.com/AbooJan/WatchTableDemo.git
目前 watch OS 2.0 开放的接口非常有限
WKinterfaceTable 类包含的接口:
- (void)setRowTypes:(NSArray<NSString*> *)rowTypes;// row names. size of array is number of rows - (void)setNumberOfRows:(NSInteger)numberOfRows withRowType:(NSString *)rowType;// repeating row name @property(nonatomic,readonly) NSInteger numberOfRows; - (nullable id)rowControllerAtIndex:(NSInteger)index; - (void)insertRowsAtIndexes:(NSIndexSet *)rows withRowType:(NSString *)rowType; - (void)removeRowsAtIndexes:(NSIndexSet *)rows; - (void)scrollToRowAtIndex:(NSInteger)index;
1. 首先,在Interface.storyboard 搭建好列表要显示的视图
UI
结构
2. 新建一个类TableCell继承自NSObject,然后将刚刚storyboard里面的Cell的类修改为新建的类,作为自定义Cell用。(记得要#import[b]<WatchKit/WatchKit.h>[/b]
)
注意Cell的Identifier,它就像UITableViewCell里面的重用ID
3. 将自定义Cell里面的视图组件通过IBOutlet 关联到
TableCell 类。
@interface TableCell : NSObject @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceImage *icon; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *nameLabel; @end
4. 填充列表数据,在InterfaceController类里面的awakeWithContext: 方法进行
// 初始化数据源 _counties = [NSMutableArray arrayWithArray:@[@"Belgium", @"Brussels", @"USA",@"Washington DC", @"UK",@"London", @"India",@"New Delhi", @"China",@"Beijing", @"Australia",@"Canberra"]]; // 设置列表行数和列表Cell类型(rowType就是storyboard里面的Identifier) // 要显示不同种类的Cell,可以通过cell的Identifier来控制 [_table setNumberOfRows:_counties.count withRowType:kCellType]; // 填充数据到每一行 for (int i = 0; i < _counties.count; i++) { NSString *name = _counties[i]; // 取得索引对应的行,然后填充数据,其实这一步逻辑可以放到TableCell类里面处理 TableCell *row = [_table rowControllerAtIndex:i]; [row.nameLabel setText:name]; [row.icon setImageNamed:@"watch"]; }
5. 滚动到目标行
[_table scrollToRowAtIndex:_counties.count - 1];
6. 删除目标行
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:_counties.count - 1]; [_table removeRowsAtIndexes:indexSet];
7. 插入目标行
NSInteger index = _counties.count - 2; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index]; [_table insertRowsAtIndexes:indexSet withRowType:kCellType]; // 修改新插入行的数据 TableCell *row = [_table rowControllerAtIndex:index]; [row.nameLabel setText:@"创客-第三次工业革命"]; [row.icon setImageNamed:@"testPic"];
8.列表选中事件处理,实现方法table:didSelectRowAtIndex:
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex { NSLog(@"选中:%d", rowIndex); NSString *content = [NSString stringWithFormat:@"你选中的地方是:%@", _counties[rowIndex]]; [self pushControllerWithName:@"DetailInterfaceController" context:content]; }
9. 页面跳转
WKInterfaceController 页面的跳转,通过Interface.storyboard
里面的控制器对应的Identifier来跳转。
10. 页面间传值
页面(控制器)之间的传值则通过跳转时设置的
context 参数,
- (void)pushControllerWithName:(NSString*)name
context:(nullableid)context;
- (void)presentControllerWithName:(NSString*)name
context:(nullableid)context;
- (void)presentControllerWithNames:(NSArray<NSString*>
*)names contexts:(nullableNSArray
*)contexts;
我们从以上3个页面跳转方法里面看到,context 参数是个id类型,也就是说它可以指向任何一种object类型变量,所以它可以传递数据到下一个页面。
那我们取值也是通过这个context,这就是为什么方法awakeWithContext:会有个context参数,这个就是上一个页面传递过来的数据,如果没有传值,则为nil。有个这个context,我们就可以将它转为上一个页面传递过的数据。
Demo: https://github.com/AbooJan/WatchTableDemo.git
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- ios通过按钮点击异步加载图片
- 加载带有手势识别器的XIB文件需注意哪些问题
- 仅需几行代码实现方便易用的状态栏指示器
- ios中图像进行压缩方法汇总