gpt4 book ai didi

swift - WatchOS2 - 如何在选择时使所有行组或每个行组具有不同的颜色?

转载 作者:行者123 更新时间:2023-11-30 13:46:46 25 4
gpt4 key购买 nike

我试图使其在单击时一行(一行中的一组)变为紫色,单击新行时它变为紫色,而前一行恢复为默认操作系统颜色。

当我选择一行时,它会正确变为紫色,但是当我点击另一行时,前一行不会恢复为正常的黑色行。

我还想更进一步,让 5 行每行变成不同的颜色。当选择新行时,每个行组都会恢复为默认的 watchOS 黑色。

这很有趣,因为默认颜色似乎不是深灰色或黑色。黑色本质上与透明相同,因为主视图背景是黑色的。深灰色比默认值更浅。

class TableInterfaceController: WKInterfaceController {

@IBOutlet var tableOutlet: WKInterfaceTable!

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)

let arrayOfObjects = ["ORANGE","PURPLE","BLUE","GRAY","YELLOW"]

tableOutlet.setNumberOfRows(arrayOfObjects.count, withRowType: "row")

for (var i = 0; i < arrayOfObjects.count; i++) {

let row = tableOutlet.rowControllerAtIndex(i) as? RowController
let labelValue = arrayOfObjects[i]

row?.labelOutlet.setText(labelValue)
}
}

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

let row = tableOutlet.rowControllerAtIndex(rowIndex) as? RowController

row?.groupOutlet.setBackgroundColor(UIColor.purpleColor())

if (row!.isSelected) {
row!.isSelected = false
row?.groupOutlet.setBackgroundColor(UIColor.blackColor())

}
else {
row?.isSelected = true
row?.groupOutlet.setBackgroundColor(UIColor.purpleColor())
}
}


class RowController: NSObject {
@IBOutlet var labelOutlet: WKInterfaceLabel!
@IBOutlet var groupOutlet: WKInterfaceGroup!
var isSelected : Bool = false

}

enter image description here

我想要这样

enter image description here

最佳答案

您需要实现以下更改。

let arrayOfObjects = [["text":"ORANGE", "color":UIColor.orangeColor(),"status":false] as NSMutableDictionary ,["text":"PURPLE", "color":UIColor.purpleColor(),"status":false] as NSMutableDictionary,["text":"BLUE", "color":UIColor.blueColor(),"status":false] as NSMutableDictionary,["text":"GRAY", "color":UIColor.grayColor(),"status":false] as NSMutableDictionary,["text":"YELLOW", "color":UIColor.yellowColor(),"status":false] as NSMutableDictionary];

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)

tableOutlet.setNumberOfRows(arrayOfObjects.count, withRowType: "row")

for (var i = 0; i < arrayOfObjects.count; i++) {

let row = tableOutlet.rowControllerAtIndex(i) as? RowController
row?.updateData(arrayOfObjects[i] as NSMutableDictionary);
}

}

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

let selectedRow = tableOutlet.rowControllerAtIndex(rowIndex) as? RowController

for (var i = 0; i < arrayOfObjects.count; i++) {

let row = tableOutlet.rowControllerAtIndex(i) as? RowController

//--
let data = arrayOfObjects[i] as NSMutableDictionary
data["status"] = (row == selectedRow);

row?.updateData(arrayOfObjects[i] as NSMutableDictionary);
}
}


class RowController: NSObject {

@IBOutlet var labelOutlet: WKInterfaceLabel!
@IBOutlet var groupOutlet: WKInterfaceGroup!

func updateData(data:NSMutableDictionary){

let labelValue = data["text"] as! String
let color = data["color"] as! UIColor
let status = data["status"] as! Bool

//--
self.labelOutlet.setText(labelValue)
self.groupOutlet.setBackgroundColor(status ? color : UIColor.blackColor());

}//F.E
}

关于swift - WatchOS2 - 如何在选择时使所有行组或每个行组具有不同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835366/

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