TableView 中的多个 RowType - watchKit

Multiple RowTypes in TableView - watchKit

创建具有一种行类型的简单 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"]]

loadTable 函数

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])
}

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

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

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 { [=10=] }
        }
    }

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

这是最终结果,