0
我在我的項目中使用核心數據,並在我的一個表格視圖中填充單元格與我有一些對象。無法刪除表格視圖單元格
此表視圖中的第一個單元格是我想根據此對象'狀態'屬性刪除的對象。因此,在另一個視圖控制器中,我將表中的第一個對象更改爲'1',我希望能夠直觀地刪除單元格,但不能刪除核心數據對象。
所以現在我commitEditingStyle方法是這樣的:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
target.status = @1;
CoreDataStack *stack = [CoreDataStack defaultStack];
[[stack managedObjectContext] deleteObject:target] ;
[stack saveContext];
}
if ([_delegate respondsToSelector:@selector(didDeleteObject)]) {
[self fetchData];
[_delegate didDeleteObject];
}
}
目前正如你看到的我是從堆棧中刪除記錄,我雖然我所能做的就是刪除這些行:
CoreDataStack *stack = [CoreDataStack defaultStack];
[[stack managedObjectContext] deleteObject:target] ;
[stack saveContext];
現在我不會刪除該記錄,但該單元格將被刪除。 現在奇怪的事情(對我來說)是當我刪除這些行時,我不刪除記錄,但單元格也沒有被刪除...我不明白爲什麼這些行刪除單元格......不是那些行只刪除一條記錄..?
感謝
didDeleteObject方法:
- (void)didDeleteObject {
self.homeLabel.text = stackTableViewController.currentTarget;
self.homeLabel.frame = homeLabelRect;
for(int i = 40; i>5; i--){
CGRect labelRect = [self.homeLabel.text
boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName : self.homeLabel.font
}
context:nil];
if(labelRect.size.height < self.homeLabel.frame.size.height){
self.homeLabel.font =[UIFont fontWithName:@"Candara-Bold" size:i];
return;
}
}
}
分享'didDeleteObject'方法的代碼。 – Gandalf
@Gandalf完成。 (它在另一個vc,因爲它是一個委託方法) –
好吧,說實話,我希望你用這種方法重新加載表數據。但是,因爲你不那麼沒有幫助。但讓我給你幾點指示: - 1)在你的方法中,你正在刪除記錄,然後你必須在fetchData之後重新加載表數據,這會自動刪除該行,因爲記錄的數量少於一個。這就是爲什麼行被刪除。 __ @ Solution__ - 維護一個本地數組的記錄和刪除單元格時,從數組中刪除該對象並重新加載表。按照此[鏈接](http://www.ioscreator.com/tutorials/delete-rows-from-tableview) – Gandalf