2015-08-20 4 views
0

Я работаю с пользовательской ячейкой uitableview. Для вывода информации в нее я добавил один UILabel и два UIButtons. Для структуры данных я создал пользовательский классНастройка пользовательских заголовков uibutton для ячейки из массива - Swift

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

Вот реализация setCell в ViewControllercellForRowAtIndexPath)

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

Мой вопрос - потому что у меня есть uibutton названия установлены в массиве optionone: [String], optiontwo: [String] как я правильно выводить их в setCell функции и ее реализации в cellForRowAtIndexPath.

Любое понимание очень ценится.

Спасибо!

ответ

1

У вас есть массив объектов 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) 
} 
Смежные вопросы