Swift - 将核心数据检索到自定义单元格

时间:2014-12-29 11:40:52

标签: ios uitableview core-data swift

我试图检索核心数据并将其显示在自定义单元格类中。我认为如果我首先提出我的代码会更容易。

这是我的原始代码",带有常规单元格:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellID:NSString = "cell"

    var cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell

    if let ip = indexPath as Optional {

        var data:NSManagedObject = myList[ip.row] as NSManagedObject

        cell.textLabel!.text = data.valueForKeyPath("username") as String!
    }

    return cell
}

这是我在尝试使用自定义单元格时更改代码的原因:

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let CellID:NSString = "cell"

        var cell: CustomCell = tv.dequeueReusableCellWithIdentifier(CellID) as CustomCell

        if let ip = indexPath as Optional {

            var data:NSManagedObject = myList[ip.row] as NSManagedObject

            cell.titleLabel.text = data.valueForKeyPath("username") as String!
            cell.dateLabel.text = data.valueForKeyPath("date") as String!
        }

        return cell
    }

第一个代码完美运行,但是当使用第二个代码时,我得到(lldb)运行时错误。

两者"用户名"和"日期"保存为字符串。

任何建议都将不胜感激。

编辑:

其他信息:

var myList: Array<AnyObject> = []

弹出的错误只是&#34;(lldb)&#34;和&#34;线程1:EXC_BREAKPOINT(代码= EXC_l386_BPT,子代码= 0x0)&#34;。

我的模型文件:

@objc(Model)
class Model: NSManagedObject {

    @NSManaged var username: String
    @NSManaged var date: String
    @NSManaged var isAnonymousMessage: Bool

}

我的cellForRowAtIndexPath-function:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellID:NSString = "cell"

    var cell: CustomCell = tv.dequeueReusableCellWithIdentifier(CellID) as CustomCell

    if let ip = indexPath as Optional {

        let data = myList[indexPath.row] as Model

        cell.titleLabel.text = data.username
        cell.dateLabel.text = data.date
    }


    return cell
}

我的viewDidAppear-function:

override func viewDidAppear(animated: Bool) {

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!
    let freq = NSFetchRequest(entityName: "Message")
    let en = NSEntityDescription.entityForName("Message", inManagedObjectContext: context)

    let fetchRequest = NSFetchRequest(entityName: "Message")

    myList = context.executeFetchRequest(fetchRequest, error: nil) as [Model]


    tv.reloadData()

}

我的CustomCell类看起来像这样:

import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!

    @IBOutlet weak var dateLabel: UILabel!



    override func awakeFromNib() {
        super.awakeFromNib()



        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

2 个答案:

答案 0 :(得分:2)

我建议稍微改变你的代码。

将NSManagedObjects加载到Core Data Class的数组中,例如:

 var myList = [ListObject]() // Where ListObject is your NSManagedClass

 let fetchRequest = NSFetchRequest(entityName: "List")
 myList = context.executeFetchRequest(fetchRequest, error: nil) as [ListObject]

// You should load this once (maybe in ViewDidLoad) so every Core Data object gets only fetched   once (you could easy refresh this if needed).

然后在你的cellForRowAtIndexPath:

中使用
let data = myList[indexPath.row] as ListObject

cell.titleLabel.text = data.name
cell.dateLabel.text = data.date

// You dont need to use "valueForKeyPath" - just use the property as shown above.

答案 1 :(得分:2)

var cell: CustomCell = tv.dequeueReusableCellWithIdentifier(CellID) as CustomCell

这一行崩溃意味着当你从桌面视图中出列时,你没有得到CustomCell

您需要使用该重用标识符注册该类,方法是在storyboard或xib中设置它,或者在表视图上调用registerClass(_ cellClass: AnyClass, forCellReuseIdentifier identifier: String),通常在视图中加载。

如果您已在故事板中添加了新单元格并想要使用它而不是默认单元格,请确保正确设置重用标识符。