私は人の連絡先名を入力するためにUITableViewを実装しています。私はほとんどすべてのことを実装しています。セクションに空の行が含まれている場合、UITableHeaderのタイトルを削除します
行がユーザーによって削除され、他の行が含まれていないセクションのセクションヘッダータイトルを削除する必要があります。それを行うにはいくつかの方法がありますか? index.itemでクラッシュすると、タイトル配列から値を削除するだけです。その上の任意のアイデア...
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sortedUniqueFirstLetters[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! TableViewCell
cell.account = sections[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.setColor(Red: 223, Green: 220, Blue: 224, Alpha: 1)
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true;
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
***if indexPath.item == 0{ //Trying to update title array when a section is empty
sortedUniqueFirstLetters.remove(at: indexPath.section) //Index out of range error here.
}***
sections[indexPath.section].remove(at: indexPath.item)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.middle);
}
}
私はこれが最善の解決策ですが、私は何をするだろうことは、コールreloadtableであれば知りません。これはテーブルのすべてのデリゲートを思い出し、 'tableViewtitleForHeaderInSection'関数でセクションが行を持たないかどうかを確認し、そのセクションの空のヘッダを入れます。 –
セクションは空ではありませんが、両方のメソッドが正しいアイデアのように見えました。 – Bikram