2017-11-03 1 views
1

私のアプリでは、すべてのビューの背景色を暗く変更しました。しかし、tableViewから行を削除すると、行の背景が白に変わりました。なぜ、どうすれば修正できますか?tableViewから行を削除しているときにデフォルトの背景色を変更する

Screenshot

Reveal

tableView.backgroundView?.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
     view.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
     if dataManagement.taskData.count != 0{ 
      for rowIndex in 0...tableView.numberOfRows(inSection: 0) - 1 { 
       let cellPath = IndexPath(row: rowIndex, section: 0) 
       if let cell = tableView.cellForRow(at: cellPath) as? TaskListTableViewCell { 
        cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
        cell.taskLabel.textColor = UIColor.white 
       } 
      } 
     } 

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { //可删除 
    let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskListTableViewCell 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     if dataManagement.taskData[(indexPath as NSIndexPath).row].id == dataManagement.currentTask { 
      dataManagement.currentTask = nil 
      dataManagement.historyIsSelected = true 
      pomodoroTimer.stop() 
     } 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
} 
+0

関連コードを表示してください。 – LinusGeffarth

+2

テーブルを作成して行を削除するために使用したコードを表示します –

+0

テーブルはUITableViewControllerにあります。私は自分のコードを追加しました。 @LinusGeffarth –

答えて

0

変更プログラムで使用してセルの背景:あなたはeditingStyleが方法がかもしれコミットに単一ラインコードの下に追加してい

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = [UIColor <yourcolor>]; 
} 
+0

動作しません。私はすでにセルの背景色を変更しました。 –

0

それは助けます君は!!次のコード

cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 

や簡単な使用:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { //可删除 
    let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskListTableViewCell 
    cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) // set cell background color here!! 

    if editingStyle == UITableViewCellEditingStyle.delete { 
     if dataManagement.taskData[(indexPath as NSIndexPath).row].id == dataManagement.currentTask { 
      dataManagement.currentTask = nil 
      dataManagement.historyIsSelected = true 
      pomodoroTimer.stop() 
     } 
    tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
} 
1

は、レルムスウィフト文書からの回避策を見つけました。

https://realm.io/docs/swift/latest/#realm-notifications

彼らは更新をトリガする非同期を使用しています。 deleteinsert、またはupdateのメソッドをUITableViewDelegateの範囲で呼び出すことを避けてください。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     boards.remove(at: indexPath.row) 
     // use async to avoid showing white background. 
     DispatchQueue.main.async { 
      tableView.deleteRows(at: [indexPath], with: .automatic) 
     } 
    } 
} 
関連する問題