每次呼叫pushViewController
此控制器时,都会收到消息“数组索引超出范围”。
我找到了很长时间的原因。
我在使用它时检查了每个Array的长度和索引。
但要找出原因真的很难。
所以,我在这里粘贴,希望有人能找到原因。
import UIKit
class Notification: UIViewController, UITableViewDelegate, UITableViewDataSource {
var NotImg = ["one","one","one","one","one","one"]
var NotName = ["系统通知","系统通知","系统通知","系统通知","系统通知","系统通知"]
var NotDetail = ["aaa","bbb","ccc","ddd","eee","fff"]
@IBOutlet weak var TV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
TV.delegate = self
TV.dataSource = self
self.title = "通知"
TV.tableFooterView = UIView()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return NotName.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.imageView?.image = UIImage(named: NotImg[indexPath.row])
cell.textLabel?.text = NotName[indexPath.row]
cell.detailTextLabel?.text = NotDetail[indexPath.row]
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50.0
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("NotDetail") as! NotificationDetail
self.navigationController?.pushViewController(VC, animated: true)
}
override func viewWillAppear(animated: Bool) {
setTabBarVisible(!tabBarIsVisible(), animated: true)
}
func setTabBarVisible(visible:Bool, animated:Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? CGFloat(0): height)
// zero duration means no animation
let duration:NSTimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animateWithDuration(duration) {
self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
return
}
}
}
func tabBarIsVisible() ->Bool {
return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}
}
当程序推送到此控制器时,这是消息的屏幕截图。
好。这是下一个ViewController(NotDetail)的代码。
import UIKit
class NotificationDetail: UIViewController {
@IBOutlet weak var NotDetail: UILabel!
@IBOutlet weak var NotTime: UILabel!
@IBOutlet weak var NotTitle: UILabel!
@IBOutlet weak var NotImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
NotImg.image = UIImage(named: "focus")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
这是跳转到此ViewController之前ViewController(Message)的代码。
import UIKit
class Message: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var UsersNameA = ["大哈","二哈","三哈","四哈","五哈","六哈"]
var UsersMesA = ["啊","啊","啊啊","啊啊","啊啊啊","啊啊啊啊"]
@IBOutlet var MessageTV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
MessageTV.delegate = self
MessageTV.dataSource = self
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
MessageTV.tableFooterView = UIView()
let options = PullToRefreshOption()
options.backgroundColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
options.indicatorColor = UIColor.blackColor()
self.MessageTV.addPullToRefresh(options: options, refreshCompletion: { [weak self] in
// some code
self!.MessageTV.reloadData()
self!.MessageTV.stopPullToRefresh()
})
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
if indexPath.row == 0 {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Not") as! Notification
self.navigationController?.pushViewController(VC, animated: true)
}
if indexPath.row == 1 {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Comment") as! Comment
self.navigationController?.pushViewController(VC, animated: true)
}
else {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Chat") as! ChatRoomViewController
VC.UserName = UsersNameA[indexPath.row - 2]
self.navigationController?.pushViewController(VC, animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return UsersNameA.count + 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! UITableViewCell
cell.imageView?.image = UIImage(named: "not")
cell.textLabel?.text = "通知"
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
if indexPath.row == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! UITableViewCell
cell.imageView?.image = UIImage(named: "mes")
cell.textLabel?.text = "消息"
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier("cell2") as! UITableViewCell
cell.imageView?.image = UIImage(named: "one")
cell.textLabel?.text = UsersNameA[indexPath.row - 2]
cell.detailTextLabel?.text = UsersMesA[indexPath.row - 2]
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50.0
}
override func viewWillAppear(animated: Bool) {
setTabBarVisible(!tabBarIsVisible(), animated: true)
}
func setTabBarVisible(visible:Bool, animated:Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? CGFloat(0) : height)
// zero duration means no animation
let duration:NSTimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animateWithDuration(duration) {
self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
return
}
}
}
func tabBarIsVisible() ->Bool {
return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}
}
答案 0 :(得分:2)
我已经解决了这个问题!!!!
原因是我的语法很差!!!
此代码导致崩溃
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
if indexPath.row == 0 {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Not") as! Notification
self.navigationController?.pushViewController(VC, animated: true)
}
if indexPath.row == 1 {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Comment") as! Comment
self.navigationController?.pushViewController(VC, animated: true)
}
else {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Chat") as! ChatRoomViewController
VC.UserName = UsersNameA[indexPath.row - 2]
self.navigationController?.pushViewController(VC, animated: true)
}
}
我错误地使用了if-else语句然后导致了这个问题。
如果indexPath == 0并且它将执行第一个闭包和第三个闭包。
崩溃来了。 :(
感谢您的回答。
答案 1 :(得分:0)
您正在didSelectRowAtIndexPath中调用tableView.deselectRowAtIndex。
请删除该行和您的 代码应该没问题。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("NotDetail") as! NotificationDetail
self.navigationController?.pushViewController(VC, animated:true)
}