0
我有一個事件列表,其中每個事件都將數據傳遞給具有來自核心數據模型的對象的詳細VC。例如,一個Event
可以有一個標題,日期,房間,isFavorite等。現在,從DetailVC
,我有一個「添加到我的日程安排」按鈕,讓用戶選擇他們最喜歡的活動參加。一旦點擊了,我想將這個對象保存到一個數組中。將行添加到Detail視圖控制器的Swift數組中將不起作用
我得到這個ERROR1:
不能調用類型爲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)
}
}
想不通這一點呢。這是否是一種有效的方法來與核心數據對象一起使用,以便作爲新數組保存起來?在我的情況下,當按下名爲「我的計劃」的分段按鈕時,會顯示此信息。謝謝您的回答。
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對象detailVC選擇)
@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()
}
對,現在我正確地將它轉換爲Event,但是我得到警告並且構建失敗。查看更改的代碼。 – serg
就像我說的你可以施放「如果可能」,你的變量事件似乎不能被當作一個字符串。你能發佈事件聲明嗎? – Icaro
我現在正在發佈它。現在,它似乎讓我把它作爲事件,在一小時後通過改變先前代碼的第一個聲明。現在,一旦我有這個收藏夾對象的數組,如何在屬於主VC的cellForRowAtIndexPath方法中調用favesArray?因爲這個數組是在detailVC中聲明的。我是否需要用類函數創建一個Swift類?查看代碼更新。 – serg