2017-08-14 5 views
1

私は人の連絡先名を入力するために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); 
     } 
    } 
+0

私はこれが最善の解決策ですが、私は何をするだろうことは、コールreloadtableであれば知りません。これはテーブルのすべてのデリゲートを思い出し、 'tableViewtitleForHeaderInSection'関数でセクションが行を持たないかどうかを確認し、そのセクションの空のヘッダを入れます。 –

+0

セクションは空ではありませんが、両方のメソッドが正しいアイデアのように見えました。 – Bikram

答えて

0

はまた、あなたのtitleForHEaderInSction方法と一緒に、同様heightForHeaderInSctionを使用する必要があります。コードの下にあなたのために働くなら、私に教えてください:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    let numberOfRows = tableView.numberOfRows(inSection: section) 
    if numberOfRows == 0 { 
     return "" 
    }else{ 
     return sortedUniqueFirstLetters[section] 
    } 

} 

func tableView (tableView:UITableView , heightForHeaderInSection section:Int) -> Float { 
    let title = self.tableView(tableView, titleForHeaderInSection: section) 
    if (title == "") { 
     return 0.0 
    } 
    return 80.0 
} 
+0

'heightForHeaderInSection'(実際には' viewForHeaderInSection'だけで使用する必要があります)を実装するのではなく、単純に空文字列の代わりに 'titleForHeaderInSection'から' nil'を返します。 – rmaddy

+0

私はnumberOfRows == 0でcrahsを得ています。 – Bikram

関連する問題