错误:致命错误:索引超出范围xcode swift 2

时间:2016-06-26 13:38:26

标签: ios xcode swift swift2

我不太懂英语。 İ尝试用代码解释这个错误。

DBContextDelagate

import Foundation
import UIKit
import Parse

@objc protocol DBContextDelegate {
    optional func SingUpResponse(result : Bool)
    optional func LoginResponse(result : Bool)
    optional func GetUsersInfoResponse(result : AnyObject)
    optional func GetUsersInfo(result : Bool)
    optional func GetDataResponse(result : Bool)
}

class DBContext : UIViewController{


    var delegate : DBContextDelegate?

    func SingUp(user : User) {
        let pfUser = PFUser()
        pfUser.username = user.userUserName
        pfUser.password = user.userPassword
        pfUser.email = user.userEmail
        pfUser["fullname"] = user.userFullName


        let profilePhotoData = UIImageJPEGRepresentation(user.userProfilePhoto, 0.5)
        let profilePhotoFile = PFFile(name: "profilePhoto.jpg", data: profilePhotoData!)
        pfUser["profilePhoto"] = profilePhotoFile


        pfUser.signUpInBackgroundWithBlock { (success:Bool, error:NSError?) -> Void in

            if  (error != nil) {
                self.delegate?.SingUpResponse!(false)
                print(error)
            }else {
                self.delegate?.SingUpResponse!(true)

            }
        }

    }

    func Login(user:User){

            PFUser.logInWithUsernameInBackground(user.userUserName, password:user.userPassword) {
                (user: PFUser?, error: NSError?) -> Void in
                if user != nil {
                    self.delegate?.LoginResponse!(true)
                } else {
                    self.delegate?.LoginResponse!(false)
                }
            }
        }

    func GetUsersInfo(){

        var userArray = Array<UserInfo>()

        let query = PFUser.query()
        query!.findObjectsInBackgroundWithBlock {
            (objects:[PFObject]?, error:NSError?) -> Void in

            if error == nil {

            if let objects = objects {

                for object in objects {
                    let user = UserInfo()

                    user.userID = object.objectId!
                    user.userUserName = object["username"] as? String
                    user.userFullName = object["fullname"] as? String
                    user.userEmail = object["email"] as? String

                    let userPhoto = object["profilePhoto"] as! PFFile
                    userPhoto.getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in
                        user.userProfilePhoto = UIImage(data: data!)!
                    }

                    userArray.append(user)
                    print(userArray.count)

                }
                self.delegate?.GetUsersInfoResponse!(userArray)
                }
                self.delegate?.GetDataResponse!(true)
            } else {
                self.delegate?.GetDataResponse!(false)
            }
        }

    }
}

和HomeVC CollectionView

import UIKit
import Parse


class homeVC: UICollectionViewController,DBContextDelegate{

    var userInfoArray : [UserInfo] = [UserInfo]()

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = .whiteColor()

        self.navigationItem.title = PFUser.currentUser()?.username?.uppercaseString

        Singleton.sharedInstance.dbContext.delegate = self
        Singleton.sharedInstance.dbContext.GetUsersInfo()
        print("ViewDidLoad: \(userInfoArray.count)")
    }


    func GetUsersInfoResponse(result: AnyObject) {
        userInfoArray = result as! [UserInfo]
    }

    func GetDataResponse(result: Bool) {
        if result == true {
        }else {
            print("Olmadı")
        }
    }


    override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", forIndexPath: indexPath) as! headerVC
        //I have problem with this line index out of range
        let user = userInfoArray[indexPath.row]
        header.fullnameLbl.text = user.userFullName

        header.editProfileButton.setTitle("Profili Düzenle", forState: UIControlState.Normal)

        return header
    }




    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 0
    }

}

1 个答案:

答案 0 :(得分:1)

我完全错过了您返回补充视图,而不是正常的细胞。为每个部分返回这些视图。由于您的numberOfSectionsInCollectionView返回1,即使您的数组为空,也会因为您承诺存在的1部分调用collectionView(collectionView:viewForSupplementaryElementOfKind:atIndexPath)方法而崩溃。

解决方案可能是这样做:

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return userInfoArray.count
}

此外,确保在更改基础数据后重新加载集合视图:

func GetUsersInfoResponse(result: AnyObject) {
    userInfoArray = result as! [UserInfo]
    collectionView?.reloadData()
}