《Apple Watch 开发》WKInterfaceTable 列表控件小结

本文总结了Apple Watch应用开发中WKInterfaceTable的使用,包括如何通过(void)pushControllerWithName:context:,(void)presentControllerWithName:context:以及(void)presentControllerWithNames:contexts:方法来展示列表内容。
摘要由CSDN通过智能技术生成
目标结果截图:


目前 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<WatchKit/WatchKit.h>


注意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,我们就可以将它转为上一个页面传递过的数据。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值