2013-03-24 33 views
0

我已經實現了這個按鈕抽屜,我已經添加了一些按鈕。也就是說,我不確定如何將消息從抽屜中的這些按鈕發送到適當的委託方法以從我的tableView中刪除項目。我有一個UITableViewCell中的按鈕的抽屜。如何獲得單元格的indexPath並將其刪除?

如何獲得正確的indexPath?我是否應該爲已將其檢查按鈕切換的uitableviewcell創建新的NSIndex?

感謝

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    NSInteger directionMask = indexPath.row % 5; 
    if (cell == nil) { 
     cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 




    UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame]; 

    drawerView.backgroundColor = [UIColor grayColor]; 
    cell.drawerView = drawerView; 
     UIImage *checkImage = [UIImage imageNamed:@"check.png"]; 
     UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [checkButton setImage:checkImage forState:UIControlStateNormal]; 
     cell.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height); 
     checkButton.frame = CGRectMake(10, 10, checkImage.size.width, checkImage.size.height); 
     [drawerView addSubview:checkButton]; 

     [checkButton addTarget:nil action:@selector(onCheckMarkTap:) forControlEvents:UIControlEventTouchUpInside]; 

    } 

- (void)onCheckMarkTap { 
    NSLog(@"Delete the cell"); 

} 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     } 

} 
+1

我已經回答了這個類似的問題:http://stackoverflow.com/questions/7504421/getting-row-of-uitableview-cell-on-button-press/7505025#7505025 – user523234 2013-03-24 02:51:25

回答

1

此問題已經has an answer。您可以使用目標/動作與事件或通過手勢識別器獲取在表格視圖的座標系中點擊的按鈕的座標,然後使用UITableView的方法-indexPathForRowAtPoint:獲取包含此按鈕的單元格的索引路徑。

0

cellForRowAtIndexPath:

checkButton.tag = indexPath.row 

也在裏面onCheckMarkTap,這樣做:

- (void)onCheckMarkTap : (id) sender 
{ 
    if (sender.tag == 0) 
    ///code 
    else if (sender.tag == 1) 
    ///code 

} 
-2

我處理我自己的代碼這樣的問題的方法是繼承「 UIButton「,其中我加入了」NSIndexPath「或」tableRow「ivar進入子類按鈕。我們的名字是「StangButton」。

通過子類別按鈕,您可以在通過「cellForRowAtIndexPath:」組合單元格時填充「indexPath」ivar。

然後,按下按鈕時,你就會知道哪個按鈕被按下,這是在哪一行。

也可以檢查,看看是否"tableView:didSelectRowAtIndexPath:"表視圖的委託方法被調用時,該按鈕在單元格中觸摸。

+0

我想我遵循,但是,你不會碰巧有一個例子或者一個跟隨這種方法的例子嗎? – STANGMMX 2013-03-24 01:07:14

相關問題