2013-10-28 19 views
2

我不明白爲什麼這UIButton不工作在我的UITableView的標題。它出現在那裏,但觸摸不起作用。UIButton不工作在我的UITableView的標題

NSLog語句不會被觸發。這幾乎就像是在另一個視圖或其他視圖之下,以便您可以看到它,但是新聞行爲不起作用。

感謝這個

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull]; 
sectionHeader.backgroundColor = [UIColor clearColor]; 

[sectionHeader addSubview:self.topMapView]; 

// add map launch button 
mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[mapLaunchButton addTarget:self 
        action:@selector(mapButtonTouch:) 
      forControlEvents:UIControlEventTouchDown]; 
[mapLaunchButton setTitle:@"ggggggg" forState:UIControlStateNormal]; 
mapLaunchButton.frame = CGRectMake(0, 0, 300, 90); 
mapLaunchButton.backgroundColor = [UIColor redColor]; 

[sectionHeader addSubview:mapLaunchButton]; 


    // [sectionHeader bringSubviewToFront:mapLaunchButton]; 

    self.tableView.tableHeaderView = sectionHeader; 

    return sectionHeader; 

} 
- (void)mapButtonTouch:(id)sender { 

    NSLog(@"map button was touched"); 
} 
+0

做你找到了解決辦法? –

+0

在我的情況下,包含uibutton的視圖禁用了userInteraction tick。 – jonypz

回答

2

我可以看到不止一個錯誤 -

  1. 如果你的表只有一個部分(以驗證該支票numberOfSectionsInTableView代表),然後刪除此行 -

    self.tableView.tableHeaderView = sectionHeader;

  2. sectionHeader.frame設置爲適當的值(而不是CGRectNull)。設置節標題(與表標題視圖)的好處是,當用戶將滾動表格行時,節標題將粘在頂部(浮動)並且不會消失。 (普通樣式表)

  3. 還有問題沒有解決,那麼請驗證他的方法 -

    - (CGFloat的)的tableView:(UITableView的*)的tableView heightForHeaderInSection:(NSInteger的)部分 { 回報50; //取決於該部分的高度 }

  4. 正如其他海報所指出的,在UIButton上捕獲觸摸事件UIControlEventTouchUpInside是首選事件。

編輯 - (如表頭視圖實現)

如果要滾動起來,然後讓它爲表頭(不節頭)。所以從viewForHeaderInSection中刪除所有這些,並將其放置在視圖控制器類的viewDidLoad中。請記住這行(不要在這種情況下將其刪除) -

self.tableView.tableHeaderView = sectionHeader; 

然而,上述點2和4仍然是成立的。

+0

謝謝,快速的東西,如果我刪除self.tableView.tableHeaderView = sectionHeader;那麼標題不再與表格視圖 – user2588945

+0

一起滾動,我相信你會知道這是什麼意思:如果我改變 [sectionHeader addSubview:self.mapLaunchButton];到[self.view addSubview:self.mapLaunchButton]; UIButton的作品,但它當然不滾動,因爲它是self.view的一部分,而不是頭。再次感謝你的幫助 – user2588945

2

任何幫助,如果你改變UIControlEventTouchDownUIControlEventTouchUpInside

+0

謝謝,但那不是。在我將它添加到表頭之前,這是另一種觀點。 – user2588945

1

有一件事是section header,另一件事是table header。您正在爲兩者分配相同的視圖。我不確定這是你問題的原因,但是這是讓你開始的東西。

無需實現該方法的,在你的viewDidLoad,創建視圖,其指定爲self.tableView.tableHeaderView

此外,最常見的用戶體驗與UIControlEventTouchUpInside(動作相關的不執行,直到手指擡起並仍在按鈕內)。但是,這可能與您的問題無關。這只是一個行動被調用的時間問題。

如果不能解決您的問題,讓我知道,我會嘗試看看是否有別的東西,我已經錯過了

乾杯!

0

我有同樣的問題。更改UIXXXXX從的UILabel到的UIView在下面的行爲我工作:

UIView *sectionHeader = [[UIXXXXX alloc] initWithFrame:CGRectNull]; 

,我沒有用:

self.tableView.tableHeaderView = sectionHeader; 
0

如果添加視圖

self.tableView.tableHeaderView = ({ 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)]; 

您可以添加新組件:UIImageView, UIlabel... 它的作品適合我! 我測試過了。

最終代碼:

self.tableView.tableHeaderView = ({ 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)]; 
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
     imageView.image = [UIImage imageNamed:@"avatar.jpg"]; 
     imageView.layer.masksToBounds = YES; 
     imageView.layer.cornerRadius = 50.0; 
     imageView.layer.borderColor = [UIColor whiteColor].CGColor; 
     imageView.layer.borderWidth = 3.0f; 
     imageView.layer.rasterizationScale = [UIScreen mainScreen].scale; 
     imageView.layer.shouldRasterize = YES; 
     imageView.clipsToBounds = YES; 
UIButton *mapLaunchButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [mapLaunchButton addTarget:self action:@selector(mapButtonTouch:) forControlEvents:UIControlEventTouchDown]; 
     [mapLaunchButton setTitle:@"ggggggg" forState:UIControlStateNormal]; 
     mapLaunchButton.frame = CGRectMake(0, 0, 300, 90); 
     mapLaunchButton.backgroundColor = [UIColor redColor]; 
     [view addSubview:imageView]; 
     [view addSubview:mapLaunchButton]; 
     view; 
});