从所有 TableViewCell 获取数据 [英] Getting Data From all TableViewCells

查看:19
本文介绍了从所有 TableViewCell 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个抽认卡应用程序,现在我正在创建用户创建抽认卡的界面.我最初将它设置为用户无法添加另一张卡片(添加另一个 UITableViewCell),直到当前卡片具有术语和定义.如果卡片已填写,我会将术语和定义保存到字典中.我在部分过程中意识到,用户可以返回上一个单元格并更改单元格中的数据,并且永远不会保存.我的最新想法是等到用户完成所有单元格的填充,然后保存数据.我似乎无法找到获取所有单元格数据的方法.

I am creating a flashcards app and right now I am creating the interface where the user creates the flashcards. I originally had it set up where the user could not add another card (add another UITableViewCell) until the current one had a term and definition. If the card was filled in, I would save the term and definition to a dictionary. What I realized part of the way through is that the user can go back to a previous cell and change the data in the cell and it would never be saved. My latest idea is to wait until the user finishes filling in all of the cells and then save the data. I can't seem to find a way to get the data of all of the cells.

我的 tableViewController 现在有什么:

// MARK: Variables

var cards = [Int]()

var cardData = [String : String]()



// MARK: Actions

@IBAction func plusBarButtonItemPressed(sender: AnyObject) {

    self.cards.append(self.cards.count + 1)

    let insertionIndexPath = NSIndexPath(forRow: self.cards.count - 1, inSection: 0)

    tableView.insertRowsAtIndexPaths([insertionIndexPath], withRowAnimation: .Automatic)

}


@IBAction func doneBarButtonItemPressed(sender: AnyObject) {

    // Save each cell into cardData [termTextView.text : definitionTextView.text]

}



// MARK: Functions

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.cards.count
}

这是我现在在我的自定义 tableViewCell

// MARK: Outlets

@IBOutlet var termTextView: UITextView!

@IBOutlet var definitionTextView: UITextView!

再次,我的问题:当按下按钮(保存)时,如何将所有 tableViewCells 内的 textViews 内的文本放入字典中?

感谢您的帮助!

推荐答案

tableView 不应存储数据.数据应该保存在你的模型中(一个或多个数据结构,它独立于 tableView 保存你的数据).您应该拥有它,以便当单元格滚动离开屏幕然后重新打开时,您可以恢复其中的数据.

A tableView should not store data. Data should be kept in your model (a data structure or data structures which hold your data independent of the tableView). You should have this so that when cells scroll off of the screen and then back on, you can restore the data that is in them.

当用户在单元格中输入数据时,您应该立即更新您的模型.

When the user enters data into a cell, you should immediately update your model.

按下保存按钮后,您应该将模型中的数据保存到更永久的位置(文件、Core Data、SQL 数据库).

When the save button is pressed, you should save your data from your model to a more permanent location (a file, Core Data, SQL database).

首先,让我们为 CardHandler 定义一个协议:

First, let's define a protocol for a CardHandler:

protocol CardHandler {
    func totalCards() -> Int
    func writeTerm(term: String, forCard cardNum: Int)
    func writeDefinition(definition: String, forCard cardNum: Int)
    func readTermForCard(cardNum: Int) -> String?
    func readDefinitionForCard(cardNum: Int) -> String?
    func addCard() -> Int
    func removeCardNum(cardNum: Int)
}

接下来,定义一个类作为您的模型:

Next, define a class to be your model:

class Cards: CardHandler {
    struct CardRecord {
        var term: String = ""
        var definition: String = ""
    }

    var cards = [CardRecord]()

    func totalCards() -> Int {
        return cards.count
    }

    func writeTerm(term: String, forCard cardNum: Int) {
        if cardNum < cards.count {
            cards[cardNum].term = term
        }
    }

    func writeDefinition(definition: String, forCard cardNum: Int) {
        if cardNum < cards.count {
            cards[cardNum].definition = definition
        }
    }

    func readTermForCard(cardNum: Int) -> String? {
        if cardNum < cards.count {
            return cards[cardNum].term
        }
        return nil
    }

    func readDefinitionForCard(cardNum: Int) -> String? {
        if cardNum < cards.count {
            return cards[cardNum].definition
        }
        return nil
    }

    func addCard() -> Int {
        cards.append(CardRecord())
        return cards.count - 1
    }

    func removeCardNum(cardNum: Int) {
        if cardNum < cards.count {
            cards.removeAtIndex(cardNum)
        }
    }

    func convertToDictionary() -> [String: String] {
        var dict = [String: String]()

        for card in cards {
            dict[card.term] = card.definition
        }

        return dict
    }
}

<小时>

为了在用户修改 textField 时收到通知,请将您的 CustomTableViewCell 类设为 UITextViewDelegate:


In order to be notified when the user has modified a textField, make your CustomTableViewCell class be a UITextViewDelegate:

class CustomTableViewCell: UITableViewCell, UITextViewDelegate {

    @IBOutlet weak var termTextView: UITextView!
    @IBOutlet weak var definitionTextView: UITextView
    weak var cardHandler: CardHandler?

    var rowInTable = 0   // set this in cellForRowAtIndexPath to indexPath.row

    override func awakeFromNib() {
        super.awakeFromNib()

        termTextView.delegate = self
        definitionTextView.delegate = self
    }

    func textViewDidChange(textView: UITextView) {
        let text = textView.text

        if textView == termTextView {
            cardHandler?.writeTerm(text, forCard: rowInTable)
        } else if textView == definitionTextView {
            cardHandler?.writeDefinition(text, forCard: rowInTable)
        }
    }
 }

在你的 UITableViewController 中,有一个属性来包含你的模型:

In your UITableViewController, have a property to contain your model:

var cards = Cards()

cellForRowAtIndexPath中,为cell指定cards作为cardHandler:

let cell = dequeReusableCell ... as CustomTableViewCell
cell.rowInTable = indexPath.row
cell.cardHandler = cards
cell.contentView.termTextView.text = cards.readTermForCard(indexPath.row) ?? ""
cell.contentView.defintionTextView.text = cards.readDefinitionForCard(indexPath.row) ?? ""

对于numberOfRowsInSection:

return cards.totalCards()

对于 plusBarButtonItemPressed:

@IBAction func plusBarButtonItemPressed(sender: AnyObject) {
    let newCardNum = cards.addCard()
    let insertionIndexPath = NSIndexPath(forRow: newCardNum, inSection: 0)
    tableView.insertRowsAtIndexPaths([insertionIndexPath], withRowAnimation: .Automatic)
}

<小时>

要将您的卡片作为字典,调用 cards.convertToDictionary(),它会返回带有 term[String: String] 字典作为定义作为.


To get your cards as a dictionary, call cards.convertToDictionary() which returns a [String: String] dictionary with the term as the key and the definition as the value.

这篇关于从所有 TableViewCell 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆