简体   繁体   English

数组数据静态表视图

[英]Array of data static table view

I have a static table view that consists of several sections, each with several rows. 我有一个静态表视图,该视图由几个部分组成,每个部分都有几行。 The data in the cells is relatively simple: a static image (loaded from bundle) and a label with a static string. 单元格中的数据相对简单:一个静态图像(从包中加载)和一个带有静态字符串的标签。

I need to implement search for this table view. 我需要实现对该表视图的搜索。

Without having to convert the static table view to a dynamic table view, how would I implement search ? 无需将静态表视图转换为动态表视图, 如何实现搜索

I know how to implement search on a dynamic table view, and I've tried a few things to generate the array that would be necessary to examine the data model that would then generate the search results array. 我知道如何在动态表视图上实现搜索,并且我尝试了一些方法来生成检查数据模型所必需的数组,该数据模型随后将生成搜索结果数组。 I tried looping on the known number of sections and cells, but that doesn't work because not all cells exist in memory at the same time. 我尝试对已知数量的节和单元进行循环,但这不起作用,因为并非所有单元都同时存在于内存中。 I tried using scrollToRow and catching them as it scrolled but that didn't work either. 我尝试使用scrollToRow并在滚动时捕获了它们,但这也不起作用。

(I will have an if/else block that displays search results if there's anything in that array.) (如果该数组中有任何内容,我将有一个if / else块显示搜索结果。)

I would prefer to just have an array available, with which I can generate search results. 我希望只提供一个可用的数组,通过它可以生成搜索结果。 I want to avoid converting the table view to some kind of statically identified dynamic hack system (naming the images something like image_[row]_[col] because that would be a nightmare to add a static cell or reorder it). 我想避免将表视图转换为某种静态标识的动态hack系统(将图像命名为image_ [row] _ [col]之类的东西,因为这将是添加静态单元格或对其进行重新排序的噩梦)。 How can I generate an array that will be the cells in a static table view ? 我如何生成一个将成为静态表视图中单元格的数组

Edit: 编辑:

I have what appears to be a solution but I feel like a dirty dirty programmer for doing this. 我有一个解决方案,但是我感觉自己像个肮脏的程序员。 It's almost like I'm writing a goto to a Singleton Factory. 几乎就像我在为Singleton Factory写一个goto。

    self.tableView.isHidden = true
    let numSections = self.tableView.numberOfSections
    for _ in 0..<numSections {
        let newSectionArray = Array<KeyMenuTableViewCell>()
        cells.append(newSectionArray)
    }

    for aSectionIndex in 0..<numSections {
        let numRows = self.tableView.numberOfRows(inSection: aSectionIndex)
        for aRowIndex in 0..<numRows {
            let index = IndexPath(row: aRowIndex, section: aSectionIndex)
            self.tableView.scrollToRow(at: index, at: .bottom, animated: false)
            let cell = self.tableView.cellForRow(at: index) as! KeyMenuTableViewCell
            self.cells[aSectionIndex].insert(cell, at: aRowIndex)
        }
    }

    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
    self.tableView.isHidden = false

There is no data model in my table view controller when using a static table view with which I can perform search filtering. 使用静态表格视图可以执行搜索过滤时,表格视图控制器中没有数据模型。

Well, that's the problem. 好吧,这就是问题所在。 The search needs data to search. 搜索需要数据进行搜索。 Even if you keep your table as a static table, you will need to have a data model so to as have something to search. 即使将表保留为静态表,也需要具有数据模型才能进行搜索。

Under no circumstances should you attempt to search the table view as if it were the data. 在任何情况下,您都不应试图像搜索数据一样搜索表视图。 The table view is view ; 表格视图是view ; the data is model . 数据是模型 What you are searching needs to be model. 您要搜索的内容必须是模型。

Honestly, your code snippet is a hack, and you should listen to that little voice in your head that's telling you so. 坦白地说,您的代码段是一种hack,您应该听清楚自己脑子里的声音。 I guess it's handy to be able to use IB to edit the table and see how it'll look, but using a static table when you need access to the data in the table is the wrong way to go for exactly the reason you're complaining about: the data for the cells of a static table exist in the storyboard, and the cells typically aren't all in memory at the same time. 我想能够使用IB来编辑表并查看其外观很方便,但是由于需要访问表中的数据,因此在需要访问表中数据时使用静态表是错误的方法抱怨:静态表的单元格数据存在于情节提要中,并且通常这些单元格不会同时存储在所有内存中。

In order to search the table you need access to the data, and the best way to get access to the data is to control the data model yourself in the first place. 为了搜索表,您需要访问数据,而访问数据的最佳方法是首先控制您自己的数据模型。 There's no reason it should be "a royal pain" to change a dynamic table. 更改动态表没有任何理由是“皇家的痛苦”。 Create a property list or other suitable file to contain the data. 创建一个属性列表或其他合适的文件来包含数据。 If your table includes images, use the image names in the property list. 如果您的表包含图像,请在属性列表中使用图像名称。 Editing the table only should only require that you update the data file. 仅编辑表仅应要求您更新数据文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM