gpt4 book ai didi

ios - TableView 中的多个 RowType - watchKit

转载 作者:行者123 更新时间:2023-11-28 13:03:52 24 4
gpt4 key购买 nike

创建具有一种行类型的简单 TableView 相当容易。你只要设置

tableView.setNumberOfRows(yourArray.count, withRowType: "yourowtype")

然后添加一个 for 循环来填充您的 uilabel 或任何您拥有的数组数据。

当涉及到多行类型时,就不是那么清楚了。我知道你必须设置

tableView.setRowTypes(yourRowTypesArray)

但我不明白其余的。

在 iOS 中,您在 cellForRowAtIndexPath 中有一个非常清晰和直接的 indexPath.row 解决方案,您可以在其中说 - 好吧,我想要这个数组来填充那些 indexPaths,另一个数组应该填充那些 e.t.c.使用简单的 IF 条件

但是,在 WatchKit 中,没有 indexPath.row 这样的东西,我不清楚如何为特定数组分配特定行号?为什么要在多行类型解决方案中删除 setNumberOfRows(正如我在整个网络的示例中看到的那样)?

关于这个问题,我大量浏览了网络,但未能找到合适的可行解决方案。只是技巧和解决方法。

谢谢。

更新:添加代码

我的数组

var questionsList = [["[What is the color of?]"],["Which city is the capital of Great Britain", "additional info"],["Some question"]]
var answersList1 = [["Blue"],["London"],["some answer 1"]]
var answersList2 = [["Grey"],["Liverpool"],["some answer 2"]]

加载表函数

private func loadTable(){
tableView.setRowTypes(rowTypes)

for i in 0 ..< questionsList[0].count {
let rowController = tableView.rowControllerAtIndex(i) as! TableViewRowController
rowController.lblQuestion.setText(questionsList[0][i])
}

let rowController1 = tableView.rowControllerAtIndex(answersList1[0].count) as! AnswerRowController1
rowController1.button1.setTitle(answersList1[0][0])

let rowController2 = tableView.rowControllerAtIndex(answersList2[0].count+1) as! AnswerRowController2
rowController2.button2.setTitle(answersList2[0][0])
}

最佳答案

我宁愿建议你改进你的模型。看起来真的很难理解。将其重构为类或结构,使其易于理解。

这是我对它进行一些重构并创建您想要的东西的方法,

let QuestionRowIdentifier = "QuestionRowIdentifier"
let AnswerRowIdentifier = "AnswerRowIdentifier"
let QuestionSeparatorRowIdentifier = "QuestionSeparatorIdentifier"


protocol QuestionAnswerRowTypes {
var titleLabel: WKInterfaceLabel! { get set }
}

class QuestionRowController: NSObject, QuestionAnswerRowTypes {
@IBOutlet var titleLabel: WKInterfaceLabel!
}

class AnswerRowController: NSObject, QuestionAnswerRowTypes {
@IBOutlet var titleLabel: WKInterfaceLabel!
}

struct Question {
let title: String
let additionalInfo: String?
let answers: [String]
}

let questions = [
Question(title: "What is the color of?", additionalInfo: nil, answers: [
"Blue",
"Gray"
]),
Question(title: "Which city is the capital of Great Britain?", additionalInfo: "additional info", answers: [
"London",
"Liverpool"
]),
Question(title: "Some question?", additionalInfo: nil, answers: [
"some answer 1",
"some answer 2"
])
]

class InterfaceController: WKInterfaceController {

@IBOutlet private var tableView: WKInterfaceTable!

var names = ["Alexander", "Ferdinand", "Jack", "Samuel", "Thompson", "Tony"]

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

let rowTypes = getRowTypes()

tableView.setRowTypes(rowTypes)

for i in 0 ..< rowTypes.count {
if let rowController = tableView.rowControllerAtIndex(i) as? QuestionAnswerRowTypes {
rowController.titleLabel.setText(textAtIndex(i)!)
}
}
}

func getRowTypes() -> [String] {
return questions.flatMap { question in
return [
[QuestionRowIdentifier],
Array(count: question.answers.count, repeatedValue: AnswerRowIdentifier),
[QuestionSeparatorRowIdentifier]
].flatMap { $0 }
}
}

func textAtIndex(index: Int) -> String? {
let titles = questions.flatMap { question in
return
[
[Optional.Some(question.title)],
question.answers.map(Optional.Some),
[Optional.None],
]
}.flatMap( { $0 })
return titles[index]
}
}

这是最终结果,

enter image description here

关于ios - TableView 中的多个 RowType - watchKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259279/

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