TableView中的多个RowType - watchKit

时间:2015-10-21 12:25:11

标签: ios swift row tableview watchkit

使用一种行类型创建简单的TableView相当容易。 你刚设置

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

然后添加for循环以填充uilabel或您对数组中的数据所拥有的任何内容。

对于多行类型,它不是那么清楚。我知道你必须设置

tableView.setRowTypes(yourRowTypesArray)

但我不明白其余的。

在iOS中,indexPath.row中有一个非常清晰明了的cellForRowAtIndexPath解决方案,你可以说 - 好的,我希望这个数组填充那些indexPaths,另一个数组应该填充那些等使用简单的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])
}

1 个答案:

答案 0 :(得分:2)

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

我的方法是重构一下并创建一些你想要的东西,

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