How to manually call UITableView's titleForHeaderInSection method?
Pretty simple question, but I just can't get i开发者_如何学Got to work right. I need to be able to access the string header value of a section when a cell within that section is clicked.
My initial attempts haven't been working:
[tableView titleForHeaderInSection:1];
The reason I thought that would work is because I am also using this:
[tableView cellForRowAtIndexPath:indexPath];
Which works fine. I'm implementing the delegate, but I still get the warning that UITableView may not respond to titleForHeaderInSection. And then it crashes because of an unrecognized selector.
All I need to do is pass the string value of the cell title, and the section title into the next view.
What am I doing wrong? Is there a better approach? Thanks
You can access titleForHeaderInSection
from UITableViewController
and UITableView
: you just need to follow the objects chain.
Example starting at the view controller:
class TableViewController: UITableViewController {
func titleForHeaderInSection(indexPath:NSIndexPath) -> String? {
if let tableView = self.tableView {
if let dataSource = tableView.dataSource {
if dataSource.respondsToSelector("tableView:titleForHeaderInSection:") {
return dataSource.tableView!(tableView,
titleForHeaderInSection:indexPath.row)
}
}
}
return nil
}
}
titleForHeaderInSection
is the method of UITableViewDataSource
protocol which is implemented by the client class not UITabelView
.
So you can't call titleForHeaderInSection
on your UITableView
instance.
how about:
[tableView titleForHeaderInSection:indexPath.section];
Calling manually titleForHeaderInSection
did not work for me.
But a simple [tableView reloadData]
did the trick.
Hope this helps
精彩评论