2013-11-15 77 views
0

我有一個自定義單元格和節頭的TableView。標題顯示我的國家和自定義單元顯示我的城市。如何確定自定義UITableViewCell中的按鈕點擊?

它看起來像這樣:

美國

  • 紐約

  • 洛杉磯

德國

  • 柏林

  • Frakfurt

每一個細胞有一個按鈕。在按下之後,它將導航並將城市名稱推送到詳細視圖。

問題是,我不知道如何確定城市名稱後,我按下按鈕。

我有一個解決方案,但它不工作了:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:@"ortCell"]; 
    [cell.detailButton setTag:indexPath.row]; 
    cell.ortLabel.text = [managedObject valueForKey:@"stadtname"]; 
} 


- (IBAction)detailButton:(id)sender { 

    UIView *contentView = [sender superview]; 
    UITableViewCell *cell = (UITableViewCell *)[contentView superview]; 
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; 

    NSInteger section = cellIndexPath.section; 

     UIButton * myButton = sender; 
    int row=myButton.tag; 

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 

    StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"strasse"]; 
    self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    detail.currentStandort = self.selectedStandort; 
    [self.navigationController pushViewController:detail animated:YES]; 

} 

當我用我的舊代碼將只顯示正確的街道行中的第一部分。它沒有檢測到其他部分(國家);

當我選擇第三部分的第一個城市。它會顯示我第一部分中的第一個城市。

我希望你能幫助我。

+0

的可能重複[如何在iOS的參數的UITableView IndexPath傳遞給UIButton的@selector?](http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector -by-parameters-in-ios) – TheTiger

回答

0

您需要seperately發送行和部分信息,從而使標籤作爲

標籤=行* 1000 +部分。

現在從標籤中獲取的部分,使用行=標籤/ 1000 節=標籤%1000

使用這個值來做出正確的索引路徑,或者找到一些更好,然後2分鐘解決方案,使該行的值

注意其正確的解決方法將是

  1. 子類的UIButton
  2. 添加indexpath財產詮釋它
  3. 使類,使按鈕對象和insted的標籤設置indexpath財產。

在CustomButton.h

@interface CustomButton :UIButton 
@property (nonatomic,assign)NSIndexPath *path; 
@end 
中的CustomButton

。米

@implementation CustomButton 
@synthesize path; 
@end 

使用你的表視圖單元格這個自定義按鈕和標籤,而不是設置路徑屬性

+0

你能舉個例子嗎? –

+0

[cell.detailButton setTag:indexPath.row]; insted這行使用long int tag = indexPath.row * 1000 + indexPath.section,[cell.detailButton setTag:tag]; – amar

+0

它的工作非常感謝你! –

1

使用此我希望這將解決你的目的。

在視圖控制器創建一個按鈕按下方法

  • (IBAction爲)detailButton:(的UIButton *)發件人{

    CustomUITableViewCellName *細胞=(CustomUITableViewCellName *)[[發件人上海華]上海華] ; NSIndexPath * cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];

}

對於IOS 7添加另一個上海華。

現在您可以通過這種方式獲得您的IndexPath,並通過使用 indexPath.row或indexPath.section獲取當前行。

您可以使用sender.title屬性獲取標題。

你可以在你的數組中使用這個indexPath來獲得所需的細節。

相關問題