2017-05-16 51 views
0

我想創建一個單元屬性並訪問它。我創建它懶惰,因爲它在tableView的初始化過程中無法訪問。爲什麼我無法創建訪問tableView行的惰性屬性?

我把它當作我的tableViewController子類的屬性,但我發現了以下錯誤:

Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'

我的代碼:

lazy var messageCell : CustomCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 1)) as! CustomCell 

然而,如果我發佈這個確切的行不lazy裏面的函數一切都正常......

回答

0

lazy變量只能聲明爲一個類的成員或者一個stru克拉。將@IBOutlet設置爲您的表格。我們稱之爲tblView。然後你可以聲明一個lazy實例化的變量,如下所示。

lazy var cell: CustomCell = { 
    return self.tblView.cellForRow(at: IndexPath(row: 1, section: 1)) as! CustomCell 
}() 

記住,變量必須是一個類的成員或一個結構和沒有被聲明的函數內部