Watchkit Table Row Background Color programatically
Instead of setting the background color of the row, you set the background color of the group.
WatchKit - How to programmatically change background image of UITableViewCell using RowControllerAtIndex
Yes.
First you set the number of rows based on your model, iterate through each record in the model, and determine what image to show.
You will need to create a custom object which will be the class for each of your table rows. Here in this custom NSObject, you can create the property fields to display in the table. Then as we iterate, we assign these properties based on the model
class myTableRow : NSObject{
@IBOutlet weak var myText: WKInterfaceLabel!
@IBOutlet weak var myImage: WKInterfaceImage!
}
and then somewhere in your controller you can do this.
func loadTable(){
myTable.setNumberOfRows(myModel.count, withRowType: "myTableRow")
for(index, content) in myModel.enumerate(){
let row = myTable.rowControllerAtIndex(index) as! myTableRow
row.myLabel.setText("\(content.property1)")
//do whatever logic you need to do to determine the correct image. You have access to your array here and its properties
//...then
row.myImage.setImage(content.imageToUse);
}
update
Since there are different templates of dynamic row, try setRowTypes
It takes an array of strings, each of which corresponds to the name of a row controller defined in your storyboard file. The total number of items in this array is the number of rows to be created in the table.
myTable.setRowTypes(["TimeTableRowController", "ActTableRowController", "ActTableRowController", "TimeTableRowController", "LocationTableRowController"])
If you call this method I believe you no longer have to call the setNumberOfRows
function.
How would I programmatically change the background color of a cell
A tableView
sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. for more info
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.redColor()
}
else {
cell.backgroundColor = UIColor.blueColor()
}
return cell
}
WatchKit setBackground Color Crash
The reason was in unused breakpoints.
Anyway:
Use awake(withContext:)
for changing UI:
When creating an interface controller, WatchKit instantiates the class
and calls its init() method followed shortly by its
awake(withContext:) method. Use those methods to initialize
variables, load data, and configure the items in your storyboard
scene. If WatchKit passes a valid object to the awake(withContext:)
method, use the information in that object to customize the
initialization process.
You can't use willActivate()
for changing background color etc.:
The willActivate() method lets you know when your interface is
active. Use the willActivate() method to perform any last minute
tasks, such as checking for updates to your content. (Do not use it
for your primarily initialization.)
Also always use weak
outlets:
@IBOutlet fileprivate weak var myButton : WKInterfaceButton!
And check that your outlet was connected.
Detect selected row with force touch watchkit
The only thing you can handle programmatically with the Force Touch is the Context Menu and this menu is associated with a specific controller, more information you will find following the link.
About the row highlighting: When you start tapping the system doesn't know what the real action is - the regular touch or the force one. So it is starting to highlight a specific area and only later find out what was the action.
How to create a Static Table in WatchKit
You can't, at present, since watchOS WKInterface
tables are dynamic: they require you to programmatically configure both the number of rows and the row controllers.
You could file a feature request with Apple. In the meantime, you could either
- configure your table programmatically (which would involve setting up a custom row for each different type of static row), or
- just drag groups and their controls to the storyboard, to represent each static row. Depending on the controls, you may have to tweak the group height and background color to make the groups consistently look like an
WKInterfaceTable
.
The first option would require more code and classes. The second, while much simpler, is more fragile, as it would rely on Apple not changing the appearance for their table row.
Alternatively, you could wait for the new watch OS to be presented at WWDC in a few weeks before making any decision.
Related Topics
Wkwebview Calayer to Image Exports Blank Image
Lazy Initialization and Deinit
Switch Statement Where Value Is Int But Case Can Contain Array
Uitableviewcell Subclass Wrong Image in Cell or Old Image Bug
Convert Input Data to Integer in Swift
Apple Mach-O Linker Error (Static, Not Ld)
Aurendercallbackstruct in Swift
Xcode 9.3 Watchkit Crash on Wkinterfacebutton Tap
Synchronous Request Using Alamofire
Realitykit - Add Force to Entity at Specific Point
Leaving Equal Width Gap Spacing Before and Between Menu Buttons
Linking Pages in Swift Playgrounds [Xcode]
How to Convert an Array to List in Realm
[Core Data]: Threw While Encoding a Value. with Userinfo of (Null)
Anchor Constraints Not Honored in Xcode 10/iOS 12
How to Find Multiple Nsrange for a String from Full String iOS Swift