使用UISearchController搜索自定义对象

时间:2015-05-11 10:35:54

标签: ios swift uisearchcontroller

我正在尝试创建一个UISearchController来搜索自定义的Team对象,然后在filteredArray中添加和删除它们。然而,我似乎遇到了一种我似乎无法解决的奇怪行为。例如,tableView可以如下所示:

enter image description here

然后当我在“Copen”上搜索时,它返回:

enter image description here

为什么选择它不应该是什么?

我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell

    if (self.teamSearchController.active) {
         let team = filteredTableData[indexPath.row] as! Team
        cell.textLabel?.text = filteredTableData[indexPath.row].name

    } else {
    let team = self.teamArray[indexPath.row] as Team
    cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20)
    cell.textLabel?.text = self.teamArray[indexPath.row].name as String

    }



    var removed = false

    for (index, value) in enumerate(self.teamSelected) {
        if (value.id == team.id) {

            cell.accessoryView = cell.accessoryCheck
            removed = true
        }
    }

    if (!removed) {
        cell.accessoryView = cell.accessoryUncheck

    }

    cell.selectionStyle = UITableViewCellSelectionStyle.None


    return cell
}

UpdateSearchController

func updateSearchResultsForSearchController(searchController: UISearchController)
{

    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR shortname CONTAINS[c] %@", searchController.searchBar.text, searchController.searchBar.text)
    let array = (teamArray as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [Team]
    filteredTableData = array

    self.tableView.reloadData()
}

LLDB错误:

* thread #1: tid = 0x1a4be2, 0x0000000106e7d52c  libswiftCore.dylib`function signature specialization <Arg[0] = Exploded,   Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of  Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString,  Swift.StaticString, Swift.UInt) -> () + 44, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
 frame #0: 0x0000000106e7d52c libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 44
 * frame #1: 0x000000010484c548 Striky`Striky.TeamViewController.tableView (tableView=0x00007fe402887000, indexPath=0xc000000000000016, self=0x00007fe401d4ec20)(ObjectiveC.UITableView, cellForRowAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UITableViewCell + 3592 at TeamViewController.swift:181
 frame #2: 0x000000010484c94f Striky`@objc Striky.TeamViewController.tableView (Striky.TeamViewController)(ObjectiveC.UITableView, cellForRowAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UITableViewCell + 79 at TeamViewController.swift:0
 frame #3: 0x0000000105beaa28 UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
 frame #4: 0x0000000105bc9248 UIKit`-[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
 frame #5: 0x0000000105bdf8a9 UIKit`-[UITableView layoutSubviews] + 210
 frame #6: 0x0000000105b69a2b UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
 frame #7: 0x0000000104fe1ec2 QuartzCore`-[CALayer layoutSublayers] + 146
frame #8: 0x0000000104fd66d6 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 380
frame #9: 0x0000000104fd6546 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
frame #10: 0x0000000104f42886 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 242
frame #11: 0x0000000104f43a3a QuartzCore`CA::Transaction::commit() + 462
frame #12: 0x0000000104f440eb QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 89
frame #13: 0x00000001053e1ca7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #14: 0x00000001053e1c00 CoreFoundation`__CFRunLoopDoObservers + 368
frame #15: 0x00000001053d7a33 CoreFoundation`__CFRunLoopRun + 1123
frame #16: 0x00000001053d7366 CoreFoundation`CFRunLoopRunSpecific + 470
frame #17: 0x0000000108b30a3e GraphicsServices`GSEventRunModal + 161
frame #18: 0x0000000105ae9900 UIKit`UIApplicationMain + 1282
frame #19: 0x0000000104846c57 Striky`main + 135 at AppDelegate.swift:13
frame #20: 0x0000000108094145 libdyld.dylib`start + 1
frame #21: 0x0000000108094145 libdyld.dylib`start + 1

1 个答案:

答案 0 :(得分:1)

选择该小组是因为let team = self.teamArray[indexPath.row] as Team indexPath.row == 0返回Cloud9而不是狼。要修复它,请从filteredTableData获取团队,如下所示

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell

var team : Team!

if (self.teamSearchController.active) {
    team = filteredTableData[indexPath.row] as! Team
    cell.textLabel?.text = team.name

} else {
team = self.teamArray[indexPath.row] as Team
cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20)
cell.textLabel?.text = team.name as String

}

var removed = false

for (index, value) in enumerate(self.teamSelected) {
    if (value.id == team.id) {

        cell.accessoryView = cell.accessoryCheck
        removed = true
    }
}

if (!removed) {
    cell.accessoryView = cell.accessoryUncheck

}

cell.selectionStyle = UITableViewCellSelectionStyle.None


return cell
}