簡體   English   中英

在UITableView中點擊時,找出節標題的一部分

[英]Figure out section of a section header when tapped in a UITableView

我有一個UITableView與節標題的自定義視圖。 我在客戶部分標題視圖中添加了一個UITapGestureRecognizer,以檢測是否有人點擊了部分標題。

如何確定節標題屬於哪個部分?

提前致謝。

最簡單的方法是在節頭視圖類上指定一個屬性來保存節索引,然后在-tableView:viewForHeaderInSection:為該屬性分配索引-tableView:viewForHeaderInSection:如下所示:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // CustomHeaderView *headerView = ...

    headerView.section = section;

    // ...
    return headerView;
}

然后讓手勢回調查看該屬性。

在viewDidLoad部分插入你的gestureRecognizer:

- (void)viewDidLoad
{
    (...)

    UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapTable:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;
    [self.yourTable addGestureRecognizer:doubleTap];

    (...)

}

如果您只想檢測單擊更改doubleTap.numberOfTapsRequired為1。

然后添加以下方法。 這將檢查分接點是否在節標題內:

-(void)doubleTapTable:(UISwipeGestureRecognizer*)tap
{
    if (UIGestureRecognizerStateEnded == tap.state)
    {
        CGPoint p = [tap locationInView:tap.view];

        NSIndexPath* indexPath = [yourTable indexPathForRowAtPoint:p];

        if(indexPath){  // user taped a cell

            // whatever you want to do if user taped cell

        } else { // otherwise check if section header was clicked 

            NSUInteger i;

            for(i=0;i<[yourTable numberOfSections];i++) {

                CGRect headerViewRect = [yourTable rectForHeaderInSection:i];

                BOOL isInside = CGRectContainsPoint (headerViewRect,
                                                     p);
                if(isInside) {

                    // handle Header View Selection

                    break;
                }

            }
        }

    }
}

您提供的action方法必須具有以下簽名

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

gestureRecognizer具有以下屬性:

獲得識別器的狀態和視圖
國家財產
查看屬性
啟用屬性

所以基本上你可以要求它附加並查詢該視圖的視圖。

這里的聚會有點晚了,但這可能是一個難以解決的問題,特別是如果(正如@klyngbaek在評論中提到的那樣),你正在添加/刪除部分。 通過重新加載整個部分來更改標題UIView上的標記或自定義索引屬性可能會導致丑陋的動畫。

嘗試將此作為附加到每個標題UIView的手勢識別器的回調方法(誠然,hackey):

- (void)headerTapped:(UITapGestureRecognizer *)sender{

    NSInteger section = 0;
    for(int counter = 0; counter < [self.tableViewOfInterest numberOfSections]; counter++){
        if([[self.tableViewOfInterest headerViewForSection:counter] frame].origin.y == sender.view.frame.origin.y){
            section = counter;
            break;
        }
    }

}

基本上,當為每個節標題詢問UITableView時,它會返回標題的實例,並將框架設置為表格中標題的位置。 將其與UITapGestureRecognizerview屬性的幀進行比較將導致某個點匹配(沒有雙關語)!

您可以向標題添加按鈕並將標記設置為按鈕,如下所示:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.height, tableView.frame.size.width)];
    UIButton *button = [[UIButton alloc] initWithFrame:headerView.frame];
    button.tag = section;
    [button addTarget:self action:@selector(detectSection:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubView:button];
    return headerView;
}

-(void)detectSection:(UIButton *)sender {
    switch(sender.tag) {
    //your code
    }
}

暫無
暫無

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

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