3

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

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

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

}

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

4

4 回答 4

5

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

在 rowController 中创建 WKInterfaceGroup 的 IBOutlet 并将其设置为带有 DefaultGroup 的 Storyboard,这是在将 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]];
    }
}
于 2015-06-02T08:04:19.920 回答
1

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

于 2015-05-30T16:46:53.463 回答
1
@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]];
    }
}

}

于 2017-06-14T10:00:57.337 回答
0

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

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

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

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

于 2015-05-30T12:00:35.080 回答