gpt4 book ai didi

ios - WatchKit - 如何使用 RowControllerAtIndex 以编程方式更改 UITableViewCell 的背景图像

转载 作者:行者123 更新时间:2023-11-29 01:08:41 24 4
gpt4 key购买 nike

我一直在寻找如何根据传递给 WatchKit TableViewCell 的数组数据以编程方式更改其背景图像。

我有两个数组根据[“时间”,“行为”,“位置”]传递数据,并且我希望位置具有基于实际位置的不同背景图像...

这可能吗?我正在阅读有关代表的资料,但他们仍然让我有些困惑!

到目前为止的尝试:

了解如何使用 WKInterfaceGroup 执行此操作查看此代码以获取答案

主类

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!

}

最佳答案

是的。

首先,您根据模型设置行数,遍历模型中的每条记录,并确定要显示的图像。

您需要创建一个自定义对象,该对象将成为每个表格行的类。在此自定义 NSObject 中,您可以创建要在表中显示的属性字段。然后随着我们的迭代,我们根据模型分配这些属性

class myTableRow : NSObject{
@IBOutlet weak var myText: WKInterfaceLabel!
@IBOutlet weak var myImage: WKInterfaceImage!
}

然后在您的 Controller 中的某处您可以执行此操作。

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

它接受一个字符串数组,每个字符串对应于 Storyboard文件中定义的行 Controller 的名称。此数组中的项目总数是要在表中创建的行数。

myTable.setRowTypes(["TimeTableRowController", "ActTableRowController", "ActTableRowController", "TimeTableRowController", "LocationTableRowController"])

如果您调用此方法,我相信您不再需要调用 setNumberOfRows 函数。

关于ios - WatchKit - 如何使用 RowControllerAtIndex 以编程方式更改 UITableViewCell 的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com