Ios TableView中的多行类型-watchKit

Ios TableView中的多行类型-watchKit,ios,swift,row,tableview,watchkit,Ios,Swift,Row,Tableview,Watchkit,用一行类型创建一个简单的TableView相当容易。 你刚开始 tableView.setNumberOfRows(yourArray.count, withRowType: "yourowtype") 然后添加一个for循环,用数组中的数据填充uilabel或其他任何内容 当涉及到多行类型时,就不那么清楚了。我知道你必须把它设置好 tableView.setRowTypes(yourRowTypesArray) 但其余的我不明白 在iOS中,cellforrowatinedexpath中有

用一行类型创建一个简单的TableView相当容易。 你刚开始

tableView.setNumberOfRows(yourArray.count, withRowType: "yourowtype")
然后添加一个for循环,用数组中的数据填充
uilabel
或其他任何内容

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

tableView.setRowTypes(yourRowTypesArray)
但其余的我不明白

在iOS中,
cellforrowatinedexpath
中有一个非常清晰明了的
indexPath.row
解决方案,您可以说-好的,我希望这个数组填充那些indexPath,另一个数组应该用simple IF conditional填充那些e.t.c

然而,在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])
}
private func loadTable(){
tableView.setRowTypes(rowTypes)
对于0中的i..<问题列表[0]。计数{
让rowController=tableView.rowControllerAtIndex(i)作为!TableViewRowController
rowController.lblQuestion.setText(问题列表[0][i])
}
让rowController1=tableView.rowControllerAtIndex(answersList1[0]。计数)作为!AnswerRowController1
rowController1.按钮1.设置标题(应答列表1[0][0])
让rowController2=tableView.rowControllerAtIndex(answersList2[0]。计数+1)作为!AnswerRowController2
rowController2.按钮2.设置标题(应答列表2[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]
    }
}
let QuestionRowIdentifier=“QuestionRowIdentifier”
让AnswerRowIdentifier=“AnswerRowIdentifier”
让QuestionSeparatorRowIdentifier=“QuestionSeparatorIdentifier”
协议问题回答者行类型{
var titleLabel:WKInterfaceLabel!{get set}
}
类QuestionRowController:NSObject,QuestionAnswerRowTypes{
@IBOutlet var标题标签:WKInterfaceLabel!
}
类AnswerRowController:NSObject、QuestionAnswerRowTypes{
@IBOutlet var标题标签:WKInterfaceLabel!
}
结构问题{
标题:字符串
让我们看看附加信息:字符串?
让我们回答:[字符串]
}
让问题=[
问题(标题:“颜色是什么?”,附加信息:无,答案:[
“蓝色”,
“灰色”
]),
问题(标题:“哪个城市是英国的首都?”,附加信息:“附加信息”,回答:[
“伦敦”,
“利物浦”
]),
问题(标题:“一些问题?”),补充信息:无,答案:[
“一些答案1”,
“一些答案2”
])
]
类InterfaceController:WKInterfaceController{
@IBOutlet私有var表视图:WKInterfaceTable!
变量名称=[“亚历山大”、“费迪南德”、“杰克”、“塞缪尔”、“汤普森”、“托尼”]
重写func awakeWithContext(上下文:AnyObject?){
super.awakeWithContext(context)
让rowTypes=getRowTypes()
tableView.setRowTypes(rowTypes)
对于0中的i..[字符串]{
返回questions.flatMap{question in
返回[
[QuestionRowIdentifier],
数组(计数:question.answers.count,repeatedValue:AnswerRowIdentifier),
[问题分隔符RowIdentifier]
].flatMap{$0}
}
}
func textAtIndex(索引:Int)->字符串{
让titles=questions.flatMap{question in
返回
[
[可选。部分(问题。标题)],
问题。答案。地图(可选。部分),
[可选。无],
]
}.flatMap({$0})
返回标题[索引]
}
}
这就是最终结果


我已经设法让它工作了。但是在静态版本中。另一个问题是-我有三个数组作为三个独立的数据源,用于不同的行类型。这些数组会动态更新。不清楚如何在这些行类型之间进行区分,因为没有节或indexPath.rows…添加了代码。我的目标是能够1)在问答列表单元格和问答列表单元格之间进行切换,并加载具有标识符的适当类2)以类似于“问题单元格\问题单元格\回答1单元格\回答1单元格”的方式从数组中输入数据,然后再次输入“问题2单元格\问题2单元格\回答1单元格\回答1单元格”e.t.c.取决于设备的尺寸array@DavidRobertson,看看我的变化,这太神奇了。非常感谢你的努力。