WatchKit - 如何使用 RowControllerAtIndex 以编程方式更改 UITableViewCell 的背景图像
WatchKit - How to programmatically change background image of UITableViewCell using RowControllerAtIndex
我一直在寻找如何根据传递给它的数组数据以编程方式更改 WatchKit TableViewCell 的背景图像。
我有两个基于 ["Time"、"Act"、"Location"] 传递数据的数组,我希望 Location 根据实际 Location 具有不同的背景图像...
这可能吗?我正在阅读有关代表的资料,但他们仍然让我有些困惑!
目前尝试次数:
了解如何使用 WKInterfaceGroup 执行此操作请参阅此代码以获得答案
主要CLASS
private func timeTable(typesA: [String]) {
var tempA: [String] = createTypesArray(typesA)
ByTimeTable.setRowTypes(tempA)
for var rowIndex = 0; rowIndex < typesA.count; rowIndex++ {
switch tempA[rowIndex] {
case "Time":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! TimeTableRowController
row.timeLabel.setText(typesA[rowIndex])
case "Act":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! ActTableRowController
row.actLabel.setText(typesA[rowIndex])
case "Location":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! LocationTableRowController
if typesA[rowIndex] == "Silent Disco" {
row.LocationGroup.setBackgroundImageNamed("disco")
}
else if typesA[rowIndex] == "Christmas Barn" {
row.LocationGroup.setBackgroundImageNamed("xmas")
}
else if typesA[rowIndex] == "The Other Tent" {
row.LocationGroup.setBackgroundImageNamed("other")
}
else if typesA[rowIndex] == "This Tent" {
row.LocationGroup.setBackgroundImageNamed("this")
}
else if typesA[rowIndex] == "That Tent" {
row.LocationGroup.setBackgroundImageNamed("that")
}
else if typesA[rowIndex] == "Which Stage" {
row.LocationGroup.setBackgroundImageNamed("which")
}
else if typesA[rowIndex] == "What Stage" {
row.LocationGroup.setBackgroundImageNamed("what")
}
row.locationLabel.setText(typesA[rowIndex])
default:
print("nope")
}
}
}
LocationTableRowController
import WatchKit
class LocationTableRowController: NSObject {
@IBOutlet var locationLabel: WKInterfaceLabel!
@IBOutlet weak var LocationGroup: WKInterfaceGroup!
}
是的。
首先根据模型设置行数,遍历模型中的每条记录,并确定要显示的图像。
您将需要创建一个自定义对象,它将成为每个 table 行的 class。在此自定义 NSObject 中,您可以创建 属性 字段以显示在 table 中。然后随着我们迭代,我们根据模型分配这些属性
class myTableRow : NSObject{
@IBOutlet weak var myText: WKInterfaceLabel!
@IBOutlet weak var myImage: WKInterfaceImage!
}
然后在你的控制器的某个地方你可以这样做。
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);
}
更新
由于有不同的动态行模板,尝试 setRowTypes
它接受一个字符串数组,每个字符串对应于故事板文件中定义的行控制器的名称。此数组中的项目总数是要在 table.
中创建的行数
myTable.setRowTypes(["TimeTableRowController", "ActTableRowController", "ActTableRowController", "TimeTableRowController", "LocationTableRowController"])
如果你调用这个方法我相信你就不用再调用setNumberOfRows
函数了。
我一直在寻找如何根据传递给它的数组数据以编程方式更改 WatchKit TableViewCell 的背景图像。
我有两个基于 ["Time"、"Act"、"Location"] 传递数据的数组,我希望 Location 根据实际 Location 具有不同的背景图像...
这可能吗?我正在阅读有关代表的资料,但他们仍然让我有些困惑!
目前尝试次数:
了解如何使用 WKInterfaceGroup 执行此操作请参阅此代码以获得答案
主要CLASS
private func timeTable(typesA: [String]) {
var tempA: [String] = createTypesArray(typesA)
ByTimeTable.setRowTypes(tempA)
for var rowIndex = 0; rowIndex < typesA.count; rowIndex++ {
switch tempA[rowIndex] {
case "Time":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! TimeTableRowController
row.timeLabel.setText(typesA[rowIndex])
case "Act":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! ActTableRowController
row.actLabel.setText(typesA[rowIndex])
case "Location":
let row = ByTimeTable.rowControllerAtIndex(rowIndex) as! LocationTableRowController
if typesA[rowIndex] == "Silent Disco" {
row.LocationGroup.setBackgroundImageNamed("disco")
}
else if typesA[rowIndex] == "Christmas Barn" {
row.LocationGroup.setBackgroundImageNamed("xmas")
}
else if typesA[rowIndex] == "The Other Tent" {
row.LocationGroup.setBackgroundImageNamed("other")
}
else if typesA[rowIndex] == "This Tent" {
row.LocationGroup.setBackgroundImageNamed("this")
}
else if typesA[rowIndex] == "That Tent" {
row.LocationGroup.setBackgroundImageNamed("that")
}
else if typesA[rowIndex] == "Which Stage" {
row.LocationGroup.setBackgroundImageNamed("which")
}
else if typesA[rowIndex] == "What Stage" {
row.LocationGroup.setBackgroundImageNamed("what")
}
row.locationLabel.setText(typesA[rowIndex])
default:
print("nope")
}
}
}
LocationTableRowController
import WatchKit
class LocationTableRowController: NSObject {
@IBOutlet var locationLabel: WKInterfaceLabel!
@IBOutlet weak var LocationGroup: WKInterfaceGroup!
}
是的。
首先根据模型设置行数,遍历模型中的每条记录,并确定要显示的图像。
您将需要创建一个自定义对象,它将成为每个 table 行的 class。在此自定义 NSObject 中,您可以创建 属性 字段以显示在 table 中。然后随着我们迭代,我们根据模型分配这些属性
class myTableRow : NSObject{
@IBOutlet weak var myText: WKInterfaceLabel!
@IBOutlet weak var myImage: WKInterfaceImage!
}
然后在你的控制器的某个地方你可以这样做。
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);
}
更新
由于有不同的动态行模板,尝试 setRowTypes
它接受一个字符串数组,每个字符串对应于故事板文件中定义的行控制器的名称。此数组中的项目总数是要在 table.
中创建的行数myTable.setRowTypes(["TimeTableRowController", "ActTableRowController", "ActTableRowController", "TimeTableRowController", "LocationTableRowController"])
如果你调用这个方法我相信你就不用再调用setNumberOfRows
函数了。