簡體   English   中英

從自定義Tableview節標題中獲取indexPath

[英]Get indexPath from custom Tableview section header

我創建了一個自定義視圖並將其添加到tableView部分標題中。

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if section == 2 {

            if (headerView == nil) {
                headerView = CustomHeaderSection(frame: CGRectMake(0,0,self.view.bounds.width,38))
            }
            headerView!.delegate = self

            if isExpandableCell {

                headerView!.arrowImage.transform = CGAffineTransformIdentity

            }else {

                headerView!.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

            }
            return headerView

        }

        return nil
    }

我在tableview部分添加了自定義視圖上的按鈕。 現在,當我點擊它時,如何獲取indexPath?

在CustomHeader類中聲明一個節號變量,並在viewForHeaderInSection()內實例化時賦值。

在同一個類中實現目標按鈕操作。 因此,點擊get self.section並將其傳遞給ViewController。

void onButtonTap (id sender)
        {
            UIButton button = (UIButton)sender;

        this.sectionHeaderViewDelegate.buttonTapped (this.section);

        }

在您的函數上,您可以向按鈕添加標記ID,使其與indexPath相同

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FoldingCell", forIndexPath: indexPath) as! myTableViewCell
...
cell.myButton.tag = indexPath.row
...
}

您可以將節標題視圖的tag屬性分配給tableView(tableView: UITableView, viewForHeaderInSection section: Int)方法中的節數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM