首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchController和FetchRequest之间的同步检查点

UISearchController和FetchRequest之间的同步检查点
EN

Stack Overflow用户
提问于 2015-10-15 04:56:29
回答 1查看 85关注 0票数 0

我试图在搜索视图和非搜索视图之间同步accessoryType.Checkmark。我尝试将cell.accessoryType = .None设置在cellForRowAtIndexPath上的几个不同的位置,但是当我在fetchedResults和搜索结果之间切换时,我仍然会得到随机的检查点。我不知道我搞砸了什么,但我可以放心,这会是一件令人吃惊的蠢事。

我有一个设置好的UITableViewController。当它加载时,我将它配置为显示来自NSFetchRequest的项。它工作得很完美。

我也设置了一个UISearchController。它工作得很完美,并显示了我想要的结果。

当我在搜索和fetchRequest之间切换时,我遇到了随机检查点的问题。我要保存的东西数组工作得很好,正确的项目也在里面如有任何反馈,将不胜感激。我没有主意了。

下面是我在viewDidLoad类中在UITableViewController类中声明的相关属性:

代码语言:javascript
复制
// Create array to dump fetchResults
var unfilteredJingles = [Jingle]()

// Create array to store filtered search results
var filteredJingles = [Jingle]()

// Array where ones I want to save get added/removed
var jinglesToSave = [Jingle]()

// resultsSearchController
var resultsSearchController = UISearchController()

当选择单元格时,我已经将didSelectRowAtIndexPath配置为做两件事:

1)将该单元格的jinglesToSave附加到数组中,并在单元格旁边放置一个复选标记。这个很管用。

2)从jinglesToSave数组中删除该单元格的jinglesToSave。这个也能用。

我遇到的问题是,当我在searchResultsController.activesearchResultsController之间切换时,随机单元会被检查,而不是活动的。正确的细胞会被检查,但随机的细胞有时会被检查。

这是我的cellForRowAtIndexPath

代码语言:javascript
复制
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("jingleCell", forIndexPath: indexPath)

    // instantiate a local array and assign it to filtered/unfiltered results based on whether
    // the resultsSearchController is active
    var jingleArray = [Jingle]()

    if resultsSearchController.active {
        // set local array to the filtered array
        jingleArray = filteredJingles
        let jingle = jingleArray[indexPath.row]
        // Set labels for cell
        cell.textLabel?.text = jingle.jingleDescription
        cell.detailTextLabel?.text = jingle.jingleStar

    } else {
        // set the local array to the UN-filtered array
        jingleArray = unfilteredJingles
        // Get the jingle for the index
        let jingle = jingleArray[indexPath.row]
        // Set labels for cell
        cell.textLabel?.text = jingle.jingleDescription
        cell.detailTextLabel?.text = jingle.jingleStar
    }

    // Set checkmarks
    if self.jinglesToSave.count > 0 {
        // enumerate jinglesToSave...
        for (indexOfJinglesToSave, jingleToSave) in jinglesToSave.enumerate() {
            // if the object in the array of stuff to save matches the object in the index of the tableview
            if jingleArray[indexPath.row].hashValue == jinglesToSave[indexOfJinglesToSave].hashValue {
                // then set its accessoryView to checkmark
                cell.accessoryType = .Checkmark
            }
        }
    }

    return cell
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-15 05:03:14

在单元格去队列后,将单元格复选标记状态重置为默认状态(im猜测没有复选标记)。这可能是一个细胞循环的问题,它的标记出现在细胞上,它不应该在上面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33140167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档