[英][__NSArrayI pickerView:numberOfRowsInComponent:]: unrecognized selector sent to instance
[英][__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance
我正在嘗試刪除特定索引處的行,但出現此錯誤。 因為它顯示了 NSArray,但我使用的是 NSMutableArray。 不知何故,它是強制類型轉換的。
這兩個數組是全局聲明的。
NSArray *contacts;
NSMutableArray *sortedContacts;
在 cellForRowAtIndexPath 方法中,我復制 NSArray 中的 NSMutableArray 以顯示行中的數據,否則直接使用 NSMutableArray,鍵值丟失。
contacts = [sortedContacts copy];
這是我在行上的按鈕,按下它后,我想刪除所有包含數據的行。
-(void)CancelButtonClicked:(UIButton*)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[sortedContacts removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
這只是簡單的解決方案如果您想刪除數組索引的第一個對象,請添加這行代碼。
data = [[dicsResponse valueForKey:@"data"]mutableCopy];
[data removeObjectAtIndex:0];
我找到了這個解決方案。 在 NSMutableArray 中復制 sortedconacts。 刪除對象后,將修改后的數組復制到原始 sortedcontacts 數組中。 :)
-(void)CancelButtonClicked:(UIButton*)sender
{
NSMutableArray *copy=[sortedContacts mutableCopy];
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[copy removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
sortedcontacts=[copy mutableCopy];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.