【问题标题】:Get indexPath for cell of UITable embedded in UITableViewCell获取嵌入在 UITableViewCell 中的 UITable 单元格的 indexPath
【发布时间】: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


【解决方案1】:

由于 pbasdf 评论找到了解决方案:

TableVC 中的委托函数:

 func sendCellInfo(cell: UITableViewCell) {

    /*** Take the cell passed and convert to a CGPoint wrt to TableView ***/
    let cellPosition: CGPoint = cell.convert(cell.bounds.origin, to: self.tableView)

     /*** wrt to CGPoint find index on current TableView ***/
    /*** Returns as Section,Cell ***/
    let indexPathForSelectedCell = (tableView.indexPathForRow(at: cellPosition)?.row)

    print(indexPathForSelectedCell)
}

【讨论】:

    【解决方案2】:

    下面的答案是针对我所说的逻辑添加@Supratik Majumdar 请求的。

    Supratik 尝试使用以下代码,希望您能完成您的需求。

    //Initialize your question or answer in viewDidLoad or where ever you like to as shown below
    self.questionArray = ["Question1", "Question2"]
    self.optionArray = [["Option 1", "Option 2", "Option 3", "Option 4"], ["Option 1", "Option 2", "Option 3", "Option 4"]]
    
    
    //Make us of the following tableview delegate & datasource code
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return self.questionArray.count
        }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.OptionArray[section].count
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
        return self.questionArray[section]
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    
        let currentOptionArray = self.questionArray[section]
        let currentOption = currentOptionArray[indexPath.row]
    
        cell.textLabel.text = currentOption
    
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedIndex = indexPath
        let selectedQuestionIndex = indexPath.section
        let selectedOptionIndex = indexPath.row
    
        //Make use of the data you need in the above values
    }
    

    【讨论】:

      【解决方案3】:

      使用这个:

      tableView.cellForRowAtIndexPath(YourIndexPath) as!选项单元格

      您可以将自己的 indexPath 作为全局变量并将其归档到 didSelectRow 方法中

      你的索引路径 = 索引路径

      【讨论】:

        猜你喜欢
        • 2017-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多