如何解决UITableViewController的指定初始化错误?

时间:2015-05-19 03:04:46

标签: ios uitableview swift initialization designated-initializer

我是swift的新手,我在PlacesTableViewController类中声明初始化程序时遇到问题。它提示我“在从初始化程序返回之前没有调用Super.init”,当我添加Super.init时,它会提示我:

  

“必须调用超类的指定初始值设定项   '的UITableViewController'“

这是我的PlacesTableViewController.swift文件:

class PlacesTableViewController: UITableViewController { 

    var pa55DB : COpaquePointer = nil
    var selectStatement : COpaquePointer = nil;

    var chapterdata : Array<Entry> = []
    var ety : Entry
    var sqlString : String = "";

    var chpData : ChapterData

    init(pa55DB : COpaquePointer,selectStatement : COpaquePointer, chapterdata : Array<Entry>, ety : Entry, sqlString : String) {
        super.init()
        self.pa55DB = pa55DB
        self.selectStatement = selectStatement
        self.chapterdata = chapterdata
        self.ety = ety
        self.sqlString = sqlString

    }

    required init!(coder aDecoder: NSCoder!) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        chpData = ChapterData(newEntryArray : chapterdata, entryV : ety, paDB55 : pa55DB, selectStatement : selectStatement, sqlString : sqlString)
        chpData.openDB()
        chapterdata = chpData.prepareStartment()

        println("Testing...")
    }
...
}

这是我的另一个名为ChapterData.swift的类文件,用于打开数据库和检索数据

class Entry {

    let image : String!
    let heading : String!
    let title : String!
    let description : String!
    public init(image : String, heading : String, title : String, desc : String) {
        self.heading = heading
        self.image = image
        self.title = title
        self.description = desc
    }
}


class ChapterData {

    var paDB : COpaquePointer = nil;
    var selectStatement : COpaquePointer = nil;
    var sqlString : String = "";

    var newEntryArray : Array<Entry> = [];
    var entryV : Entry;

    init(newEntryArray : Array<Entry>, entryV : Entry, paDB55 : COpaquePointer, selectStatement : COpaquePointer, sqlString : String){
        self.newEntryArray = newEntryArray
        self.entryV = entryV
        self.paDB = paDB55
        self.selectStatement = selectStatement
        self.sqlString = sqlString
    }

    func openDB() {
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
        var docsDir = paths.stringByAppendingPathComponent("pa55.sqlite")


        if (sqlite3_open(docsDir, &paDB) == SQLITE_OK) {
            println("success open database")
        }
        else {
            println("failed to open database")
        }
    }

    func prepareStartment() -> Array<Entry>   {
        sqlString = "SELECT * FROM Chapter ORDER BY CID ASC"
        var cSql = sqlString.cStringUsingEncoding(NSUTF8StringEncoding)
        sqlite3_prepare_v2(paDB, cSql!, -1, &selectStatement, nil)

        println("startment start")

        while (sqlite3_step(selectStatement) == SQLITE_ROW) {

            let image_buf = sqlite3_column_text(selectStatement, 3)
            var image = String.fromCString(UnsafePointer<CChar>(image_buf))

            let heading_buf = sqlite3_column_text(selectStatement, 1)
            var heading = String.fromCString(UnsafePointer<CChar>(heading_buf))

            let title_buf = sqlite3_column_text(selectStatement, 2)
            var title = String.fromCString(UnsafePointer<CChar>(title_buf))

            let desc_buf = sqlite3_column_text(selectStatement, 4)
            var description = String.fromCString(UnsafePointer<CChar>(desc_buf))

            entryV = Entry(image: image!, heading: heading!, title: title!, desc: description!)
            newEntryArray.append(entryV)
        }
        println("select statement successful executed")
        return newEntryArray
    }

}

4 个答案:

答案 0 :(得分:4)

init混乱是ObjC&lt; - &gt;的已知限制。快速过渡。

解决此问题的一种方法是委托super init(nibName:bundle:)初始化程序。

class PlacesTableViewController: UITableViewController { 

    var chapterdata: [Entry]
    var chpData: ChapterData?
    var ety: Entry
    var pa55DB: COpaquePointer
    var selectStatement: COpaquePointer
    var sqlString: String


    init(pa55DB: COpaquePointer, selectStatement: COpaquePointer, chapterdata: [Entry], ety: Entry, sqlString: String) {
        self.pa55DB = pa55DB
        self.selectStatement = selectStatement
        self.chapterdata = chapterdata
        self.ety = ety
        self.sqlString = sqlString

        super.init(nibName: nil, bundle: nil)
    }


    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // …
}

答案 1 :(得分:2)

看看这是否可以解决您的问题:

override init(style: UITableViewStyle) {
    super.init(style: style)
}

init(pa55DB : COpaquePointer,selectStatement : COpaquePointer, chapterdata : Array<Entry>, ety : Entry, sqlString : String) {
    super.init(style: UITableView.Style.plain)
    self.pa55DB = pa55DB
    self.selectStatement = selectStatement
    self.chapterdata = chapterdata
    self.ety = ety
    self.sqlString = sqlString

}

UITableView Class Reference

来自Swift编程语言指南:

  

指定的初始值设定项是类的主要初始值设定项。一个   指定的初始化程序完全初始化所引入的所有属性   该类并调用适当的超类初始化程序继续   初始化过程超级链。

[read more]

<强>更新

上述代码实际上解决了您指定的初始化程序的问题。您现在需要正确初始化您的self.ety属性。

答案 2 :(得分:0)

也许尝试这样的事情:

class PlacesTableViewController: UITableViewController { 

   var chapterdata: [Entry]
   var chpData: ChapterData?
   var ety: Entry
   var pa55DB: COpaquePointer
   var selectStatement: COpaquePointer
   var sqlString: String


   convenience init(pa55DB: COpaquePointer, selectStatement: COpaquePointer, chapterdata: [Entry], ety: Entry, sqlString: String) {
       self.init()

       self.pa55DB = pa55DB
       self.selectStatement = selectStatement
       self.chapterdata = chapterdata
       self.ety = ety
       self.sqlString = sqlString

   }

   required init(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
   }

   // …
}

答案 3 :(得分:-1)

UITableViewController对高级用例没有任何好处。您可以始终将UIViewController子类化并在其中添加UITableView属性,如下所示:

class PlacesTableViewController: UIViewController {

    let tableView = UITableView(frame: CGRectZero, style: .Plain)

    override func loadView() {
        view = tableView
    }

    // Custom designated initializer
    init(pa55DB : COpaquePointer,selectStatement : COpaquePointer, chapterdata : Array<Entry>, ety : Entry, sqlString : String) {
        // setting up these properties
        super.init(nibName: nil, bundle: nil)  // designated initializer of the superclass 'UIViewController'"
    }
}

它的作用类似于UITableViewController

其他实用程序UITableViewController提供了refreshControl,您也可以为自己设置一个,clearsSelectionOnViewWillAppear机制也可以viewWillAppear()didSelectRowAtIndexPath实施根据你的需要。