所以這裏UITable的細胞是TableView中的結構: 有一個主的UITableView,每個的UITableViewCell裏面還有另外一個的UITableView獲取indexPath嵌入在UITableViewCell的
每個UITableViewCells都被設計成自定義視圖,並且已經通過將它從Nib中加載到cellForRowAtIndexPath函數。
我想要做的就是在內部表視圖我想要得到的是表視圖中內嵌的單元格的索引路徑選擇任何選項
我試圖遵循Paulw11這裏提到的委託方法: swift: how to get the indexpath.row when a button in a cell is tapped?: StackOverflow
注:通過Paulw11建議的方法完美地工作
代碼(TableVC):
class TableVC: UITableViewController, QuestionCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 5
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 220.0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("QuestionCell", owner: self, options: nil)?.first as! QuestionCell
cell.delegate = self
return cell
}
func sendCellInfo(cell: UITableViewCell) {
print(cell)
let indexPathForQuestion = tableView.indexPath(for: cell)
print(indexPathForQuestion)
}}
代碼(QuestionCell):
protocol QuestionCellDelegate: class {
func sendCellInfo(cell: UITableViewCell)
}
class QuestionCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var optionsTableview: UITableView!
var delegate: QuestionCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.optionsTableview.delegate = self
self.optionsTableview.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("OptionsCell", owner: self, options: nil)?.first as! OptionsCell
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = optionsTableview.cellForRow(at: indexPath)
print("selectedCell")
self.delegate?.sendCellInfo(cell: selectedCell!)
}}
代碼(OptionsCell):
class OptionsCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}}
注:當前的O/P爲零
注:代碼變更每通過pbasdf評價,實現了錯誤
我不知道你爲什麼要採取這一方式,我建議你用第(問題標題)簡單的UITableView和各部分去了的細胞(選項)具體數量。使用這種方法,您可以輕鬆處理上述情況。只要按照我對上述案例的理解說,請忽略它是否不滿足您的需要。 – Bharath
@Bharath是的,這是一個更簡單的方法,甚至更好的設計明智。但是,我想知道如何做到這一點。如果你如何。 –
您在錯誤的地方使用委託設計模式。你的'QuestionCell'應該定義協議並且具有符合它的'delegate'屬性。你的'TableVC'應該採用這個協議並且實現相應的方法(並且在'cellForRowAt:'中設置單元'delegate'屬性爲self)。 'QuestionCell'中的'didSelectRowAt'方法應該調用'self.delegate?'上的協議方法。 – pbasdf