gpt4 book ai didi

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:38 26 4
gpt4 key购买 nike

我在转至下一个 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/

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