2013-04-26 180 views
9

我有一個UITableview與多個單元格。在每個單元格上,我會根據某些數組動態地添加按鈕。所以,當我點擊一個按鈕時,我可以得到該按鈕的值爲tag。但是如何獲得該細胞的indexPath如何在UITableViewCell中獲取UIButton的indexPath?

這是我在-cellForRowAtIndexPath代碼:

UIView *view=(UIView*)[cell.contentView viewWithTag:indexPath.row+444]; 

    UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:indexPath.row+999]; 
    img.image=[UIImage imageNamed:@"BHCS_empty.png"]; 

    if(integer!=50) 
    { 
     NSInteger y_axis=0; 
     NSArray *Arr=[tableSubCategoryArr objectAtIndex:indexPath.row]; 
     img.image=[UIImage imageNamed:@"BHCS_selected.png"]; 

     view.Frame= CGRectMake(0,50,281, integer-50); 
     for (int i=0; i<[Arr count]; i++) 
     { 
      NSLog(@"arr %@",[Arr objectAtIndex:i]); 
      UIButton *Btn=[UIButton buttonWithType: UIButtonTypeCustom]; 
      Btn.frame=CGRectMake(0, y_axis, 281, 44); 
      [Btn setImage:[UIImage imageNamed:@"BHCS_panel.png"] forState:UIControlStateNormal]; 
      [Btn addTarget:self action:@selector(subCategoryBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; 
      [Btn setTag:i+100]; 
      [view addSubview:Btn]; 


      UILabel *nameLbl=[[UILabel alloc]initWithFrame:CGRectMake(20, y_axis,248, 44)]; 
      nameLbl.text = [[Arr objectAtIndex:i]objectForKey:@"SubCategoryName"]; 
      nameLbl.textColor = [UIColor whiteColor]; 
      nameLbl.backgroundColor=[UIColor clearColor]; 
      panelTableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BHCS_panel_div1.png"]]; 
      nameLbl.font = [UIFont fontWithName:@"Helvetica" size:12.0f]; 
      [view addSubview:nameLbl]; 

      y_axis=y_axis+44+1.3f; 
     } 
    } 
+0

一個簡單的方法是創建一個UITableView擴展。 看到我對[這個問題](https://stackoverflow.com/a/39059239/3395921)的回答,看看它是如何做到的。 – 2016-08-20 22:33:24

回答

6

感謝所有我解決售後服務這個使用下面的代碼

 NSIndexPath *indexPath = [panelTableView indexPathForCell:(UITableViewCell *)sender.superview.superview]; 

NSLog(@"%d",indexPath.row); 
+3

在iOS8 ..這將始終給0.請參閱此.... http://stackoverflow.com/questions/23784630/how-以查找indexpath-for-tap-button-in-tableview -using-seque/27292570#27292570 – 2014-12-04 11:15:58

+0

不要使用此答案。這是錯誤的。永遠不要假設私人視圖層次結構的特定結構。 – rmaddy 2017-08-16 15:21:28

-1

您可以使用UITableViewindexPathForCell:方法,像這樣[tableView indexPathForCell:cell];。祝你好運!

+0

我應該在哪裏使用這條線? – user2230971 2013-04-26 06:12:53

5

更新答案

這樣使用它:

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; 
+0

它崩潰在NSIndexPath * indexPath = [view indexPathForCell:cell]; – user2230971 2013-04-26 06:10:48

+0

你是否將你的按鈕直接添加到單元格中?如果沒有,你需要去superview,直到你到達像button.superview.superview等單元格。調試時請檢查單元的類型,這將有所幫助。 – Amit 2013-04-26 06:31:08

+4

不,不,不!只是不要通過超級視圖循環! http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – CW0007007 2014-01-30 15:24:28

0

將這個代碼在按鈕的操作方法:

NSIndexPath *indexPath = [yourtableviewname indexPathForCell:(UITableViewCell *)sender.superview]; 
    NSLog(@"%d",indexPath.row); 
+0

但每次它在你的代碼中重新調整零索引路徑 – user2230971 2013-04-26 06:18:33

+0

你正在添加按鈕來查看這就是爲什麼它給出0值意味着在這裏sender.superview採取我認爲如此。 – Balu 2013-04-26 06:20:46

+0

嘿,我試過這樣它的工作正常在iOS8中按鈕動作 – user2230971 2013-04-26 06:29:19

9

ALLLLL這裏的答案很糟糕,你不應該通過超級視圖循環。經典示例,使用iOS 7 Apple更改了tableViewCell層次結構,現在應用程序會崩潰!

使用這個代替:

How to know the UITableview row number

+0

哦。這是我幾乎錯過的一個簡短的鏈接答案,但它確實非常好。重點在於該按鈕包含有關屏幕位置的信息,可以輕鬆將其轉換爲索引路徑。 – Suz 2015-07-21 22:01:35

+0

你想認證「壞」嗎? [someView superview]的固定依賴關係可能隨時間而中斷(即使整個視圖層次由應用程序開發人員控制)。但循環直到發現表格視圖單元現在和將來都是安全的。 – danh 2017-01-10 15:47:47

0

我建議增加一個屬性到您的自定義的UITableViewCell實現類存儲indexPath。然後,當您的cellForRowAtIndexPath委託在您的TableViewController中觸發時,請爲該單元格設置indexPath屬性。

customTableViewCell.h:

@interface customTableViewCell : UITableViewCell 

@property NSIndexPath *indexPath; 

@end 

customTableViewController配置單元:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath]; 
    // Do whatever you want with your cell 
    cell.indexPath = indexPath; 

    return cell; 
} 

然後你就可以通過調用self.indexPath

+1

不,不要這樣做。如果表視圖允許刪除,插入或移動行,則此自定義單元格的'indexPath'屬性將會出錯。 – rmaddy 2017-08-16 15:23:27

23

指的是你的customTableViewCell的indexPath財產我都試過最大給出的答案,但在和我通常使用去最通用和理想的方式如下:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; 
+0

超... 在斯威夫特 生命值讓= sender.convert(CGPoint.zero,至:self.tablename) 讓indexpath_select = self.Flight_detail_table.indexPathForRow(在:生命值) 打印(「indexpath:\(字符串(描述:indexpath_select))「) – 2018-01-03 13:14:26

0

這些答案都不是一個非常乾淨的解決方案。我將實現這一點的方式是使用委託模式。視圖控制器是單元格的委託,這意味着你讓單元格自己處理按鈕的按下,並且它告訴它的委託,當按鈕被按下時,它可以處理它,但是它是想要的。

假設您有一個tableview,其中每個單元格表示一個Person對象,並且當按下按鈕時,您想顯示此人的個人資料。所有你需要做的是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath]; 
    cell.delegate = self; 
    cell.person = self.people[indexPath.row]; 
    return cell; 
} 

- (void)personCell:(PersonCell *)personCell didPressButtonForPerson:(Person *)person { 
    [self showProfileForPerson:person]; 
} 

然後,所有你需要在你的按鈕類做的就是添加一個名爲buttonPressedHandler屬性,是塊傳回的Person一個實例,當您創建按鈕,添加目標做這樣的事情:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // do whatever whatever else you need/want to here 

    [self.button addTarget:self selector:@selector(handleButtonPressed) forState:UIControlStateNormal]; 
} 

- (void)handleButtonPressed { 
    // Make sure this is a required delegate method, or you check that your delegate responds to this selector first... 
    [self.delegate personCell:self didPressButtonForPerson:self.person]; 
} 
相關問題