gpt4 book ai didi

ios - UITableview 部分?

转载 作者:行者123 更新时间:2023-11-29 12:08:20 28 4
gpt4 key购买 nike

我想在我的 tableView 中有两个部分。我希望将项目添加到第 0 节,然后能够选择一行以将其从第 0 节移动到第 1 节。到目前为止,我已将这些项目添加到第 0 节,但是当它关​​闭时数据不会加载第二个 View Controller 。我必须更改 View (点击添加按钮和后退按钮),它终于出现了(仍然没有弄清楚)。我知道跨部分移动行必须通过数组和 didSelectRow 方法完成,但它让我陷入循环(尤其是使用 coreData)。我是否为第 1 部分的项目创建第二个数组? TableView 数据从第二个 ViewController 输入,并由实体“SList”的 NSFetchedResultsController 管理。我在“SList”下为“slcross”创建了一个属性(还包括输入“slitem”、“sldesc”、“slqty”和“slprice”,但不知道如何从第 0 节到第 0 节1. 或者,我是否需要为划掉的项目创建第二个实体?抱歉,我有点头大了...

class ShoppingList: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate {

let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var frc : NSFetchedResultsController = NSFetchedResultsController()

func itemFetchRequest() -> NSFetchRequest{

let fetchRequest = NSFetchRequest(entityName: "SList")
let primarySortDescription = NSSortDescriptor(key: "slcross", ascending: true)
let secondarySortDescription = NSSortDescriptor(key: "slitem", ascending: true)
fetchRequest.sortDescriptors = [primarySortDescription, secondarySortDescription]
return fetchRequest
}

func getFetchRequetController() ->NSFetchedResultsController{

frc = NSFetchedResultsController(fetchRequest: itemFetchRequest(), managedObjectContext: moc, sectionNameKeyPath: "slcross", cacheName: nil)
return frc
}

@IBOutlet weak var tableView: UITableView!

@IBAction func AddNew(sender: AnyObject) {

frc = getFetchRequetController()
frc.delegate = self

do {
try frc.performFetch()
} catch _ {
print("Failed to perform inital fetch.")
return
}
self.tableView.reloadData()
}

override func viewDidLoad() {

super.viewDidLoad()


frc = getFetchRequetController()
frc.delegate = self

do {
try frc.performFetch()
} catch _ {
print("Failed to perform inital fetch.")
return
}

self.tableView.reloadData()

//TableView Background Color
self.tableView.backgroundColor = UIColor.clearColor()
self.tableView.separatorColor = UIColor.blackColor()
tableView.reloadData()

override func viewDidDisappear(animated: Bool) {

frc = getFetchRequetController()
frc.delegate = self

do {
try frc.performFetch()
} catch _ {
print("Failed to perform inital fetch.")
return
}
self.tableView.reloadData()
}

//tableView Data
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let managedObject:NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject
moc.deleteObject(managedObject)
do {
try moc.save()
} catch _ {
print("Failed to save.")
return
}
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

let numberOfSections = frc.sections?.count
return numberOfSections!
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
let sectionHeader = "Items - #\(frc.sections![section].numberOfObjects)"
let sectionHeader1 = "Crossed Off Items - #\(frc.sections![section].numberOfObjects)"
if (section == 0) {
return sectionHeader
}
if (section == 1){
return sectionHeader1
}else{
return nil
}
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberOfRowsInSection = frc.sections?[section].numberOfObjects
return numberOfRowsInSection!
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let items = frc.objectAtIndexPath(indexPath) as! SList
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.text = "\(items.slitem!) - Qty: \(items.slqty!)"
cell.textLabel?.font = UIFont.systemFontOfSize(23)


return cell
}
//end tablevViewData*/


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
}

//segue to add/edit
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

if segue.identifier == "edit" {

let cell = sender as! UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
let SListController:SLEdit = segue.destinationViewController as! SLEdit
let items:SList = frc.objectAtIndexPath(indexPath!) as! SList
SListController.item = items
}
}
}

第二个 View Controller

class SLEdit: UIViewController {

let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext


@IBOutlet weak var slitem: UITextField!
@IBOutlet weak var sldesc: UITextField!
@IBOutlet weak var slqty: UITextField!
@IBOutlet weak var slprice: UITextField!

var item: SList? = nil


override func viewDidLoad() {
super.viewDidLoad()

if item != nil{
slitem.text = item?.slitem
sldesc.text = item?.sldesc
slqty.text = item?.slqty
slprice.text = item?.slprice
}

// "x" Delete Feature
self.slitem.clearButtonMode = UITextFieldViewMode.WhileEditing
self.sldesc.clearButtonMode = UITextFieldViewMode.WhileEditing
self.slqty.clearButtonMode = UITextFieldViewMode.WhileEditing
self.slprice.clearButtonMode = UITextFieldViewMode.WhileEditing
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func dismissVC() {
navigationController?.popViewControllerAnimated(true)
}


// Dispose of any resources that can be recreated.


@IBAction func saveButton(sender: AnyObject) {

if item != nil {
edititems()
} else {
createitems()
}

dismissVC()
}

func createitems() {

let entityDescription = NSEntityDescription.entityForName("SList", inManagedObjectContext: moc)

let item = SList(entity: entityDescription!, insertIntoManagedObjectContext: moc)

item.slitem = slitem.text
item.sldesc = sldesc.text
item.slqty = slqty.text
item.slprice = slprice.text

if slitem.text == nil{
createitems()

}else{
edititems()
}

do {
try moc.save()
} catch _ {
return
}
}

func edititems() {
item?.slitem = slitem.text!
item?.sldesc = sldesc.text!
item?.slqty = slqty.text!
item?.slprice = slprice.text!

do {
try moc.save()
} catch {
return
}
}
}

我只是在学习和自学,所以如果您能向我展示并解释它以便我能够理解,我将不胜感激!

最佳答案

... the data doesn't load when it dismisses the second viewController ...

我无法立即明白为什么会这样。我建议使用断点和/或 print() 来尝试调试它。我希望表格 View 在 controllerDidChangeContent 委托(delegate)方法中由 FRC 自动更新,所以首先放置

print("Controller didChangeContent reloaded the tableView")

在该方法中查看它是否在您创建/编辑项目后出现在日志中。 AddNew IBAction 方法和 viewDidDisappear 也是如此。

... Do I create a second array for the section 1 items...

不需要。 FRC 应该为您管理部分和项目:假设 slcross 是一个 bool 属性,FRC 将分析 SLItem 对象并将它们正确分配给部分 0,如果 slcross 为假,第 1 节为真。 (用于在 FRC 中“查找”对象的 indexPath,例如

let items = frc.objectAtIndexPath(indexPath) as! SList

包含部分和行)。

...how to go about getting it from section 0 to section 1...

为确保项目在首次创建时正确分配,添加:

item.slcross = false

createItems() 方法。要将项目从第 0 部分移动到第 1 部分,请将此值更改为 true - 如您所说,didSelectRowAtIndexPath 可能是执行此操作的好地方:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let items = frc.objectAtIndexPath(indexPath) as! SList
items.slcross = true
}

...do I need to create a second entity for the crossed off items?

不需要,财汇局应该做的。

关于ios - UITableview 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com