2017-03-21 36 views
2

不是重複:
問題/答案是關於更改顏色。我改變單元格的顏色沒有問題,相反我有一個問題,當「顏色」.clear和插入單元格時,單元格正在「重置」。單元格backgroundColor在插入一行時清除奇怪的行爲

每當我將我的單元格的背景顏色設置爲.clear並在表格中插入一行時,單元格會自動「重置」爲其原始狀態。當背景顏色具有值時不會發生這種情況。

例如:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 

    let gesture = UIPanGestureRecognizer() 
    gesture.addTarget(self, action: #selector(handlePan)) 
    gesture.delegate = self 

    addGestureRecognizer(gesture) 

    backgroundColor = .clear 
} 

func handlePan(_ recognizer: UIPanGestureRecognizer) { 
    if recognizer.state == .changed { 
     let translation = recognizer.translation(in: self).x 

     contentView.frame.origin.x += translation 

     recognizer.setTranslation(.zero, in: self) 
    } 

    if recognizer.state == .ended { 
     guard let index = delegate?.tableView.indexPath(for: self) else { return } 

     delegate?.insert(index) 
    } 
} 

我的ViewController:

func insert(_ index: IndexPath) { 
    let cell = tableView.cellForRow(at: index) as? PannableCell 

    items.append("some") 

    let path = IndexPath(row: index.row + 1, section: index.section) 

    tableView.insertRows(at: [path], with: .fade) 
} 

問:
如何設置我的我的手機的背景色爲.clear和插入行無細胞是重啓?我想要這個,因爲插入單元格後,我想將原始單元格恢復到原始位置。

編輯
設置的backgroundColor顏色只適用於iOS的9在iOS系統10仍然以某種方式重置電池..

+0

可能重複[UITableViewCell顯示白色背景,無法在iOS7上修改](http://stackoverflow.com/questions/18878258/uitableviewcell-show-white-background-and-cannot-be-modified-on-ios7 ) – kevin

+0

感謝您的好鏈接。雖然它不能解決問題,但我希望顏色爲'.clear'時,如果我使用其他顏色(我使用我的表格的背景顏色作爲臨時修訂),它會這樣做。 –

+0

奇怪的是,如果我將'backgroundColor'更改爲另一種顏色(例如:'.gray'),則單元格仍然會重置(插入新單元格),直到單元格被重用爲止。在鏈接kkoltzau提供的答案提供了固定的(當顏色不是'.clear'。 –

回答

1

改變您的插入到這一點:

tableView.beginUpdates() 
tableView.insertRows(at: [path], with: .fade) 
tableView.endUpdates() 
+0

謝謝,但不起作用 –

1

嘗試在你的tableView上使用beginUpdates()endUpdates()

tableView.beginUpdates() 
// insert, delete your rows here 
tableView.endUpdates() 

如果不使用這些方法,你可以用各種錯誤,包括無效行數,UI毛刺等的最終

如果它不能幫助你可以嘗試「髒修復」它通過在您的單元格的prepareForReuse方法中設置所需的顏色或在tableView:willDisplayCell:forRowAtIndexPath:委託方法中設置(儘量避免像這樣髒的修復)。

+0

謝謝,不幸的是它不起作用。當顏色是'.clear'並插入單元格時,它總是會發生,即使顏色設置在'willDisplayCell'中。 –

+0

@HennyLee你可以在什麼地方上傳項目嗎?很難說,看不到實際的代碼 –

+0

不幸的是,我可以對不起,該項目不是我的,所提供的代碼應該能夠重現這一點。 –