Ios 推送到另一个UIViewController时数组索引超出范围

Ios 推送到另一个UIViewController时数组索引超出范围,ios,arrays,swift,indexing,Ios,Arrays,Swift,Indexing,每次我调用此控制器时,pushViewController,它都会崩溃,并显示消息“数组索引超出范围” import UIKit class Message: UITableViewController, UITableViewDataSource, UITableViewDelegate { var UsersNameA = ["大哈","二哈","三哈","四哈","五哈","六哈"] var UsersMesA = ["啊","啊","啊啊","啊啊","啊啊啊","啊啊啊啊"] @I

每次我调用此控制器时,
pushViewController
,它都会崩溃,并显示消息“数组索引超出范围”

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)
}
}
我已经找到原因很久了

我已经检查了每个数组的长度和使用时的索引

但是很难找到原因

所以,我粘贴在这里,希望有人能找出原因

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之前的ViewController代码(消息)

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)
}
}
导入UIKit
类消息:UITableViewController、UITableViewDataSource、UITableViewDelegate{
var UsersNameA=[”大哈","二哈","三哈","四哈","五哈","六哈"]
var UsersMesA=[”啊","啊","啊啊","啊啊","啊啊啊","啊啊啊啊"]
@ibvar MessageTV:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
MessageTV.delegate=self
MessageTV.dataSource=self
self.navigationItem.backBarButtonim=uiBarButtonim(标题:,样式:uiBarButtonimStyle.Plain,目标:nil,操作:nil)
self.navigationController!.navigationBar.tintColor=UIColor.whiteColor()
MessageTV.tableFooterView=UIView()
let options=PullToRefreshOption()
options.backgroundColor=UIColor(红色:239/255,绿色:239/255,蓝色:244/255,alpha:1)
options.indicatorColor=UIColor.blackColor()
self.MessageTV.addPullToRefresh(选项:选项,刷新完成:{[weak self]in
//一些代码
self!.MessageTV.reloadData()
self!.MessageTV.stoppelltorefresh()
})
//取消注释下一行以保留演示文稿之间的选择
//self.clearselectiononviewwillappear=false
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem()
}
重写func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
self.tableView.declerowatinexpath(indexPath,动画:false)
如果indexath.row==0{
将VC=self.storyboard?.instantialeviewController标识符(“Not”)设为!通知
self.navigationController?.pushViewController(VC,动画:true)
}
如果indexath.row==1{
让VC=self.storyboard?.instanceeviewcontrollerwhiteIdentifier(“Comment”)作为!Comment
self.navigationController?.pushViewController(VC,动画:true)
}
否则{
让VC=self.storyboard?.instantialeviewcontrollerwhiteIdentifier(“聊天”)作为!聊天室视图控制器
VC.UserName=UsersNameA[indexPath.row-2]
self.navigationController?.pushViewController(VC,动画:true)
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//标记:-表视图数据源
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
//#警告可能不完整的方法实现。
//返回节数。
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//#警告方法执行不完整。
//返回节中的行数。
返回UsersNameA.count+2
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
如果indexath.row==0{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell1”)设为!UITableViewCell
cell.imageView?.image=UIImage(名为“not”)
单元格.textLabel?.text=”通知"
cell.separatorInset=UIEdgeInsetsZero
cell.preservesPerViewLayoutMargins=false
cell.layoutMargins=UIEdgeInsetsZero
返回单元
}
如果indexath.row==1{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell1”)设为!UITableViewCell
cell.imageView?.image=UIImage(名为“mes”)
单元格.textLabel?.text=”消息"
cell.separatorInset=UIEdgeInsetsZero
cell.preservesPerViewLayoutMargins=false
cell.layoutMargins=UIEdgeInsetsZero
返回单元
}
否则{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell2”)设为!UITableViewCell
cell.imageView?.image=UIImage(名为“一”)
cell.textlab?.text=UsersNameA[indexPath.row-2]
cell.detailTextLabel?.text=UsersMesA[indexPath.row-2]
cell.separatorInset=UIEdgeInsetsZero
cell.preservesPerViewLayoutMargins=false
cell.layoutMargins=UIEdgeInsetsZero
返回单元
}
}
重写func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
返回50.0
}
覆盖功能视图将出现(动画:Bool){
setTabBarVisible(!tabBarIsVisible(),已设置动画:true)
}
func settabbar可见(可见:Bool,动画:Bool){
//*无法在viewDidLayoutSubviews()之前调用此函数,因为在此时间之前未设置帧
//如果当前状态与所需状态相匹配,则进行保释
如果(tabBarIsVisible()==可见){return}
//准备好框架计算
设frame=self.tabBar控制器?.tabBar.frame
让高度=框架?.size.height
let offsetY=(可见?CGFloat(0):高度)
//零持续时间意味着没有动画
let duration:NSTimeInterval=(动画?0.3:0.0)
//设置选项卡栏的动画
如果帧!=nil{
UIView.animateWithDuration(持续时间){
self.tabBar控制器?.tabBar.frame=CGRectOffset(frame!、0、offsetY!)
返回
}
}
}
func tabBarIsVisible()->Bool{
返回self.tabBar控制器?.tabBar.frame.origin.y
您正在调用tableView.DiselectRowatindex中的DidSelectRowatindex

请把那条线和你的 代码应该没问题

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: 
    let VC = self.storyboard?.instantiateViewControllerWithIdentifier("NotDetail") as! NotificationDetail
        self.navigationController?.pushViewController(VC, animated:true)
}

我已经解决了这个问题

原因是我可怜的gr
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)
}
}