How to create sections in WKInterfaceTable

后端 未结 3 1121
野趣味
野趣味 2021-02-06 02:03

How can we create sections in table as there is no delegate for it. And is there any other way for creating sections or do we have to use two tables.

相关标签:
3条回答
  • 2021-02-06 02:48

    WatchKit tables do not have sections or headers, or footers, or editing, or searching, or data sources, or delegates.

    0 讨论(0)
  • 2021-02-06 02:51

    Table sections are not available from the WatchKit API. But section is just a group of cells with extra header/footer views, that can be simulated by using custom designed cells:

    WatchKit table with simulated sections

    I’ve created simple WKInterfaceTable extensions, that help to manage tables. Download Sample app.

    0 讨论(0)
  • 2021-02-06 02:59

    WKInterfaceTable is not so flexible like UITableView, but you can create rows manually, using different row types. And fill content for each cell according to its type.

    Take a look at the documentation:

    Apple Watch Programming Guide: Tables

    WatchKit Framework Reference: WKInterfaceTable

    For example, let's create a table having two row types:

    • headerRowType
    • detailRowType

      #define type1 @"HeaderRowType"
      #define type2 @"DetailRowType"
      
      // You also must create two classes: HeaderRowType and DetailRowType - controllers for these two types
      
      // preparing datasource: fill rowTypes and tableObjects
      NSArray* rowTypes = @[type1, type2, type2, type2, type1, type2, type2]; // types for table with 1 header cell and 3 detail cells in first "section", 1 header and 2 detail cells in second "section"
      
      // set types! self.someTable - WKInterfaceTable, associated with table in UI
      [self.someTable setRowTypes:rowTypes];
      
      for (NSInteger i = 0; i < rowTypes.count; i++)
      {
          NSString* rowType = self.rowTypes[i];
          if ([rowType isEqualToString:type1])
          {
              // create HeaderRowType object and fill its properties. There you also can parse any data from main iPhone app.
              HeaderRowType* type1Row = [self.someTable rowControllerAtIndex:i];
              // type1Row.property1 = ...;
      
          }
          else
          {
              DetailRowType* type2Row = [self.someTable rowControllerAtIndex:i];
              // type2Row.property1 = ...;
              // type2Row.property2 = ...;
          }
      }
      

    Done! Use your imagination and create more complex data structures.

    0 讨论(0)
提交回复
热议问题