无法弄清楚为什么我得到“班级没有初始化者”

时间:2016-06-23 10:01:21

标签: ios uitableview swift2

我正在使用Xcode 7.3.1开发swift2.2并且我有一个类,其中我收到“类没有初始化器”的错误。 经过这么多的谷歌搜索,我无法弄清楚为什么会发生这种情况?

我在我的视图控制器中使用tableview,并在tableview中添加了一个搜索栏,而不是我使用所有那些普通类和tableview扩展名。

任何帮助都会非常明显。

由于

import UIKit
import Foundation
import RealmSwift


class ownersListView: UIViewController
{
    //MARK:- Properties

    var arSelectedRows:NSMutableArray?
    var arSelectedRowValues:NSMutableArray?
    var ownerIDList:NSMutableArray?
    var userNameArray:NSMutableArray?
    var companyOwnerList:NSMutableArray?

    var ownerPicklistObject:OwnerPicklist?
    var searchResults:Results<OwnerPicklist>?
    var owner:Results<OwnerPicklist>?

    var searchBarActive:Bool

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad()
    {
        arSelectedRows = NSMutableArray()
        userNameArray = NSMutableArray()
        companyOwnerList = NSMutableArray()
        ownerIDList = NSMutableArray()

        ownerPicklistObject = OwnerPicklist()

        owner = try! Realm().objects(OwnerPicklist).sorted("ownerPicklistFirstname")

        tableView.reloadData()

        if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
        {
            if ((NSUserDefaults.standardUserDefaults().objectForKey("addcontactOwnerList")) != nil)
            {
                arSelectedRows = NSUserDefaults.standardUserDefaults().objectForKey("addcontactOwnerList") as? NSMutableArray
            }
        }

        tableView.tableFooterView = UIView()

        reloadTableData()
    }


    //MARK:- Custom methods

    func reloadTableData() -> Void
    {
        companyOwnerList?.removeAllObjects()

        for i in 0..<owner!.count
        {
            ownerPicklistObject = owner![i]

            if i == 0 && (ownerPicklistObject?.ownerPicklistFirstname != nil)
            {
                companyOwnerList?.addObject(NSString(format: "%@ %@", (ownerPicklistObject?.ownerPicklistFirstname)!, (ownerPicklistObject?.ownerPicklistLastname)!))
            }
            else if i > 0 && !(companyOwnerList?.containsObject((ownerPicklistObject?.ownerPicklistFirstname)!))!
            {
                companyOwnerList?.addObject(NSString(format: "%@ %@", (ownerPicklistObject?.ownerPicklistFirstname)!, (ownerPicklistObject?.ownerPicklistLastname)!))
            }

            ownerIDList?.addObject((ownerPicklistObject?.ownerPicklistID)!)
        }

        tableView.reloadData()
    }

}


extension ownersListView:UITableViewDataSource, UITableViewDelegate
{

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if searchBarActive
        {
            if searchResults?.count == 0
            {
                let messageLabel = UILabel(frame: CGRectMake(UIScreen.mainScreen().bounds.origin.x, UIScreen.mainScreen().bounds.size.height / 2.5, UIScreen.mainScreen().bounds.size.width, 50.0))
                messageLabel.text = "No Owner Found."
                messageLabel.textColor = UIColor.blackColor()
                messageLabel.textAlignment = NSTextAlignment.Center
                messageLabel.font = UIFont.init(name: "Avenir-Medium", size: 16)
                messageLabel.numberOfLines = 4

                let msgView:UIView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))

                msgView.addSubview(messageLabel)

                tableView.backgroundView = msgView
            }
            else
            {
                tableView.backgroundView = nil
            }

            return searchResults!.count
        }
        else if owner?.count >= 1
        {
            tableView.backgroundView = nil
            return (owner?.count)!
        }
        else
        {
            if owner?.count == 0
            {
                let messageLabel = UILabel(frame: CGRectMake(UIScreen.mainScreen().bounds.origin.x, UIScreen.mainScreen().bounds.size.height / 2.5, UIScreen.mainScreen().bounds.size.width, 50.0))
                messageLabel.text = "No Data Available."
                messageLabel.textColor = UIColor.blackColor()
                messageLabel.textAlignment = NSTextAlignment.Center
                messageLabel.font = UIFont.init(name: "Avenir-Medium", size: 16)
                messageLabel.numberOfLines = 4

                let msgView:UIView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))

                msgView.addSubview(messageLabel)

                tableView.backgroundView = msgView
            }

            return 0
        }

    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        if searchBarActive
        {
            ownerPicklistObject = searchResults![indexPath.row]
        }
        else
        {
            ownerPicklistObject = owner![indexPath.row]
        }

        var ownerName:NSString

        if ownerPicklistObject?.ownerPicklistLastname != nil
        {
            ownerName = NSString (format: "%@ %@", (ownerPicklistObject?.ownerPicklistFirstname)!, (ownerPicklistObject?.ownerPicklistLastname)!)
        }
        else
        {
            ownerName = (ownerPicklistObject?.ownerPicklistFirstname)!
        }


        var isObjectThere:Bool

        if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
        {
            isObjectThere = NSUserDefaults.standardUserDefaults().objectForKey("addContactOwnerList")!.containsObject(ownerPicklistObject?.ownerPicklistID)
        }


        if isObjectThere
        {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryType.None
        }

        cell.textLabel?.text = ownerName as String
        cell.detailTextLabel?.text = ownerPicklistObject?.ownerPicklistEmail

        return cell
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

        if searchBarActive
        {
            ownerPicklistObject = searchResults![indexPath.row]
        }
        else
        {
            ownerPicklistObject = owner![indexPath.row]
        }

        if cell.accessoryType == UITableViewCellAccessoryType.None
        {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

            var ownerName:NSString

            if ownerPicklistObject?.ownerPicklistLastname != nil
            {
                ownerName = NSString (format: "%@ %@", (ownerPicklistObject?.ownerPicklistFirstname)!, (ownerPicklistObject?.ownerPicklistLastname)!)
            }
            else
            {
                ownerName = (ownerPicklistObject?.ownerPicklistFirstname)!
            }

            var isObjectThere:Bool

            if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
            {
                isObjectThere = NSUserDefaults.standardUserDefaults().objectForKey("addContactOwnerList")!.containsObject(ownerPicklistObject?.ownerPicklistID)
            }

            if !isObjectThere
            {
                if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
                {
                    arSelectedRows?.addObject((ownerPicklistObject?.ownerPicklistID)!)
                }
                else
                {
                    arSelectedRows?.addObject(ownerName)
                }
            }
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryType.None

            if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
            {
                arSelectedRows?.removeObject((ownerPicklistObject?.ownerPicklistID)!)
            }
            else
            {
                arSelectedRows?.removeObject((cell.textLabel?.text)!)
            }
        }

        if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
        {
            NSUserDefaults.standardUserDefaults().setObject(arSelectedRows, forKey: "addContactOwnerList")
        }

        if arSelectedRows?.count == 0
        {
            if ((backViewController(self)?.isKindOfClass(addContactViewController)) != nil)
            {
                NSUserDefaults.standardUserDefaults().removeObjectForKey("addContactOwnerList")
            }
        }

        tableView.reloadData()
    }

}

1 个答案:

答案 0 :(得分:2)

var searchBarActive: Bool

你必须明确searchBarActive声明:

var searchBarActive: Bool?
var searchBarActive: Bool!
var searchBarActive = false