我是一名初学者,正在尝试通过将一维数据数组更改为二维。
这是原始实现:
Data Class:
class Emoji {
var symbol: String
var name: String
var description: String
var usage: String
init(symbol: String, name: String, description: String, usage: String)
{
self.symbol = symbol
self.name = name
self.description = description
self.usage = usage
}
}
Original Data Structure: An array of emojis:
var emojis: [Emoji] = [...]
@IBAction func unwindToEmojiTableView(segue: UIStoryboardSegue) {
guard segue.identifier == "saveUnwind" else { return }
let sourceViewController = segue.source as! AddEditEmojiTableViewController
if let emoji = sourceViewController.emoji {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
emojis[selectedIndexPath.row] = emoji
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = IndexPath(row: emojis.count, section: 0)
emojis.append(emoji)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
}
这就是我正在尝试做的事情:
My Data Structure: Array of arrays:
var emojis: [[Emoji]] = [ [Emoji(...), Emoji(...), ...], [Emoji(...), etc...] ]
@IBAction func unwindToEmojiTableView(segue: UIStoryboardSegue) {
guard segue.identifier == "saveUnwind" else { return }
let sourceViewController = segue.source as! AddEditEmojiTableViewController
if let emoji = sourceViewController.emoji {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
emojis[selectedIndexPath.row][selectedIndexPath.row] = emoji
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = IndexPath(row: ?, section: 0) emojis[?].append(emoji)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
}
但我不知道如何让它发挥作用。 block 引用中的代码(带问号)是我陷入困境的地方。请帮忙。
最佳答案
let newIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section)
emojis[selectedIndexPath.section].append(emoji)
如果您尝试附加到为特定部分选择的对象。
关于ios - 创建一个与二维数组数据结构一起使用的新 IndexPath(使用 Swift 进行应用程序开发,项目 EmojiDictionary,第 556 页),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161452/