2016-11-06 7 views
0

My searchBarがアクティブでテキストがある場合、セルを削除できますが、削除されたセルが空白のままになると、再度読み込まれません。ここで私はこのコードが私のdeleteActionです。 cellForRowAtためsearcBarにテキストがあるときにtableViewを読み込むことができません

 if self.searchController.isActive && self.searchController.searchBar.text != "" { 

      context.delete(filteredArray[indexPath.row]) 
      tableView.reloadData() 
      self.appDelegate.saveContext() 
      do { people = try context.fetch(Person.fetchRequest()} catch { print("Fetching Failed !!")} 

     } else { 

      context.delete(people[indexPath.row]) 
      self.appDelegate.saveContext() 
      do { people = try context.fetch(Person.fetchRequest())} catch { print("Fetching Failed !!")} 
      tableView.reloadData() 

     } 

とコード..

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell! 

     let person = people[indexPath.row] 

     if searchController.isActive && searchController.searchBar.text != "" { 
      cell?.textLabel?.text = filteredArray[indexPath.row].name 

      if filteredArray[indexPath.row].isChecked { 
       cell?.accessoryType = .checkmark 
      } else { 
       cell?.accessoryType = .none 
      } 

     } else { 
      cell?.textLabel?.text = person.name 

       if person.isChecked { 
        cell?.accessoryType = .checkmark 
       } else { 
        cell?.accessoryType = .none 
       } 

     } 

     cell?.backgroundColor = UIColor.clear // loading color 

     return cell! 
    } 

そして、ここではスクリーンショットです。

enter image description here ​​

+0

スクリーンショットを追加する? –

+0

2つのスクリーンショットを追加しました –

+0

'reloadData()'の代わりに 'beginUpdates()'と 'endUpdates()'を使ってみてください – jjatie

答えて

0

あなたはfilteredArrayからnameを引いているためである:あなたの後

if self.searchController.isActive && self.searchController.searchBar.text != "" { 
    context.delete(filteredArray[indexPath.row]) 
    self.appDelegate.saveContext() 

    // This line below 
    do { people = try context.fetch(Person.fetchRequest()} catch { print("Fetching Failed !!")} 
    // Please also update your filteredArray here 
    filteredArray = ... 

    // And don't forget to reload after date is updated. 
    tableView.reloadData() 
} else { 
... 

if searchController.isActive && searchController.searchBar.text != "" { 
    cell?.textLabel?.text = filteredArray[indexPath.row].name // this line 

はしかし、あなただけのpeople配列を更新しましたfilteredArrayを更新しました。仕事。

+0

すでに同じ問題を試みていますが、代わりに削除後にsearchBarを無効にしましたが、良い解決策ではありません。 'self.searchController.isActive = false' ' self.searchController.searchBar.text = "" –

+0

@EnesKaraosman、私は答えを更新しました。あなたが今回必要とするものでなければなりません。 –

関連する問題