【问题标题】:Change color of row programmatically in WatchKit在 WatchKit 中以编程方式更改行的颜色
【发布时间】:2015-05-30 11:53:40
【问题描述】:

如果 WKInterfaceTable 已被选中,我想更改它的颜色。我已经完成了这些步骤,但不知道如何进一步:

override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {

    let row = tableView.rowControllerAtIndex(rowIndex) as! TableRowObject

}

我希望你们中的某个人可以帮助我做到这一点。

【问题讨论】:

    标签: swift xcode6 watchkit wkinterfacetable


    【解决方案1】:

    您可以根据行选择更改颜色,

    在 rowController 中创建 WKInterfaceGroup 的 IBOutlet 并将其设置为带有 DefaultGroup 的 Storyboard,该 DefaultGroup 是在您将 Table 拖到 Storyboard 时创建的(无需添加新的 WKInterfaceGroup)。

    @property (weak, nonatomic) IBOutlet WKInterfaceGroup *rowGroup;
    

    创建一个 BOOL 属性来跟踪 rowController 中的选择状态。

    @property (nonatomic, readwrite) BOOL isSelected;
    

    将此添加到 didSelectRow,

    - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
    
        TableRowObject *row = [self.interfaceTable rowControllerAtIndex:rowIndex];
    
        if (row.isSelected) {
            row.isSelected = NO;
            [row.rowGroup setBackgroundColor:[UIColor clearColor]];
        }
        else {
            row.isSelected = YES;
            [row.rowGroup setBackgroundColor:[UIColor blueColor]];
        }
    }
    

    【讨论】:

    • 非常感谢您的回答!我真的很想把它标记为正确,但是可以将它翻译成 swift 吗?
    • 这只是一个简单的 didSelectRowAtIndex 方法和 if-else 循环所以你可以将它们转换为 swift。
    • 这不再有效。 Xcode 会给你一个“非法配置 - 从 MultiPickerInterfaceController 到 WKInterfaceGroup 的 rowGroup 出口无效。出口无法连接到重复内容。”
    • @BlueskyMed 我有同样的错误。我删除了与 rowGroup 关联的网点并清理了我的项目。再次添加了插座,它起作用了。我相信触发因素是我将插座放置在多个地方,即使我移除了一个它仍然会抛出错误。
    【解决方案2】:

    您可以通过将WKInterfaceGroup 添加到行控制器来完成此操作。将组的宽度和高度设置为“相对于容器”,大小为 1。这样可以确保组完全填满您的行。然后,您可以在WKInterfaceGroup 上使用setBackgroundColor: 方法设置背景颜色。在这个新组中添加所有其他控件。

    【讨论】:

      【解决方案3】:
      @property (weak, nonatomic) IBOutlet WKInterfaceGroup *groupCellContainer;
      Create above property in your custom cell for the Group in your cell and connect it with watch interface storyboard.
      
      Add below code in your didSelectRowAtIndex method, this code will set Red background color to selected cell & set Gray color to other cells.
      - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
      for (int row=0; row < Array.count; row++) {
          CustomCell *customCell = [table rowControllerAtIndex:row];
          if (rowIndex == row) {
              [customCell.groupCellContainer setBackgroundColor:[UIColor redColor]];
          } else {
              [customCell.groupCellContainer setBackgroundColor:[UIColor grayColor]];
          }
      }
      

      }

      【讨论】:

        【解决方案4】:

        我认为您必须在 Storyboard 中设置背景颜色。

        高度理论化,我自己从未这样做过:

        如果您有一个更动态的应用程序,您可能会创建两个原型行并为它们指定 ID(正常、突出显示)。然后使用insertRowsAtIndexes(_:)removeRowsAtIndexes(_:) 将行“切换”到突出显示的行...

        不是最好的解决方案,但唯一想到的 WatchKit 仍然非常有限。

        【讨论】:

          猜你喜欢
          • 2011-02-04
          • 2015-12-06
          • 2021-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多