ios - 创建一个与二维数组数据结构一起使用的新 IndexPath(使用 Swift 进行应用程序开发,项目 EmojiDictionary,第 556 页)

标签 ios swift

我是一名初学者,正在尝试通过将一维数据数组更改为二维。

这是原始实现:

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/

相关文章:

ios - 根据速度更改快速的SKSprite节点动画

ios - 如何在接收远程通知时从应用程序委托(delegate)正确转换到 View Controller ?

iphone - 从 Xcode map 上移除多段线

ios - 以编程方式从另一个详细 View Controller 中的表中推送详细 View Controller

ios - SceneKit unproject Z 文档解释?

ios - 使用载波将图像从 iphone 应用程序上传到 Rails 应用程序

ios - 如何正确使用协议(protocol)来调用 GameViewController 的函数?

ios - 滚动时 UITableView 复选标记消失

ios - iOS 7 中的 UICollectionView 奇怪崩溃

swift - 使用计数器变量隐藏许多标签