- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 Swift iOS 应用程序中有一个 tableView,它允许用户对行重新排序。点击“编辑”按钮,可以重新排序或删除行,然后再次点击“编辑”按钮(现在显示为“完成”)以完成该过程。
如果我只是使用 tableView:moveRowAtIndexPath:toIndexPath: 方法,它会按上述方式工作,并且当点击“完成”时,表格会正确显示重新排序的行。但是它在返回表时不记得新的顺序,所以我在核心数据中添加了一个新的“位置”变量 (Int),并根据它让我的表自行排序。
我的问题是,在用户移动一行并单击“完成”后,表格会立即自行重新排序,恢复到之前的状态。所以它在核心数据中使用旧的(原始的)位置变量,然后我的代码才能正确捕获新订单并将其重新写入核心数据。
有没有办法在表格重新加载前点击“完成”时捕获这个新订单?
这是我处理移动的代码:
func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) {
}
这是我在按下“完成”时调用的代码。我遍历表格的每一行,并根据新行在核心数据中重新分配人员的位置变量:
for currentIteration in 0..<tableView.numberOfRowsInSection(0) {
var indexPathForCurrentIteration = NSIndexPath(forRow: currentIteration, inSection: 0)
let personAtCurrentIndexIteration = fetchedResultsController.objectAtIndexPath(indexPathForCurrentIteration) as PersonModel
personAtCurrentIndexIteration.position = indexPathForCurrentIteration.row
println("\(personAtCurrentIndexIteration.name) has a position value of \(personAtCurrentIndexIteration.position)")
(UIApplication.sharedApplication().delegate as AppDelegate).saveContext()
tableView.reloadData();
}
当按下 Done 按钮时调用上面的代码,但我也尝试在 moveRowAtIndexPath 方法中调用它但没有成功(相同的结果 - 当按下 Done 时它重新加载表的旧顺序)。我也试过注释掉 reloadData 但没有成功。
我认为我的问题可能在于上面没有捕获新的行顺序,而是仍然获取旧的行顺序。如果是这样,那么我不知道让它“等待”获取新行顺序的最简单方法。
我开始考虑的一个更长的解决方法是捕获移动行的初始和最终行值,并基于该值相应地更新剩余行的值。其中一些代码在下面,但我停止了,因为我认为可能有更聪明的方法。
if((sourceIndexPath.row > indexPathForCurrentIteration.row) && (destinationIndexPath.row > indexPathForCurrentIteration.row)) {
//if the moved row was ahead of this row before AND after the move, do nothing (don't change this row's position value)
//println("\(personAtCurrentIndexIteration.name)'s position was unchnaged")
} else if((sourceIndexPath.row > indexPathForCurrentIteration.row) && (destinationIndexPath.row < indexPathForCurrentIteration.row)) {
//if the moved row was ahead of this row before BUT behind it after the move, then this row's position value needs to be bumped up one number
personAtCurrentIndexIteration.position = indexPathForCurrentIteration.row+1
//Etc...
在此先感谢您提供的任何帮助。
最佳答案
添加到 CoreData“orderPosition”类型的 Int 属性并将 orderPosition 添加到您的模型文件。
当您向 CoreData 添加一些数据时,还要添加 orderPosition 值
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("MyCoreData", inManagedObjectContext: contxt)
let freq = NSFetchRequest(entityName: "MyCoreData")
var MyData = contxt.executeFetchRequest(freq, error: nil)!
var newItem = Model(entity: en!, insertIntoManagedObjectContext: contxt)
var newOrderPosition = MyData.count
//here you add some data to your CoreData
//and also you must add new order position
newItem.orderPosition = orderPosition
在您的 TableView 中,您必须创建一个函数来对您将在 TableView 中显示的数组进行排序。像这样:
func RefreshCoredata()
{
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let freq = NSFetchRequest(entityName: "MyCoreData")
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let sortDescriptor = NSSortDescriptor(key: "orderPosition", ascending: true)
freq.sortDescriptors = [sortDescriptor]
MyData = contxt.executeFetchRequest(freq, error: nil)!
}
记住,在将他发送到表之前,您必须始终对数组进行排序!
如果要对表格行重新排序,您必须做什么? :
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
RefreshCoredata()
if fromIndexPath.row > toIndexPath.row
{
for i in toIndexPath.row..<fromIndexPath.row
{
MyData[i].setValue(i+1, forKey: "orderPosition")
}
MyData[fromIndexPath.row].setValue(toIndexPath.row, forKey: "orderPosition")
}
if fromIndexPath.row < toIndexPath.row
{
for i in fromIndexPath.row + 1...toIndexPath.row
{
MyData[i].setValue(i-1, forKey: "orderPosition")
}
MyData[fromIndexPath.row].setValue(toIndexPath.row, forKey: "orderPosition")
}
contxt.save(nil)
RefreshCoredata()
}
这需要在您的 CoreData 中重置排序位置
当你想删除一些表格行时,你必须做什么?
override func tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
RefreshCoredata()
contxt.deleteObject(MyData[indexPath.row] as NSManagedObject)
for i in indexPath.row + 1..<MyData.count
{
MyData[i].setValue(i-1, forKey: "orderPosition")
}
RefreshCoredata()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
contxt.save(nil)
}
就是这样!
关于xcode - 使用 tableView :moveRowAtIndexPath:toIndexPath: method 后将新订单保存到核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413081/
我正在对 tableView 是否呈现单元格进行单元测试。 我发现 tableView.cellForRow(at:) 返回 nil,而 tableView.dataSource?tableView(
我有 Tableview 用于显示客户下的食品订单。其中包含水平 UIStackview。在 UIStackView UIStackview 中很少有一/两行标签和一个用于显示订单项的UITableV
我有 Viewcontroller,我在其中设置了 ScrollView 。在 scrollview 中,我有一个 tableview,它位于 Container 中,就在容器下方,当我将 table
我想在滚动时让我的第一个单元格始终位于 tableview 的顶部。 任何帮助都是可观的。提前致谢... 最佳答案 这是为 UITableView 创建标题 View 的方法 - (UIView *)
在我的应用程序中,我点击一行,它会在其下方展开另一行。我想生成一种随机颜色,当我单击一行时,行背景会变成该颜色,而它下面的展开行会变成相同的颜色。如何让行生成相同的随机颜色? 我创建了一个函数来生成随
我正在为这个问题苦苦挣扎,所以我需要你的帮助。基本上我已经编写了一个复杂的 TableView Controller (使用 NSFetchedResults 协议(protocol)等)并且已经在我
我正在使用 JavaFx 2.2。我遇到了一个问题,我无法在 TableView 列中放置不同的组件。例如我有两列 1) 回答 2) 答案类型 如果 AnswerType 包含“Multiple Ch
试图弄清楚如何在 javafx 2 中禁用表列的重新排序? 最佳答案 这是解决方案: tblView.getColumns().addListener(new ListChangeListener()
我一直在寻找有关将数据刷新到 tableview 的信息。我试图直接修改模型,但我遇到了一个错误。我修改了模型,但表格没有刷新,只有当我移动一列时,表格才会显示修改后的值。 为了给你展示一个例子(13
给定一个TableView,我需要检测单元格上的双击。 tableView.setOnMouseClicked(new EventHandler() { @Override publi
我成功创建了一个 TableView,其中将特定列分配给了数据模型类。该程序可以将 csv 文件解析为其中并在表中正确显示所有内容。在此阶段,滚动不是问题。 然后我想选择特定行并将它们发送到另一个表。
我尝试了所有方法来用数据填充 TableView。下一个代码在表中插入一个新行,但数据没有出现在表中。我试图为此找到解释,但没有成功。 请帮忙。我不知道怎么了。 在 controller.java 中
我尝试了所有方法来用数据填充 TableView。下一个代码在表中插入一个新行,但数据没有出现在表中。我试图为此找到解释,但没有成功。 请帮忙。我不知道怎么了。 在 controller.java 中
我有一个通过 Tableview 显示玩家的应用程序。用户可以“添加”一个新玩家并输入他的姓名、高度、体重、图片等...保存后将被添加到 Tableview 中。他们还可以点击每个玩家,然后将他们带到
对于大学,我必须编写一个小应用程序,其想法是创建一个小公式集合。这个概念是有一个类别列表,选择类别后您会收到一个公式列表。选择所需的公式后,您将看到公式计算器的 View 。 我正在努力获得第一个UI
如果我只加载一个 TableView ,我的 View 中必须有两个 TableView ,它工作正常。但是当我尝试使用下面的方法加载两个表格 View 时,它给出了以下异常。 未捕获异常“NSRan
我是 JavaFx 的新手,我正在尝试创建一个 TableView ,而 fxml 是使用场景生成器创建的。如果我运行该程序,该表不会获取值。我发现这个问题在某种程度上符合我的要求( javaFX 2
是否可以对 tableView 进行反向排序?我搜索了很多解决方案,但没有任何效果。 效果就像whatsapp聊天。 一个普通的tableView是: ---------- label 1 -----
我有一个有趣的问题。我有两个 TableView ,一个在另一个里面。我有一个结构,我想用它来将数据添加到我的 TableView 中。该结构有 2 个字符串项和另一个具有 3 个字符串项的结构的数组
我正在使用的方法 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath
我是一名优秀的程序员,十分优秀!