簡體   English   中英

從表視圖為索引部分和行傳遞正確的Realm對象的問題

[英]Problems with passing the correct Realm object from tableview for index section and row

我在到下一視圖控制器的segue時遇到問題。 我有一個tableview在行和節中顯示領域數據。 如果我按下添加按鈕,它將進入視圖控制器,並允許我存儲數據並正確存儲所有數據。 如果選擇單元格,則應將我帶到同一視圖控制器,並用正確的數據填充文本字段。

目前,我只能為第一部分的indexpath行選擇數據。 我從其他部分選擇的每個單元格仍僅顯示第一部分的數據。

我嘗試了多種方法來獲取正確的部分,但失敗了。

有人可以告訴我我在做什么錯。

我正在使用xcode 7.3和2.0.3領域。

以下是我的表格視圖中的代碼。

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使用它,因此在兩個不同的地方沒有相同的過濾邏輯。

用此方法替換您的didSelectRowAtIndexPath方法。

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let contact: ContactItem = items.filter("Initial == %@", sectionNames[indexPath.section])[indexPath.row] performSegueWithIdentifier("editContact", sender: contact) print("Selected row at \\(indexPath.row)") print("Selected section at \\(indexPath.section)") } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM