从详细视图控制器向我的日程表的 swift 数组添加行将不起作用
Adding rows to a swift array of my Schedule from a Detail View Controller won't work
我有一个事件列表,其中每个事件都通过来自核心数据模型的 object 将数据传递给详细信息 VC。例如,Event
可以有标题、日期、房间、收藏夹等。现在,从 DetailVC
开始,我有一个 "Add to my Schedule" 按钮让用户 select他们最喜欢参加的活动。单击后,我想将此 object 保存到数组中。
我得到这个错误1:
Cannot invoke append with an argument list of type String.
详细视图控制器:
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var managedObjectContext: NSManagedObjectContext? = nil
// @IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
//create a new array to hold on favorite objects (sessions add it to My Schedule)
var favesArray = [Event]()
var detailItem: Event!
这是详细视图控制器cellForRowAtIndexPath
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == Sections.info && indexPath.row == 1 {
let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("faveCell", forIndexPath: indexPath) as! TitleCell
if detailItem.isFavorite.boolValue {
cell.valueFaveButton.setTitle("Remove from My Schedule", forState: .Normal)
} else {
cell.valueFaveButton.setTitle("Add to My Schedule", forState: .Normal)
}
cell.valueFaveButton.addTarget(self, action: "myScheduleButton:", forControlEvents: .TouchUpInside)
return cell
} else if indexPath.section == Sections.info && indexPath.row == 0 {
let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("TitleCell", forIndexPath: indexPath) as! TitleCell
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm"
var dateString = dateFormatter.stringFromDate(detailItem!.date)
cell.titleLabel.text = detailItem?.title
cell.fieldLabel.text = dateString
return cell
} else if indexPath.section == Sections.info && indexPath.row == 2 {
let cell: RemoveFaveCell = tableView.dequeueReusableCellWithIdentifier("removeFaveCell", forIndexPath: indexPath) as! RemoveFaveCell
cell.removeFaveButton.addTarget(self, action: "removeFavoriteButton:", forControlEvents: .TouchUpInside)
return cell
} else if indexPath.section == Sections.description {
let cell: LabelCell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath) as! LabelCell
cell.labelDescriptionField.text = "Swift is the new language from Apple."
return cell
} else {
assertionFailure("Unhandled session table view section")
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
return cell
}
}
func myScheduleButton(sender: UIButton) {
detailItem.isFavorite = !detailItem.isFavorite.boolValue
tableView.reloadSections(NSIndexSet(index: Sections.info), withRowAnimation: .Automatic)
//now add this clicked/save object(event row) over the faves array
//1 yellow warning in next line:Conditional Cast from Event to Event always succeeds. But it compiles fine.
if let faveSession:Event = detailItem as? Event {
self.favesArray.append(faveSession)
}
}
还想不通。这是处理核心数据 object 以便稍后保存为新数组的有效方法吗?在我的例子中,这将在按下名为“我的日程安排”的分段按钮时显示。谢谢你的回答。
Event.swift
class Event: NSManagedObject {
@NSManaged var date: NSDate
@NSManaged var isFavorite: NSNumber
@NSManaged var timeStamp: NSDate
@NSManaged var title: String
@NSManaged var classSelected: String
@NSManaged var desc: String
}
显示我的日程安排(favesArray objects select 详细VC)
@IBAction func FilterHSClassesByPeriod(sender: UISegmentedControl) {
let classSelected: String?
if sender.selectedSegmentIndex == 0 {
classSelected = "Period 1"
}else {
classSelected = "My Schedule"
}
let filterPredicate = NSPredicate(format: "classSelected = %@", classSelected!)
var request: NSFetchRequest = self.fetchedResultsController.fetchRequest
request.predicate = filterPredicate
var e: NSError? = nil
self.fetchedResultsController.performFetch(&e)
self.tableView.reloadData()
}
您将 favesArray 定义为事件数组:
var favesArray = [Event]()
但是您正试图附加一个字符串
if let faveSession:String = detailItem.title as? String {
self.favesArray.append(faveSession)
}
如果您希望能够附加事件和字符串,您需要将声明更改为:
var favesArray = [AnyObject]()
或者(如果可能)将您的 faveSession 转换为 Event 而不是 String,但您可以决定是否可行
if let faveSession:Event = detailItem.title as? Event {
self.favesArray.append(faveSession)
}
我有一个事件列表,其中每个事件都通过来自核心数据模型的 object 将数据传递给详细信息 VC。例如,Event
可以有标题、日期、房间、收藏夹等。现在,从 DetailVC
开始,我有一个 "Add to my Schedule" 按钮让用户 select他们最喜欢参加的活动。单击后,我想将此 object 保存到数组中。
我得到这个错误1:
Cannot invoke append with an argument list of type String.
详细视图控制器:
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var managedObjectContext: NSManagedObjectContext? = nil
// @IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
//create a new array to hold on favorite objects (sessions add it to My Schedule)
var favesArray = [Event]()
var detailItem: Event!
这是详细视图控制器cellForRowAtIndexPath
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == Sections.info && indexPath.row == 1 {
let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("faveCell", forIndexPath: indexPath) as! TitleCell
if detailItem.isFavorite.boolValue {
cell.valueFaveButton.setTitle("Remove from My Schedule", forState: .Normal)
} else {
cell.valueFaveButton.setTitle("Add to My Schedule", forState: .Normal)
}
cell.valueFaveButton.addTarget(self, action: "myScheduleButton:", forControlEvents: .TouchUpInside)
return cell
} else if indexPath.section == Sections.info && indexPath.row == 0 {
let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("TitleCell", forIndexPath: indexPath) as! TitleCell
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm"
var dateString = dateFormatter.stringFromDate(detailItem!.date)
cell.titleLabel.text = detailItem?.title
cell.fieldLabel.text = dateString
return cell
} else if indexPath.section == Sections.info && indexPath.row == 2 {
let cell: RemoveFaveCell = tableView.dequeueReusableCellWithIdentifier("removeFaveCell", forIndexPath: indexPath) as! RemoveFaveCell
cell.removeFaveButton.addTarget(self, action: "removeFavoriteButton:", forControlEvents: .TouchUpInside)
return cell
} else if indexPath.section == Sections.description {
let cell: LabelCell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath) as! LabelCell
cell.labelDescriptionField.text = "Swift is the new language from Apple."
return cell
} else {
assertionFailure("Unhandled session table view section")
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
return cell
}
}
func myScheduleButton(sender: UIButton) {
detailItem.isFavorite = !detailItem.isFavorite.boolValue
tableView.reloadSections(NSIndexSet(index: Sections.info), withRowAnimation: .Automatic)
//now add this clicked/save object(event row) over the faves array
//1 yellow warning in next line:Conditional Cast from Event to Event always succeeds. But it compiles fine.
if let faveSession:Event = detailItem as? Event {
self.favesArray.append(faveSession)
}
}
还想不通。这是处理核心数据 object 以便稍后保存为新数组的有效方法吗?在我的例子中,这将在按下名为“我的日程安排”的分段按钮时显示。谢谢你的回答。
Event.swift
class Event: NSManagedObject {
@NSManaged var date: NSDate
@NSManaged var isFavorite: NSNumber
@NSManaged var timeStamp: NSDate
@NSManaged var title: String
@NSManaged var classSelected: String
@NSManaged var desc: String
}
显示我的日程安排(favesArray objects select 详细VC)
@IBAction func FilterHSClassesByPeriod(sender: UISegmentedControl) {
let classSelected: String?
if sender.selectedSegmentIndex == 0 {
classSelected = "Period 1"
}else {
classSelected = "My Schedule"
}
let filterPredicate = NSPredicate(format: "classSelected = %@", classSelected!)
var request: NSFetchRequest = self.fetchedResultsController.fetchRequest
request.predicate = filterPredicate
var e: NSError? = nil
self.fetchedResultsController.performFetch(&e)
self.tableView.reloadData()
}
您将 favesArray 定义为事件数组:
var favesArray = [Event]()
但是您正试图附加一个字符串
if let faveSession:String = detailItem.title as? String {
self.favesArray.append(faveSession)
}
如果您希望能够附加事件和字符串,您需要将声明更改为:
var favesArray = [AnyObject]()
或者(如果可能)将您的 faveSession 转换为 Event 而不是 String,但您可以决定是否可行
if let faveSession:Event = detailItem.title as? Event {
self.favesArray.append(faveSession)
}