简体   繁体   中英

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.

(I will have an if/else block that displays search results if there's anything in that array.)

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

    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 ; 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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