gpt4 book ai didi

arrays - 从数组设置自定义单元格 uibutton 标题 - Swift

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

我正在使用 uitableview 自定义单元格。为了在其中输出信息,我添加了一个 UILabel 和两个 UIButtons。对于数据结构,我创建了一个自定义类

class Question {
var ask: String
var answers: [String]

var nextQuestions = [Question?]()

init(question: String, ans: [String]) {
self.ask = question
self.answers = ans
}

这是我用于添加数据的 ViewController 函数代码

func setupQuestion() {

let q1 = Question(question: "What is your favourite breakfast", ans: ["Pancakes", "Waffles"])
let q2 = Question(question: "What do you have for dinner", ans: ["Steak", "Spaghetti"])

nextQuestions.append(q1)
nextQuestions.append(q2)
}

这是我如何通过setCell函数输出数据

func setCell(Question: String, optionone: [String], optiontwo: [String])
{
self.mainText.text = Question
self.optionOne.setTitle(optionone, forState:UIControlState.Normal)
self.optionTwo.setTitle(optiontwo, forState:UIControlState.Normal)
}

这是 ViewControllersetCell 的实现(在 cellForRowAtIndexPath 中)

    let quest = nextQuestions[indexPath.row]
cell.setCell(quest.question, optionone: quest.ans, optiontwo: quest.ans)

我的问题是 - 因为我在数组中设置了 uibutton 标题 optionone: [String], optiontwo: [String] 我如何正确地将它们输出setCell 函数及其在 cellForRowAtIndexPath 中的实现。

非常感谢任何见解。

谢谢!

最佳答案

您有一个 Question 类型的对象数组和一个显示有关问题的信息的自定义单元格。在 cellForRowAtIndexPath 中,只需获取一个问题并将其作为自定义单元格 setCell 函数中的参数传递。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell

let question = nextQuestions[indexPath.row]
cell.setCell(question)
return cell
}

setCell 函数中用您的数据填充 UI

func setCell(question: Question)
{
self.mainText.text = question.ask
self.optionOne.setTitle(question.answers[0], forState:UIControlState.Normal)
self.optionTwo.setTitle(optiontwo.answers[1], forState:UIControlState.Normal)
}

关于arrays - 从数组设置自定义单元格 uibutton 标题 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32111956/

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