2013-03-22 61 views
-1

我想從數組中刪除一個對象,當我的表視圖獲取reload,從tableview中顯示的數組中刪除對象時,我正在使用– removeObjectAtIndex:從數組中刪除對象。 請幫我從數組永久刪除對象。提前感謝。如何從iphone中的數組中刪除一個對象

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 

     [myArray removeObjectAtIndex:indexPath.row]; 

     [tableView reloadData]; 

    } 

} 
+1

添加一些代碼... – Bhavin 2013-03-22 12:39:03

+1

你是否重新加載你的tableview之前或之後,你從數組中刪除對象? – tttthomasssss 2013-03-22 12:39:23

+0

'removeObjectAtIndex'應該可以工作。你確定你要發送正確的索引嗎?數組中的第一項是C中的索引0,與某些語言不同。 – GeneralMike 2013-03-22 12:40:21

回答

4

NSMutableArray的方法刪除對象:

– removeAllObjects 
– removeLastObject 
– removeObject: 
– removeObject:inRange: 
– removeObjectAtIndex: 
– removeObjectsAtIndexes: 
– removeObjectIdenticalTo: 
– removeObjectIdenticalTo:inRange: 
– removeObjectsFromIndices:numIndices: 
– removeObjectsInArray: 
– removeObjectsInRange: 

參見:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablearray_Class/Reference/Reference.html

,並且確保你刷新你的表格你從數組刪除對象後。並將適當的計數發送給tableview委託。

1

取一個NSMutableArray,然後刪除tableview行數的對象被選中。然後重新加載tableview來應用更改。

例如....

[yourArray removeObjectAtIndex:indexPath.row]; 
[tableView reloadData]; 

確保你重新加載tableview中,並採取不NSMutableArray的NSArray的

0

我想你刪除從臨時數組對象,所以它不會從主刪除array.so當你重新加載或viewwillAppear,它正在複製..