我遇到了一個問題,即從TableView頭呼叫一個與編程創建的TableView相關的segue。 作為權宜之計,我使用 Singleton 保存了對主 UITableViewController 的物件參考,但這并不是理想的解決方案。
我的TableViewController有多個部分,每個部分有1或2行,這取決于是否選擇了頂級部分的行。 第二行被有效地用于擴展在所選行上顯示的內容。
第二行包含一個包含滑塊選單的自定義單元格,并且根據所選的選單項,在容器視圖中顯示 5 個子視圖中的一個。
其中一個子視圖包含一個以編程方式生成的表視圖,該表視圖具有自己的自定義標題和一個自定義單元格。該單元格包含一個頁眉和一個嵌入式按鈕。當按鈕在頁眉處被按下時,我想跳轉到另一個導航控制器,然而,我的問題是我不能正確地初始化一個委托來訪問主TableViewController。
這就有點復雜了,因為自定義單元格創建了它自己的TableView,并且處理了它自己的功能,這些功能通常是通過多載在主控制器上執行的,例如didSelectRowAt、numberOfRowsInSelection、headerForRowAt等。
class pendingNotificationCustomCell。UITableViewCell, UITableViewDataSource, UITableViewDelegate{
var dataSample: [String] = ["Row 1, "Row 2", "Row 3" ]
var PendingTableView: UITableView?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:)未被實作" )
}
override func awakeFromNib() {
print("##### awakeFromNib")
super.awakeFromNib()
// 初始化代碼
setUpTable()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String? ) {
super.init(style: style , reuseIdentifier: reuseIdentifier)
setUpTable()
}
func setUpTable() {
PendingTableView = UITableView(frame: CGRect.init(), style:UITableView.Style.plain)
PendingTableView.delegate = self
PendingTableView?.dataSource = self
PendingTableView?.register(MyCell.self, forCellReuseIdentifier: "pendingnotificationsCellID")
PendingTableView?.register(PendingNotificationsHeader.self, forHeaderFooterViewReuseIdentifier: "pendingHeaderID")
PendingTableView?.sectionHeaderHeight = 40
PendingTableView?.allowselection = true
PendingTableView?.allowedMultipleSelection = false
self.addSubview(PendingTableView!)
我可以很容易地設定協議和相關的委托來訪問管理主TableView自定義單元格的控制器上的函式。然而,由于有問題的自定義單元格沒有繼承與執行分段有關的函式,我需要訪問主控制器上的函式。
我在這方面做了很多實驗,除了Singleton hack之外,我還沒有想到一個可行的解決方案。
let pendingCell = pendingNotificationCustomCell()
pendingCell.delegate4 = mStats.mainController
當我嘗試用一個初始化的outlet來分配delegate4,參考主TableViewController時,它的值總是為'nil'。 甚至當我用類中的Singleton值來分配它時,也會產生第二個 TableView。注釋出來的那一行失敗了,而使用mStats Singleton呼叫該方法時卻很正常。
//delegate4.callSegueFromCell2(myData: myData)
mStats.mainController? .callSegueFromCell2(myData: myData)
上面的delegate4被注釋掉了,在單元格頭類中被設定為:
。 protocol MyCustomCellDelegator3 {
func callSegueFromCell2(myData dataobject: AnyObject)
}
class PendingNotificationsHeader: UITableViewHeaderFooterView {
var delegate4:MyCustomCellDelegator3!
var MainTableViewController: MainTableViewController?
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
setupViews()
}
required init? (coder: NSCoder) {
fatalError("init(coder:) has not been implemented"/span>)
}
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Pending"
label.font = UIFont.systemFont(ofSize: 17)
label.textColor = .white
return label
}()
let priceAlertButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add new", for: .normal)
button.titleLabel?.font = UIFont(名稱: "Helvetica"/span>, size: 15)
button.setTitleColor(.white, for: .normal)
button.addTarget(self,action: #selector(createNewPriceAlertButtonPressed), for: .touchUpInside)
return按鈕
}()
@IBAction func createNewPriceAlertButtonPressed(_ sender: Any) {
print ("##### New Price Alert Label Pressed")
//獲取視圖var mydata = self >。
//delegate4.callSegueFromCell2(myData: myData)/span>
mStats.mainController?.callSegueFromCell2(myData: myData)
感謝任何指導。
uj5u.com熱心網友回復:
let pendingCell = pendingNotificationCustomCell()
看起來很奇怪。
你應該在tableView(cellForRow:at:)UITableViewDataSource方法中設定你的單元格。
uj5u.com熱心網友回復:
這個問題與在單元格上設定委托并沒有將其傳遞給章節標題有關。我最終解決了這個問題,具體如下:
我實作了一個函式和一個委托,并將其傳遞給章節標題。
我在MainViewController中實作了一個名為API_Segue_01的函式和委托
。final class MainTableViewController。UITableViewController, API_Segue_01 {
func API_Segue_01(myData dataobject: AnyObject) {
navigationItem.backBarButtonItem = UIBarButtonItem(標題。"Back", style: .plain, target: nil, action: nil)
self. performSegue(withIdentifier: "showCreateNotificationsViewController"/span>, sender:dataobject )
}
///其余的類代碼。
/// ...
在自定義單元格中,我創建了一個頭的原型來參考外部函式:
protocol API_Segue_01 {
func API_Segue_01(myData dataobject: AnyObject)
}
我定義了一個var來保存自定義單元格和相關標題中對MainTableViewController的參考
class pendingNotificationCustomCell。UITableViewCell, UITableViewDataSource, UITableViewDelegate{
var funcCallTo: API_Segue_01!
///其余類的代碼!
///...!
class PendingNotificationsHeader。UITableViewHeaderFooterView {
var funcCallTo:API_Segue_01!
///其余類的代碼!
///...!
當創建自定義單元格的實體時,我提供了一個對MainTableViewController的參考
。 pendingCell.funcCallTo = mainTableViewController
由于動作按鈕位于單元格標題中,我在viewForHeaderInSection中傳遞了對mainTableViewController的參考,如下所示
。func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print("##### viewForHeaderInSection"/span>)
let header = tableView. dequeueReusableHeaderFooterView(withIdentifier: "pendingHeaderID") as! PendingNotificationsHeader
header.funcCallTo = self.funcCallTo
return header
}
然后我就可以使用這個參考來呼叫API_Segue_01函式了
。@IBAction func createNewPriceAlertButtonPressed(_ sender: Any) {
print ("##### New Price Alert Label Pressed")
//獲取視圖var mydata = self /
funcCallTo.API_Segue_01(myData: mydata)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311722.html
標籤:
