[英]Dynamic Table View within Static Table View Cell
我對Swift編程還很陌生,現在我正在靜態表視圖的單元中實現動態表視圖。 我知道在stackoverflow上已經有很多解決方案,但是我意識到大多數解決方案都在Obj-C中,對此我還不太熟悉。
基本上,我有一個TableView在作為主表視圖控制器一部分的靜態表視圖的一個單元格中設置為動態的。 我現在遇到的問題是似乎沒有一種方法可以在不為靜態表視圖聲明它們的情況下實現數據源功能。 我為動態表聲明了一個@IBOutlet
(在這種情況下,我們稱其為dynamicTableView
)。
如果tableView
不是dynamicTableView
,則通過返回1
我設法通過在下面的代碼中返回1
來獲得override func numberOfSections(in tableView: UITableView)
:
override func numberOfSections(in tableView: UITableView) -> Int {
if tableView == dynamicTableView {
return data.count
}
else {
return 1
}
}
但是,我現在遇到的問題是實現override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
。 我不知道如果tableView
參數不是dynamicTableView
,而是靜態表視圖,將返回什么。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == dynamicTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "dynamic", for: indexPath) as! dynamicTableViewCell
cell.update(data[indexPath.row]) // A helper function declared in the dynamicTableViewCell.swift
return cell
}
else {
// What to return here?
}
}
謝謝!
編輯:我的意思是我似乎沒有一個不影響我的靜態表視圖的cellForRowAt
數據源函數。
如果numberForRows中有一個值,那么您必須像這樣退格
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == bloggerReviewTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "dynamic", for: indexPath) as! dynamicTableViewCell
cell.update(data[indexPath.row]) // A helper function declared in the dynamicTableViewCell.swift
return cell
}
else {
// What to return here?
let cell = tableView.dequeueReusableCell(withIdentifier: "other", for: indexPath) as! OtherTableCell
return cell
}
}
//
但是如果返回值為零,則在cellForRowAt內部不需要if語句,因為不會為其他表調用
如果靜態tableview單元格非常不同,則可以將其單獨子類化。 可以在靜態tableview單元的必需子類中添加動態tableview / collectionview。
//靜態tableview的類
let reviewCellId = "reviewCell"
class StaticTableClass: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
//register static cell classes
tableView.register(ReviewCell.self, forCellReuseIdentifier: reviewCellId)
//..
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reviewCellId, for: indexPath)
return cell
}
}
創建一個單獨的ReviewCell類,其中將包含動態UITableView,如下所示。
這樣一類將只處理一個表視圖的方法。
class ReviewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
lazy var dynamicTableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
addSubview(dynamicTableView)
dynamicTableView.register(UITableViewCell.self, forCellReuseIdentifier: "dynamicCellId")
}
// add further tableview methods in here
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.