简体   繁体   English

在WatchKit中以编程方式更改行的颜色

[英]Change color of row programmatically in WatchKit

I'd like to change the color of a row in a WKInterfaceTable if it has been selected. 如果已经选择了WKInterfaceTable,我想更改一行的颜色。 I've done these steps but don't know how I can go further: 我已经完成了这些步骤,但不知道如何进一步:

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

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

}

I hope someone of you guys can help me to do this. 我希望你们中的某些人可以帮助我做到这一点。

You can change colour as per row selection, 您可以根据行选择更改颜色,

Create IBOutlet of WKInterfaceGroup in rowController and set it to Storyboard with DefaultGroup which is created when you drag Table to storyboard (no need to add new WKInterfaceGroup). 在rowController中创建WKInterfaceGroup的IBOutlet,并使用默认组将其设置为Storyboard,默认组是将Table拖动到storyboard时创建的(无需添加新的WKInterfaceGroup)。

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

Create a BOOL property to track selection status in rowController. 创建BOOL属性以跟踪rowController中的选择状态。

@property (nonatomic, readwrite) BOOL isSelected;

Add this to didSelectRow, 将此添加到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]];
    }
}

You can do this by adding a WKInterfaceGroup to your row controller. 您可以通过向行控制器添加WKInterfaceGroup来完成此操作。 Make the group's width and height "relative to container" with a size of 1. This ensures that the group completely fills your row. 使组的宽度和高度“相对于容器”的大小为1.这可确保组完全填充您的行。 Then, you can set the background color using the setBackgroundColor: method on WKInterfaceGroup . 然后,您可以使用WKInterfaceGroup上的setBackgroundColor:方法设置背景颜色。 Add all of the other controls within this new group. 添加此新组中的所有其他控件。

@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]];
    }
}

} }

I think you have to set the background colors in Storyboard. 我认为你必须在Storyboard中设置背景颜色。

Highly theoretical, never done this myself: 高度理论化,从未自己做过:

If you have a more dynamic app, your could potentially create two prototype rows and give them ids (Normal, Highlighted). 如果你有一个更动态的应用程序,你可能会创建两个原型行并给它们ID(正常,突出显示)。 Then use insertRowsAtIndexes(_:) and removeRowsAtIndexes(_:) to "switch" the row against a highlighted one... 然后使用insertRowsAtIndexes(_:)removeRowsAtIndexes(_:)将行切换为突出显示的行...

Not the best solution, but the only one that comes to mind with WatchKit still being very limited. 不是最好的解决方案,但WatchKit中唯一想到的仍然非常有限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM