2014-03-27 16 views
1

目的:情況時,連續選擇無論何時添加自定義按鈕

  • 添加自定義按鈕標題Delete在一排時,它選擇
  • 每當小區選擇的變化等將其刪除添加'刪除'到最後的選擇。

    (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
        self.myIndexPath=indexPath; 
        UIButton *btnCustomDelete=[[UIButton alloc] initWithFrame:CGRectMake(260, 10, 60, 7)]; 
        [btnCustomDelete setTitle:@"Delete" forState:UIControlStateNormal]; 
        [tblCellForContactTable.contentView addSubview: btnCustomDelete]; //I think correction wants here 
    
        [btnCustomDelete addTarget:self action:@selector(actionCustomDelete:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    
    -(IBAction) actionCustomDelete:(id)sender{ 
        [arrForMyContacts removeObject:[arrForMyContacts objectAtIndex:myIndexPath.row]]; 
        [tblForContacts reloadData]; 
    } 
    

但是,它不是所有的工作時間。

+0

如果我選擇小區A,會發生什麼,然後細胞B(因而self.myIndexPath =索引:您可以實施一些UITableView數據源和委託方法,而無需添加自定義按鈕,如下圖所示輕鬆擁有刪除操作單元格B的路徑),然後點擊單元格A的刪除按鈕?它會刪除單元格B. – Cyrille

+0

你問問題或解決我的解決方案。請清除我。我英語不好。 –

+0

我正在提出實施的潛在問題。如果按照我寫的順序進行操作,您的應用將會出現錯誤的行爲。 – Cyrille

回答

1

你說得對。您應該將按鈕作爲子視圖添加到實際的UITableViewCell對象中,您可以使用tableView:cellForRowAtIndexPath:數據源方法實現該對象。

所以,你的實現可以像(創建後您的btnCustomDelete):

UITableViewCell * myCell = [tableView cellForRowAtIndexPath:indexPath] 
[myCell.contentView addSubview:btnCustomDelete]; 

請繼續閱讀。

您的實施方案不是從表中刪除行的健康解決方案。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [arrForMyContacts removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 
+0

它的工作。它是如何工作的。你可以解釋嗎? –

+0

哪一個?您的自定義按鈕或另一個? – ismailgulek

+0

你的'btnCustomDelete'實現的解決方案... –

相關問題