- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚从 UITableViewController
更改为 UITableView
,一切正常,除了搜索栏,当点击键盘将使应用程序崩溃并出现错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier rideCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我没有对代码进行任何更改。有人建议更改:
let cell = tableView.dequeueReusableCellWithIdentifier("rideCell", forIndexPath: indexPath) as! RideCell
到:
let cell = tableView.dequeueReusableCellWithIdentifier("rideCell") as! RideCell
但这根本没有帮助。
有什么建议吗?
编辑:
这是我要求的代码:
初始化 View Controller 类:UIViewController、UITableViewDelegate、UITableViewDataSource {
@IBOutlet var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var rideName = ""
var parkPassed: Park!
var searchResults = NSArray()
var loadingIndicator = UIActivityIndicatorView()
var backgroundLabel = LabelWithInsets()
var refreshSpinner = UIRefreshControl()
func handleRefresh(refreshControl: UIRefreshControl) {
DataManager.sharedInstance.loadRides(parkPassed.name!.stringByReplacingOccurrencesOfString(" ", withString: ""))
refreshControl.endRefreshing()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
backgroundLabel.textAlignment = NSTextAlignment.Center
backgroundLabel.textColor = UIColorFromRGB(0x424242)
backgroundLabel.numberOfLines = 0
refreshSpinner.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshSpinner)
//refreshControl = refreshSpinner
self.tableView.tableHeaderView = searchBar
self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(searchBar.frame))
self.navigationController?.navigationBar.barTintColor = UIColorFromRGB(0x0096FF)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateTableView", name: "onRidesLoadedNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "displayError", name: "onRidesLoadFailedNotification", object: nil)
}
override func viewWillAppear(animated: Bool) {
if DataManager.sharedInstance.rideArray.count == 0 {
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
loadingIndicator.frame = CGRectMake((view.bounds.width / 2) - 25, (view.bounds.height / 2) - 50, 50, 50)
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(loadingIndicator)
loadingIndicator.startAnimating()
backgroundLabel.text = "Loading rides..."
backgroundLabel.frame = CGRectMake(0, (view.bounds.height / 2) - 80, view.bounds.width, 30)
view.addSubview(backgroundLabel)
}
DataManager.sharedInstance.loadRides(parkPassed.name!.stringByReplacingOccurrencesOfString(" ", withString: ""))
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
if NSUserDefaults.standardUserDefaults().arrayForKey("favourites") != nil {
let tempFavourites: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("favourites")!
favouritesArray = tempFavourites.mutableCopy() as! NSMutableArray
}
}
@IBAction func dismiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func updateTableView() {
tableView.reloadData()
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
loadingIndicator.removeFromSuperview()
backgroundLabel.removeFromSuperview()
}
func displayError() {
loadingIndicator.removeFromSuperview()
backgroundLabel.removeFromSuperview()
backgroundLabel.text = "Failed to load rides. Please check your internet connection."
backgroundLabel.frame = CGRectMake(0, (view.bounds.height / 2) - 80, view.bounds.width, 60)
view.addSubview(backgroundLabel)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
println(DataManager.sharedInstance.rideArray.count)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("rideCell", forIndexPath: indexPath) as! RideCell
var ride: Ride
if tableView == self.searchDisplayController?.searchResultsTableView {
ride = DataManager.sharedInstance.getRideByName(searchResults[indexPath.row].name)!
} else {
ride = DataManager.sharedInstance.rideAtLocation(indexPath.row)!
}
cell.rideNameLabel.text = ride.name
var dateSinceUpdate = NSDate().timeIntervalSinceDate(ride.updated!)
var secondsSinceUpdate = Int(dateSinceUpdate)
var timeSinceUpdate = printSecondsConvert(secondsSinceUpdate)
cell.updatedLabel.text = timeSinceUpdate
if ride.waitTime == "Closed" {
cell.waitTimeLabel.text = ride.waitTime!
cell.timeBackgroundView.backgroundColor = getColorFromNumber(80)
cell.waitTimeLabel.font = UIFont(name: "Avenir", size: 13)
} else {
cell.waitTimeLabel.text = "\(ride.waitTime!)m"
cell.timeBackgroundView.backgroundColor = getColorFromNumber(ride.waitTime!.toInt()!)
cell.waitTimeLabel.font = UIFont(name: "Avenir", size: 17)
}
AsyncImageLoader.sharedLoader().cancelLoadingURL(cell.rideImageView.imageURL)
cell.rideImageView.image = UIImage(named: "Unloaded")
cell.rideImageView.imageURL = NSURL(string: ride.rideImageSmall!)
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController?.searchResultsTableView {
return searchResults.count
} else {
return DataManager.sharedInstance.rideArray.count
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! RideCell!
rideName = currentCell.rideNameLabel.text!
performSegueWithIdentifier("showDetail", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showDetail") {
var viewController = segue.destinationViewController as! DetailViewController
viewController.currentRide = DataManager.sharedInstance.getRideByName(rideName)
viewController.parkPassed = parkPassed
}
}
func filterContentForSearchText(searchText: NSString) {
let resultPredicate = NSPredicate(format: "name contains[cd] %@", searchText) //Use either contains or beginswith
searchResults = (DataManager.sharedInstance.rideArray as NSArray).filteredArrayUsingPredicate(resultPredicate)
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 71
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! RideCell
let favourite = UITableViewRowAction(style: .Normal, title: " ") { action, index in
if favouritesArray.containsObject(cell.rideNameLabel.text!) {
favouritesArray.removeObject(cell.rideNameLabel.text!)
} else {
favouritesArray.addObject(cell.rideNameLabel.text!)
}
NSUserDefaults.standardUserDefaults().setObject(favouritesArray, forKey: "favourites")
tableView.setEditing(false, animated: true)
}
if favouritesArray.containsObject(cell.rideNameLabel.text!) {
favourite.backgroundColor = UIColor(patternImage: UIImage(named: "Unfavourite")!)
} else {
favourite.backgroundColor = UIColor(patternImage: UIImage(named: "Favourite")!)
}
return [favourite]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
最佳答案
在包含您的 TableView 的 View Controller 的 viewDidLoad
方法中调用下一个代码:
如果您在 xib 中创建了 RideCell
:
tableView.registerNib(UINib(nibName: "RideCell", bundle: nil),
forCellReuseIdentifier: "rideCell")
如果您在代码中创建了 RideCell
:
tableView.registerClass(RideCell.self, forCellReuseIdentifier: "rideCell")
关于ios - 将 UITableViewController 更改为 UITableView 会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663287/
我正在使用 Swift 制作一个应用程序,但遇到了一个问题:我有一个系统,人们可以在其中发布“帖子”,并对这些“帖子”发表评论。 为了显示这一点,我有一个 UITableViewController
用户点击“远程”按钮,然后加载以下 UITableViewController: 然后用户选择我调用的任何值: [self.navigationController popViewController
在我离开页面并使用 UITabBarController 返回后,我的 TableViewController 在页面顶部留下了一个空隙。在初始加载时它很好,只有当我离开页面然后返回时才会出现间隙。我
我有一个 TableViewController (VC1) 设置为推送到另一个 TableViewController (VC2)。这个连接是通过从 VC1 到 VC2 的 ctrl-drag 在
在我的核心数据模型中,我有一个 Person 实体,它与 Course 实体具有“对多”关系(我还具有从 Course 到 Person 的反向“对一”关系)。 现在我有一个子类化的 UITableV
为什么不调用 numberOfSectionsInTableView 方法?如果我在 viewDidLoad 中添加 [self.tableView reloadData],它就会被调用,但即便如此,
在 UITableViewController 中,我有一个方法可以更新显示总价的底部工具栏。 public override UITableViewCell GetCell(UITa
我正在制作一个 iPad 应用程序,其中包含一个用于设置选项的 View 。这个“选项” View 有两个容器 View ,每个容器 View 都包含一个 TableView,其中包含两种不同的选项,
假设设置是 导航 Controller -> GradLevelUITableViewController -> DegreeUITableViewController -> View Control
我的问题是,这是否可能,如果可能,您将如何完成这项任务? 有人知道已经在使用此功能的应用程序,或者可以从哪里获得一些示例代码吗? 此外,如果我要在对主题了解不多的情况下自行实现此操作,您估计需要多长时
当 UITableViewController 的框架低于特定高度时,UITableViewController refreshControl 会出现问题。 就目前而言,我有一个 UIViewCont
我有我的主视图和一个 RoutinesTableViewController。 我想放松回到我的 VC 并尝试了这个: @IBAction func selectRoutine(segue:UISto
我是 swift 和 Xcode 的新手,我正在尝试一些事情。当我转到设置页面时,我想在设置页面(在应用程序中,而不是在设置 bundle 中)设置开关状态。 (我将segue设置为push)为此,我
我目前正在尝试将不同的实体从我的 CoreData 模型加载到一个 UITableView 中,但位于不同的部分下。我尝试过以下方法: override func tableView(tableVie
我试图让 tableview 适应 Swift 中键盘的显示。根据文档,如果 tableview 是 UITableViewController 的子类,它应该自动调整。已经在这里阅读了很多答案,只是
我想捕获UITableViewController中触摸点的x位置。网上描述的最简单的解决方案是UITapGestureRecognizer:enter link description here 但
我是 iPhone 编程的初学者。我的第一个应用程序是 UITableViewController 但现在我想尝试一下。这个想法是创建一个如下所示的自定义布局: 布局: ---------------
我有一个基于导航的应用程序,我将 UITableViewControllers 推送到堆栈上。我想向我的所有 UITableViewControllers 添加背景 UImage。不是 UIColor
在找到位置后,我尝试在 UITableViewController 中重新加载数据。这也意味着在 viewDidLoad 方法之后。我的类正在使用此接口(interface)扩展 UITableVie
我有一个TabBarController,其中一个选项卡包含一个 subview ,它是一个navigationController。然后,我将继承 UITableViewController 形式的
我是一名优秀的程序员,十分优秀!