如何从选定的tableview单元格中获取标题部分标题?

时间:2022-09-29 22:21:47

I am having a tableview which has several section. What I want is when I select a particular tableview cell from section I want title header of that section. I looked several SO question but didn't find out anything useful. Thanks in advance.

我有一个tableview,其中有几个部分。我想要的是当我从部分我想要该部分的标题标题中选择一个特定的tableview单元格时。我看了几个问题,但没有找到任何有用的东西。提前致谢。

3 个解决方案

#1


6  

You can get title with following code on tableviewcell click method

您可以使用tableviewcell单击方法获得以下代码的标题

UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:indexPath.section];
NSLog(@"Header text = %@", header.textLabel.text);

In Swift

let sectionHeaderView = table.headerViewForSection(indexPath.section)
let sectionTitle = sectionHeaderView?.textLabel?.text

#2


0  

Try following to get title if you are using "titleForHeaderInSection" table delegate :

如果您使用“titleForHeaderInSection”表委托,请尝试以下获取标题:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let title = self.tableView(tableView, titleForHeaderInSection: indexPath.row)

    }

#3


0  

Swift 4 : using cell button click

Swift 4:使用单元格按钮单击

    let buttonPosition:CGPoint = sender.convert(.zero, to:self.mytable)
    let indexPath12 = self.mytable.indexPathForRow(at: buttonPosition)

    let intSection : Int = (indexPath12?.section)!
    let intRow : Int = (indexPath12?.row)!

    let myHeaderView = tableView(self.mytable, viewForHeaderInSection: indexPath?.section)
    let labels = myHeaderView?.subviews.compactMap { $0 as? UILabel }

    var sectioTitle = ""
    for label in labels! {
        // you will get your title
        sectioTitle = label.text!
    }

Swift 4 : using uitablview didselect method

Swift 4:使用uitablview didselect方法

    let myHeaderView = tableView(self.mytable, viewForHeaderInSection: indexPath?.section)
    let labels = myHeaderView?.subviews.compactMap { $0 as? UILabel }

    var sectioTitle = ""
    for label in labels! {
        // you will get your title
        sectioTitle = label.text!
    }

#1


6  

You can get title with following code on tableviewcell click method

您可以使用tableviewcell单击方法获得以下代码的标题

UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:indexPath.section];
NSLog(@"Header text = %@", header.textLabel.text);

In Swift

let sectionHeaderView = table.headerViewForSection(indexPath.section)
let sectionTitle = sectionHeaderView?.textLabel?.text

#2


0  

Try following to get title if you are using "titleForHeaderInSection" table delegate :

如果您使用“titleForHeaderInSection”表委托,请尝试以下获取标题:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let title = self.tableView(tableView, titleForHeaderInSection: indexPath.row)

    }

#3


0  

Swift 4 : using cell button click

Swift 4:使用单元格按钮单击

    let buttonPosition:CGPoint = sender.convert(.zero, to:self.mytable)
    let indexPath12 = self.mytable.indexPathForRow(at: buttonPosition)

    let intSection : Int = (indexPath12?.section)!
    let intRow : Int = (indexPath12?.row)!

    let myHeaderView = tableView(self.mytable, viewForHeaderInSection: indexPath?.section)
    let labels = myHeaderView?.subviews.compactMap { $0 as? UILabel }

    var sectioTitle = ""
    for label in labels! {
        // you will get your title
        sectioTitle = label.text!
    }

Swift 4 : using uitablview didselect method

Swift 4:使用uitablview didselect方法

    let myHeaderView = tableView(self.mytable, viewForHeaderInSection: indexPath?.section)
    let labels = myHeaderView?.subviews.compactMap { $0 as? UILabel }

    var sectioTitle = ""
    for label in labels! {
        // you will get your title
        sectioTitle = label.text!
    }