2012-05-19 50 views
4

我創建了一個包含2個部分的表格,我希望應用程序在按下編輯按鈕時只顯示第一部分中的刪除按鈕而不是第二部分,我寫了如下代碼如何刪除只有一個部分中的行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (indexPath.section == 0) { 
Book_own *temp= (Book_own *)[self.books objectAtIndex:indexPath.row]; 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 

    [books removeObjectAtIndex:indexPath.row]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

} 

else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 
} 
} 

但是當我按Edit按鈕時,它顯示兩個部分中的刪除按鈕,我怎樣才能防止這種情況,並在第一部分進行刪除???

回答

1

試試這個...

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

     if(indexPath.section == yourVariable) return UITableViewCellEditingStyleDelete; 

     else return UITableViewCellEditingStyleNone; 

    } 
+0

感謝您的幫助 –

+0

難道這允許我做(didSelectRowAtIndexPath方法)? –

+0

是的,你只能玩UITableViewCellEditingStyleNone;這意味着只在編輯樣式,但你的意思是UITableViewSelectionStyleNone我們沒有改變任何東西在它,這是負責射擊didselectrowatindexpath –

1

'UITableViewCellEditingStyleNone'可以爲您已經實現的委託中的特定部分設置。只需返回即可。

你可以用下面的代碼實現它: - 在的cellForRowAtIndexPath

: -

if(indexPath.section==0) 
{ 
cell.editing=NO; 
} 
+0

我試過,但它仍然顯示的刪除按鈕,但感謝你的回答 –

1

dataSource可以實現tableView:canEditRowAtIndexPath:返回YES或NO。

1

您可以檢查該特定部分: 如果(indexPath.section == Editsection的){

回報UITableViewCellEditingStyleDelete;

}

else{ 

回報UITableViewCellEditingStyleNone;

}

相關問題