我正在尝试使用核心数据并按顺序和部分加载 table 视图及其内容
I am trying to use Core Data and load a table view with its contents in order and in sections
我可以加载它,我可以加载部分,但我无法正确加载每个部分中的单元格。它从每个部分的开头重新开始,有效地复制每个单元格。我的实体名称是 Customer,它具有以下属性。 firstName、lastName、phoneNumber、email、notes 和 first。 First 是用于各部分的姓氏中的第一个字母。我可以加载它,headers 加载成功但是一旦我开始添加姓氏中首字母相同的名字,它就开始搞砸了。如果您需要更多代码,请告诉我。
override func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath?) -> UITableViewCell
{
//getSections()
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!)
let cust = customers[(indexPath?.section)!]
print(indexPath?.row)
let fName = cust.valueForKey("firstName") as? String
print(fName)
let lName = cust.valueForKey("lastName") as? String
print(lName)
cell.textLabel!.text = fName! + " " + lName!
print("Cell: \(cell.textLabel!.text)")
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
context = managedContext
//2
let fetchRequest = NSFetchRequest(entityName:"Customer")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastName", ascending: true)]
//3
//var error: NSError?
let fetchedResults = try!
managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
if var results = fetchedResults
{
customers = results
}
else
{
print("Could not fetch")
}
tableView.reloadData()
getSections()
}
func getSections()
{
var sections:[String] = []
for(var i=0;i<customers.count;i++)
{
let cust = customers[i]
sections.append(cust.valueForKey("first") as! String)
}
sectionHeadersTotal = sections
let unique = Array(Set(sections))
sectionHeaders = unique.sort()
print(sectionHeaders.count)
//sectionHeaders = unique
//return unique.count
}
在此处查看读取分段数据的方法 -
http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/
和这里
iOS UITableView sections with fetchedResultsController confusion
我可以加载它,我可以加载部分,但我无法正确加载每个部分中的单元格。它从每个部分的开头重新开始,有效地复制每个单元格。我的实体名称是 Customer,它具有以下属性。 firstName、lastName、phoneNumber、email、notes 和 first。 First 是用于各部分的姓氏中的第一个字母。我可以加载它,headers 加载成功但是一旦我开始添加姓氏中首字母相同的名字,它就开始搞砸了。如果您需要更多代码,请告诉我。
override func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath?) -> UITableViewCell
{
//getSections()
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!)
let cust = customers[(indexPath?.section)!]
print(indexPath?.row)
let fName = cust.valueForKey("firstName") as? String
print(fName)
let lName = cust.valueForKey("lastName") as? String
print(lName)
cell.textLabel!.text = fName! + " " + lName!
print("Cell: \(cell.textLabel!.text)")
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
context = managedContext
//2
let fetchRequest = NSFetchRequest(entityName:"Customer")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastName", ascending: true)]
//3
//var error: NSError?
let fetchedResults = try!
managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
if var results = fetchedResults
{
customers = results
}
else
{
print("Could not fetch")
}
tableView.reloadData()
getSections()
}
func getSections()
{
var sections:[String] = []
for(var i=0;i<customers.count;i++)
{
let cust = customers[i]
sections.append(cust.valueForKey("first") as! String)
}
sectionHeadersTotal = sections
let unique = Array(Set(sections))
sectionHeaders = unique.sort()
print(sectionHeaders.count)
//sectionHeaders = unique
//return unique.count
}
在此处查看读取分段数据的方法 -
http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/ 和这里 iOS UITableView sections with fetchedResultsController confusion