- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在转至下一个 View Controller 时遇到问题。我有一个表格 View ,以行和部分的形式显示 Realm 数据。如果我按下添加按钮,它会转到 View Controller 并允许我访问数据并将其全部正确存储。如果我选择单元格,它应该将我带到同一个 View Controller 并用正确的数据填充文本字段。
目前我只能选择第一部分索引路径行的数据。我从其他部分选择的每个单元格仍然只显示第一部分的数据。
我尝试了很多方法来获取正确的部分,但都失败了。
谁能告诉我我做错了什么。
我使用的是 xcode 7.3 和 realm 2.0.3。
下面是我的表格 View 中的代码。
class TableViewController: UITableViewController {
let items = try! Realm().objects(ContactItem).sorted(["Initial", "Name"])
var sectionNames: [String] {
return Set(items.valueForKeyPath("Initial") as! [String]).sort()
}
//var sectionTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
tableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
func setupUI() {
tableView.registerClass(Cell.self, forCellReuseIdentifier: "cell")
}
func items(forSection section: Int) -> Results<ContactItem> {
return items.filter("Initial == %@", sectionNames[section])
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionNames.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionNames[section]
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return items(forSection: section).count
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionNames as [String]
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = items.filter("Initial == %@", sectionNames[indexPath.section])[indexPath.row].Name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let contact : ContactItem = items[indexPath.row]
performSegueWithIdentifier("editContact", sender: contact)
print("Selected row at \(indexPath.row)")
print("Selected section at \(indexPath.section)")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if(segue.identifier == "editContact")
{
let viewController = segue.destinationViewController as! AddEntryViewController
viewController.contact = sender as! ContactItem
}
}
@IBAction func addContact(sender: AnyObject) {
}
编辑下面的两个答案谢谢。
还有一个简单的问题 - 目前它只在添加联系人时显示索引部分标题,但我希望所有索引标题从一开始就显示在右侧,无论他们是否有联系人。这可能吗?
最佳答案
问题出在didSelectRowAtIndexPath
let contact : ContactItem = items[indexPath.row]
它应该是获取项目并过滤它们。您已经有了执行此操作的方法,因此 didSelectRowAtIndexPath
也应该使用它。
let contact : ContactItem = items(forSection: indexPath.section)[indexPath.row]
您可能应该在 cellForRowAtIndexPath
中使用它,这样您就不会在两个不同的地方使用相同的过滤逻辑。
关于ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346086/
目前有没有办法允许多个用户访问同一个 Realm? 现在我能找到的唯一方法是使用“应用程序帐户”而不是用户帐户,如 another question 中所建议的那样。 . 谢谢! 最佳答案 通常,您可
我有一个在 Ubuntu 服务器上运行的 Realm Object Server (v. 1.0)。我是否应该能够通过 Realm Browser 连接到它?应用程序? 这是我尝试过的。 文件 > 打
我正在使用 React-Native。在此我指的是 this在 React-Native 中使用 Realm 数据库的文档。我可以创建 react-native 数据库,但无法在 Realm-Stud
我想使用 Realm Mobile Platform 为我的应用程序提供同步,但不强制用户注册或登录。也就是说,我想先使用本地 Realm,然后切换到同步 Realm,如果用户决定使用该功能。 这可能
在主线程上写入 Realm 可以吗? 基本上,我想在开始 ActivityA 之前将一些 ObjectA 写入 Realm。 ActivityA 启动后,它需要立即访问(在 onCreate 中)Ob
表格大小有限制吗?我需要平均每秒添加一次新数据。我正在从蓝牙设备保存信息,因此每秒都会收到应用程序处于前台/后台的更新。 谢谢 最佳答案 Realm 使用内存映射来访问文件。根据操作系统的不同,每个进
假设有两个表 Box 和 Item。盒子里可能有很多元素,一件元素只有一个盒子。我想获取给定数组中包含盒子的所有项目。我怎么能这么做呢?在 CD 中,我将通过 Item 类中的谓词和属性来完成此操作,
我正在使用 realm-js 通过 React Native 为用户在他们的设备上存储数据,并且在工作流中有一点我想复制本地 Realm 中的所有数据进入同步 Realm (在 ROS 上持久化)。我
是否可以在不同用户之间共享同一个对象?这样,即使他们拥有自己的私有(private)对象,他们也可以在其中一些对象上进行共享/协作。我该怎么做? 最佳答案 来自 Realm 的 Katsumi。 Re
在Realm中有两种写事务的方式,它们有什么区别? 1. try! realm.write { ... } 2. realm.beginWrite() ... try! realm.commitW
我需要向 Realm 写入大量数据(例如 200000 输入),我使用 realm.add() 在后台线程中写入数据。但它收到了崩溃消息: Terminating app due to uncaugh
在 iOS 上,可以检查 Realm 的内容通过使用 Realm Browser 打开相应的文件来数据库.可以使用以下代码行打印该文件的路径(如 here 所述): print(Realm.Confi
realm.io 数据库是否支持多列索引或排序索引? documentation没有提到这些功能,但这似乎很奇怪,因为 Realm 被宣传为核心数据的替代品。 最佳答案 是的! Realm 允许您在单
我们在 AWS ECS 中运行 Keycloak docker 镜像,我们需要一种使用 ansible 导出 Realm 和所有用户以实现自动化目的的方法。我们可以使用 ansible 运行以下命令来
我正在考虑将新的 Realm 移动平台用于我的一个项目。我已经阅读了指南,并且能够在本地启动并运行它没有问题。我的问题是,部署 Realm 对象服务器以便远程运行的最佳方式是什么?我通读了找到的指南
我想在我的 Realm 数据库中使用已经预填充的数据来交付我的应用程序。我是否必须简单地将它复制到文档目录中,或者还有其他事情要做吗? 最佳答案 Realm 的文档中有一节关于 "Bundling a
我已按 Realm 创建了数据库,但无法找到该文件,因为我的操作系统(Yosemite)在/private/var/mobile 中没有移动文件夹。 我应该如何访问我的 Realm 以在浏览器中运行?
我有两个 Realm : public class ChatRealm extends RealmObject { private String id; private RealmLi
像上图一样,我想将RealmObject重命名为WeekRealmObject,这样命名更清晰。我尝试搜索 Stackoverflow 并检查 Realm Swift 文档及其示例,但似乎只能通过迁移
我通过 SyncCredentials 登录 Realm 允许创建用户,代码如下: SyncCredentials credentials = SyncCredentials.usernamePass
我是一名优秀的程序员,十分优秀!