レルムデータベースに格納されている連絡先のリストがあり、今では連絡先の名前をテーブルビューに表示します。リストとしてはうまく動作し、名前の昇順でソートすることができます。私は、インデックスリストの各文字のためにこれらの名前をグループ化するのに苦労しています。私のコードは、各セクションに同じ情報を埋めています。すぐにRealmを使用してインデックス付きテーブルビューを作成する
私のコードは次のようになります。レルムデータベースをフィルタリングし、正しくセクションを移入する方法について
var contacts: Results<ContactItem>!
var contactIndexTitles = [String]()
@IBOutlet weak var tblContacts: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupUI()
let contactIndex = "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"
contactIndexTitles = contactIndex.componentsSeparatedByString(" ")
self.reloadTheTable()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
reloadTheTable()
}
func setupUI() {
tblContacts.delegate = self
tblContacts.dataSource = self
}
func reloadTheTable() {
do {
let realm = try Realm()
contacts = realm.objects(ContactItem).sorted("Name", ascending: true)
tblContacts.reloadData()
print("reload tbl \(contacts)")
} catch {
}
}
//willDisplayCell forRowAtIndexPath
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.clearColor()
}
//numberOfSectionsInTableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return contactIndexTitles.count
}
//sectionForSectionIndexTitle
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int{
return index
}
//titleForHeaderInSection
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return self.contactIndexTitles[section] as String
}
//numberOfRowsInSection
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return contacts.count
}
//sectionIndexTitlesForTableView
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return contactIndexTitles as [String]
}
//cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifer: String = "myCell"
var cell = tblContacts.dequeueReusableCellWithIdentifier(identifer)
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: identifer)
}
let contactinfo = contacts[indexPath.row]
cell?.textLabel?.text = contactinfo.Name
cell?.detailTextLabel?.text = contactinfo.KeyNumber
return cell!
}
任意の提案ですか?
EDITは、だから私は、最初のリンクと、与えられたサンプルを使用してコードを修正したのだが、[OK]を表示しています。
TableViewControllerコード:
var contacts: Results<ContactItem>!
するvar contactIndexTitles =ストリング
クラスTableViewController:のUITableViewController {
@IBOutlet weak var tblcontacts: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupUI()
self.reloadTheTable()
let contactIndex = "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"
contactIndexTitles = contactIndex.componentsSeparatedByString(" ")
//print(Realm.Configuration.defaultConfiguration.fileURL!)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
reloadTheTable()
}
func setupUI() {
tblcontacts.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func reloadTheTable() {
do {
let realm = try Realm()
contacts = realm.objects(ContactItem.self).sorted("Name")
tblcontacts.reloadData()
}
catch
{
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return contactIndexTitles.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return contactIndexTitles[section]
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return contactIndexTitles
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return contacts.filter("Name BEGINSWITH %@", contactIndexTitles[section]).count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tblcontacts.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = contacts.filter("Name BEGINSWITH %@", contactIndexTitles[indexPath.section])[indexPath.row].Name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let contact : ContactItem = contacts[indexPath.row] //, contactIndexTitles[indexPath.section])
print(contact)
performSegueWithIdentifier("addContact", sender: contact)
print("Selected row at \(indexPath.row)")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if(segue.identifier == "addContact")
{
let viewController = segue.destinationViewController as! AddEntryViewController
viewController.contact = sender as! ContactItem
}
}
}
もう1つ問題があり、次のコントローラにセグがあります。私は正しいセクションに正しい行に行くことができるように正しいコードを取得するように見えることはできません。 正しいセクションから正しい行を選択するにはどうすればよいですか?ここで
例については、http://stackoverflow.com/a/38797693/373262を参照してください。グループ化されたテーブルビュー。 – jpsim
私は提案された例を見てきましたが、ハードコーディングされた情報を使って、そのセクションの配列とオブジェクトの結果リストを使用していないことを確認できます。どちらかというとシンプルなものが欠けているか、私はマークから離れていて、おそらくプログラマーではないでしょう。 – scooby