2016-10-25 43 views
1

所以這裏UITable的細胞是TableView中的結構: 有一個主的UITableView,每個的UITableViewCell裏面還有另外一個的UITableView獲取indexPath嵌入在UITableViewCell的

截圖: TableView Screenshot

每個UITableViewCells都被設計成自定義視圖,並且已經通過將它從Nib中加載到cellForRowAtIndexPath函數。

我想要做的就是在內部表視圖我想要得到的是表視圖中內嵌的單元格的索引路徑選擇任何選項

文檔佈局: Document Layout

我試圖遵循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評價,實現了錯誤

+0

我不知道你爲什麼要採取這一方式,我建議你用第(問題標題)簡單的UITableView和各部分去了的細胞(選項)具體數量。使用這種方法,您可以輕鬆處理上述情況。只要按照我對上述案例的理解說,請忽略它是否不滿足您的需要。 – Bharath

+0

@Bharath是的,這是一個更簡單的方法,甚至更好的設計明智。但是,我想知道如何做到這一點。如果你如何。 –

+1

您在錯誤的地方使用委託設計模式。你的'QuestionCell'應該定義協議並且具有符合它的'delegate'屬性。你的'TableVC'應該採用這個協議並且實現相應的方法(並且在'cellForRowAt:'中設置單元'delegate'屬性爲self)。 'QuestionCell'中的'didSelectRowAt'方法應該調用'self.delegate?'上的協議方法。 – pbasdf

回答

0

下面的答案是增加@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 
} 
0

使用這個:

tableView.cellForRowAtIndexPath(YourIndexPath)as!OptionCell

你可以做你自己的indexPath作爲全局變量和歸檔它didSelectRow方法

YourIndexPath = indexPath

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) 
} 
相關問題