2012-08-13 48 views
8

我在UITableViewCells中添加了UIButton。我有當用戶點擊按鈕我們得到的indexpath使用NSMutableArray的值。我已經使用了下面來得到當前IndexPath如何通過iOS中的參數將UITableView IndexPath傳遞給UIButton @selector?

[getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside]; 

-(void) resendTheErrorMessage:(id)sender 
{ 
    NSLog(@"SEnder: %@", sender); 
    //NSLog(@"Index Path : %@", indexpath); 
} 

誰能幫我傳遞電流indexpath UIButton's @選擇。提前致謝。

編輯:

這是我從NSLog()

<UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>> 
+0

您可以根據您的單元格的行數爲您的按鈕設置標籤,然後您可以輕鬆獲取cellIndex。顯示你在單元格(cellForRowAtIndexPath :)方法中添加按鈕的代碼? – TheTiger 2012-08-13 14:16:53

+0

@VakulSaini謝謝。我沒有從(id)發件人獲取cellIndex。我已編輯我的問題,請查看此。謝謝。 – 2012-08-13 14:20:18

+0

你正在得到這個,因爲你正在打印'UIButton'的id。 看我的帖子得到cellIndex – TheTiger 2012-08-13 14:26:59

回答

30

添加您UIButton像這樣

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[btn setFrame:CGRectMake(10.0, 2.0, 140.0, 40.0)]; 
[btn setTitle:@"ButtonTitle" forState:UIControlStateNormal]; 
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
[btn setTag:indexPath.row]; 
[cell.contentView addSubview:btn]; 

,然後獲取其標籤號 -

-(void)buttonClicked:(id)sender 
{ 
    NSLog(@"tag number is = %d",[sender tag]); 
    //In this case the tag number of button will be same as your cellIndex. 
    // You can make your cell from this. 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0]; 
    UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath]; 
} 

注:上述解決方案,當你的tableView只有1節會工作。如果你的tableView有多個部分,你應該知道你的部分索引或去下面的方法。

備選:1

UIView *contentView = (UIView *)[sender superview]; 
UITableViewCell *cell = (UITableViewCell *)[contentView superview]; 
NSIndexPath *indexPath = [tblView indexPathForCell:cell]; 

備選:2

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView]; 
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint]; 
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath]; 
+0

當TableView有多個部分時,這是否工作? – 2013-05-01 18:13:49

+1

第一個不會工作,因爲在這你必須知道段索引....但替代解決方案將工作...... – TheTiger 2013-05-06 04:22:43

+0

@ TheTiger感謝男人它完全工作... – Ryuk 2015-02-02 13:05:26

2

得到輸出。如果你只需要在錶行,那麼你可以設置按鈕的標籤屬性的行號。如果您需要整個路徑(包含節號),那麼您必須繼承UIButton並創建一個新屬性(「indexPath」將是一個好名字),您將按鈕添加到表格行時將設置該屬性。

+0

我不認爲這會奏效。 AFAIK你不能傳遞你自己的參數:@selector? – 2013-05-01 17:54:31

+0

哪部分不起作用?不需要額外的參數;其想法是在按鈕對象本身添加到表中時,通過標籤屬性或子類的自定義屬性將索引信息編碼。標籤方法是這個問題被接受的答案中使用的。 – sgress454 2013-05-01 20:56:35

6

鑑於按鈕是細胞視圖的子視圖中,並且該表視圖知道索引單元格視圖的路徑,類似這樣的工作:

- (UITableViewCell *)containingCellForView:(UIView *)view 
{ 
    if (!view.superview) 
     return nil; 

    if ([view.superview isKindOfClass:[UITableViewCell class]]) 
     return (UITableViewCell *)view.superview; 

    return [self containingCellForView:view.superview]; 
} 

- (IBAction)buttonClicked:(id)sender 
{ 
    UITableViewCell *containingCell = [self containingCellForView:sender]; 
    if (containingCell) { 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:containingCell]; 
     NSLog(@"Section: %i Row: %i", indexPath.section, indexPath.row); 
    } 
} 
+0

這將工作,但你有以確保該按鈕是表格單元格的一個* direct *子視圖(它通常會是)。 – sgress454 2012-08-13 15:53:32

+0

並添加subView直接cell.view不好...!我們將它添加到cell.contentView ....好主意很好:-) – TheTiger 2012-08-14 06:51:56

+0

如果將它設置爲單元格的輔助視圖,原始問題沒有指定它的添加位置,這很好。顯然,如果在視圖層次結構中有n個關卡,那麼您必須備份n個關卡才能進入單元格視圖。 – 2012-08-14 15:37:51

0

對我的作品如果附加的標籤在的cellForRowAtIndexPath按鈕...

cell.button.tag = indexPath.row; 

,並在細胞類獲得標籤...例如:

NSLog(@"tag number is = %d",[sender tag]); 

白衣,你可以知道cell.row。 好,這對我有用,對不起,我的英語。

1

對於多於一個路段的實現代碼如下一個簡單的解決辦法是分配標籤:

cell.button.tag = indexPath.section * 1000 + indexPath.row 

在事件處理程序使用方法:

let section = sender.tag/1000 
let row = sender.tag % 1000 

請注意,你需要一個乘以如果您的任何部分可以包含1000或更多行,則數量大於1000。

相關問題