Change line color programmatically in WatchKit

I would like to change the color of the row in the WKInterfaceTable if it was selected. I have taken these steps, but do not know how I can go further:

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

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

}

      

I hope some of you guys can help me do this.

+2


source to share


4 answers


You can change the color according to the row selection,

Create an IBOutlet WKInterfaceGroup in rowController and set it to Storyboard with DefaultGroup which is created when dragging table to storyboard (no need to add new WKInterfaceGroup).

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

      

Create a BOOL property to track the selection status in the rowController.



@property (nonatomic, readwrite) BOOL isSelected;

      

Add this to 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]];
    }
}

      

+3


source


You can do this by adding WKInterfaceGroup

rows to the controller. Make the width and height of the group "relative to the container" size 1. This will ensure that the group fills your row completely. Then you can set the background color with the setBackgroundColor: method to WKInterfaceGroup

. Add all other controls to this new group.



+1


source


I think you need to set the background colors in Storyboard.

Very theoretical, never did it myself:

If you have a more dynamic application, you can create two prototype strings and give them IDs (Normal, Highlighted). Then use insertRowsAtIndexes(_:)

and removeRowsAtIndexes(_:)

to "switch" the line against the selection ...

Not the best solution, but the only thing that comes to mind with WatchKit is still very limited.

0


source


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

      

}

0


source







All Articles