我已经对这个问题进行了广泛的研究,我已经尝试了书中的所有内容,但没有任何作用。有人可以帮忙吗?以下是错误和代码。如果您有任何问题,请与我们联系。
**Code**
//
// SecondViewController.swift
// Photos 2
//
// Created by Cameron on 1/17/17.
// Copyright © 2017 Cameron. All rights reserved.
//
import UIKit
//, UISearchResultsUpdating
class SecondViewController: UIViewController, UITableViewDelegate, UISearchResultsUpdating{
@IBOutlet var TableView: UITableView!
var photosArray = [Photo]()
var filteredPhotos = [Photo]()
var searchController:UISearchController!
override func viewDidLoad() {
print("Load")
super.viewDidLoad()
print("Load2")
self.photosArray += [Photo(name: "One")]
print("Load3")
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
TableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
print("Load4")
self.TableView.reloadData()
print("Load5")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateSearchResults(for searchController: UISearchController) {
print("updatesearchresults")
let searchText = searchController.searchBar.text
filterContentForSearchText(searchText: searchText!)
TableView.reloadData()
print("updatesearchresultsdone")
}
func numberOfSections(in tableView: UITableView) -> Int {
print("numberofsections")
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("TableView")
if(searchController.isActive){
print("TableViewFinishedFiltered")
return self.filteredPhotos.count
} else {
print("TableViewFinishedNormal")
return self.photosArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
print("TableView2")
let cell = TableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
TableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
var photo: Photo
if(searchController.isActive){
photo = self.filteredPhotos[indexPath.row]
} else {
photo = self.photosArray[indexPath.row]
}
cell.textLabel!.text = photo.name
print(photo.name)
print("TableView2Finished")
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Why is this being acessed")
tableView.deselectRow(at: indexPath, animated: true)
var photo: Photo
if(searchController.isActive){
photo = self.filteredPhotos[indexPath.row]
} else {
photo = self.photosArray[indexPath.row]
}
//Load the photo based off the name
print(photo.name)
}
func filterContentForSearchText(searchText: String, scope: String = "Title"){
print("FilterContent")
self.filteredPhotos = self.photosArray.filter({(photo: Photo) -> Bool in
var categoryMatch = (scope == "Title")
var stringMatch = photo.name.range(of: searchText)
print("FilterContentDone")
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(_ controller: UISearchController, shouldReloadTableForSearch searchString: String?) -> Bool {
print("SearchDisplayController")
self.filterContentForSearchText(searchText: searchString!, scope: "Title")
print("SearchDisplayControllerDone")
return true
}
func searchDisplayController(_ controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
print("SearchDisplayController2")
self.filterContentForSearchText(searchText: (self.searchController!.searchBar.text)!, scope: "Title")
print("SearchDisplayController2Done")
return true
}
}
**Console Output**
Load
Load2
Load3
TableView
TableViewFinishedNormal
Load4
TableView
TableViewFinishedNormal
Load5
TableView
TableViewFinishedNormal
TableView
TableViewFinishedNormal
TableView
TableViewFinishedNormal
2017-02-10 10:04:41.644 Photos 2[45848:651521] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:8042
2017-02-10 10:04:41.652 Photos 2[45848:651521] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7ffa99069000; frame = (0 20; 320 499); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60000004c4b0>; layer = <CALayer: 0x600000226080>; contentOffset: {0, -64}; contentSize: {320, 430}>) failed to obtain a cell from its dataSource (<Photos_2.SecondViewController: 0x7ffa96407b80>)'
*** First throw call stack:
(
0 CoreFoundation 0x00000001100fed4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010d41121e objc_exception_throw + 48
2 CoreFoundation 0x0000000110102e42 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010cfa666d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x000000010da429cd -[UITableView _configureCellForDisplay:forIndexPath:] + 222
5 UIKit 0x000000010da4e5eb -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 860
6 UIKit 0x000000010da4e7e2 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
7 UIKit 0x000000010da222b0 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295
8 UIKit 0x000000010da57b64 -[UITableView _performWithCachedTraitCollection:] + 110
9 UIKit 0x000000010da3e3be -[UITableView layoutSubviews] + 222
10 UIKit 0x000000010d9a5ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
11 QuartzCore 0x0000000112f44bf8 -[CALayer layoutSublayers] + 146
12 QuartzCore 0x0000000112f38440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13 UIKit 0x000000010d993928 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1509
14 UIKit 0x000000010dac38b1 -[UINavigationController _layoutViewController:] + 1625
15 UIKit 0x000000010dac26df -[UINavigationController _layoutTopViewController] + 341
16 UIKit 0x000000010dabfa25 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 921
17 UIKit 0x000000010dda5ad8 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 418
18 UIKit 0x000000010dda5e3a -[UINavigationTransitionView _cleanupTransition] + 766
19 UIKit 0x000000010d96dbd5 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222
20 UIKit 0x000000010d9697d2 +[UIViewAnimationState popAnimationState] + 307
21 UIKit 0x000000010dda584e -[UINavigationTransitionView transition:fromView:toView:] + 2808
22 UIKit 0x000000010dac495b -[UINavigationController _startTransition:fromViewController:toViewController:] + 3314
23 UIKit 0x000000010dac4ef9 -[UINavigationController _startDeferredTransitionIfNeeded:] + 874
24 UIKit 0x000000010dac5fdb -[UINavigationController __viewWillLayoutSubviews] + 58
25 UIKit 0x000000010dcbcdd7 -[UILayoutContainerView layoutSubviews] + 223
26 UIKit 0x000000010d9a5ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
27 QuartzCore 0x0000000112f44bf8 -[CALayer layoutSublayers] + 146
28 QuartzCore 0x0000000112f38440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
29 QuartzCore 0x0000000112f382be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
30 QuartzCore 0x0000000112ec6318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
31 QuartzCore 0x0000000112ef33ff _ZN2CA11Transaction6commitEv + 475
32 UIKit 0x000000010d8d9d9b _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
33 UIKit 0x000000010e0e477c __handleEventQueue + 5672
34 CoreFoundation 0x00000001100a3761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
35 CoreFoundation 0x000000011008898c __CFRunLoopDoSources0 + 556
36 CoreFoundation 0x0000000110087e76 __CFRunLoopRun + 918
37 CoreFoundation 0x0000000110087884 CFRunLoopRunSpecific + 420
38 GraphicsServices 0x000000011203ea6f GSEventRunModal + 161
39 UIKit 0x000000010d8e0c68 UIApplicationMain + 159
40 Photos 2 0x000000010ce1f41f main + 111
41 libdyld.dylib 0x00000001110ae68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Main.storyboard Description