【发布时间】:2017-03-07 10:12:49
【问题描述】:
所以这里是 TableView 的结构: 有一个主UITableView,每个UITableViewCell里面还有另一个UITableview
每个 UITableViewCells 都被设计为自定义视图,并通过从 cellForRowAtIndexPath 函数中的 Nib 加载它来添加。
我想要做的是对于在内部表格视图中选择的任何选项,我想要获取嵌入表格视图的单元格的索引路径。
我尝试遵循 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为nil
注意:根据pbasdf的评论修改代码,发现错误
【问题讨论】:
-
我不知道您为什么要采用这种方法,我建议您使用带有部分(问题标题)的简单 UITableView 并且每个部分都有特定数量的单元格(选项)。使用这种方法,您可以轻松处理上述情况。只是说根据我对上述案例的理解,如果不能满足您的需求,请忽略。
-
@Bharath 是的,这是一种更简单的方法,甚至更明智的设计。但是,我想知道如何做到这一点。如果你怎么做。
-
您在错误的地方使用了委托设计模式。您的
QuestionCell应该定义协议并具有符合它的delegate属性。您的TableVC应该采用协议并实现相应的方法(并且,在cellForRowAt:中将单元格delegate属性设置为self)。然后QuestionCell中的didSelectRowAt方法应该调用self.delegate?上的协议方法。 -
@pbasdf:你是对的。谢谢你。我确实做了更改,但现在我得到的 O/P 是 nil
-
@SupratikMajumdar 尝试使用
self.delegate?.sendCellInfo(cell: self)而不是self.delegate?.sendCellInfo(cell: selectedCell!)(即QuestionCell,而不是OptionCell)。
标签: ios swift uitableview