我是我的应用程序中的核心数据的新手我正在使用coredata。 我只是将数据存储在我的核心数据中。我的实体名称是“FEED”,我有一个名为“title”,“id”,“link”,“desc”的行,所以现在我想基于“id”删除特定行。那怎么能这样呢?
这是我的代码,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell
cell.pinButton.tag = indexPath.row
cell.pinButton.addTarget(self, action: #selector(MessageViewController.buttonDeletePressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
cell.selectionStyle = .None
person = people[indexPath.row]
cell.lbl_title_msg_music.text = person!.valueForKey("title") as? String
cell.img_message_music.image = UIImage(named: "PlaceHolder")
cell.desc_msg_music.text = person!.valueForKey("desc") as? String
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(people.count)
return people.count
}
func buttonDeletePressed(sender:UIButton) {
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell
}
所以我该怎么做?
答案 0 :(得分:6)
尝试这样,
func buttonDeletePressed(sender:UIButton) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let index = sender.tag
context.deleteObject(people[index] as NSManagedObject)
people.removeAtIndex(index)
let _ : NSError! = nil
do {
try context.save()
self.tableView.reloadData()
} catch {
print("error : \(error)")
}
}
希望这会对你有所帮助。
答案 1 :(得分:1)
$(document).ready(function(){
$('#inpImage').change(function(){
readURL(this, drawImage);
});
function drawImage(img) {
var canvas = document.getElementById("myCanvas");
var cContext = canvas.getContext('2d');
var size = scaleSize(400, 600, img.width, img.height);
var width = size[0];
var height = size[1];
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
cContext.beginPath();
cContext.rect(0, 0, width, height);
cContext.fillStyle = "red";
cContext.fill();
cContext.drawImage(img, 0, 0, width, height);
};
});
function readURL(input, drawImage) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = new Image();
img.onload = function(){
drawImage(img);
};
img.src = e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
}
function scaleSize(maxW, maxH, currW, currH) {
var ratio = currH / currW;
if (currW >= maxW && ratio <= 1) {
currW = maxW;
currH = currW * ratio;
} else if (currH >= maxH) {
currH = maxH;
currW = currH / ratio;
}
return [currW, currH];
}
现在你可以调用这个函数并传递你想要删除的id
func deleteFeed(id:String)
{
let appDelegate =
UIApplication.sharedApplication().delegate as? AppDelegate
let managedContext = appDelegate?.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"FEED")
fetchRequest.predicate = NSPredicate(format: "id = %@", "\(id)")
do
{
let fetchedResults = try managedContext!.executeFetchRequest(fetchRequest) as? [NSManagedObject]
for entity in fetchedResults! {
managedContext?.deleteObject(entity)
}
}
catch _ {
print("Could not delete")
}
}
答案 2 :(得分:0)
您可以删除
等数据 let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
context.deleteObject(myData[indexPath.row] as NSManagedObject)
myData.removeAtIndex(indexPath.row)
context.save(nil)
这是从commitEditingStyle
完成的,所以这里使用了indexPath.row
。如果要从其他位置删除数据,可以传递您的ID。如果要从tableview中删除数据,则应实现commitEditingStyle
并删除上述数据。
希望这会有所帮助:)
答案 3 :(得分:0)
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
switch editingStyle {
case .Delete:
// remove the deleted item from the model
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
context.deleteObject(people[indexPath.row] )
people.removeAtIndex(indexPath.row)
do {
try context.save()
} catch _ {
}
// remove the deleted item from the `UITableView`
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
return
}
}
希望这可能有所帮助!