2016-10-16 3 views
2

프로젝트에 UITableView 컨트롤러가 있습니다. 그래서 내가 여기에 같은있는 UITableViewCell 설정을 만들어 : 자신의 인덱스가있는 tableview가 나타납니다 2.UITableViewCell의 배경 설정이 스크롤 할 때 재설정됩니다.

로 나눌 수없는 경우

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

    cell.textLabel?.text = "Section: \(indexPath.section). Row: \(indexPath.row)." 

    if indexPath.row % 2 == 1 { 
     cell.backgroundColor = UIColor.gray 
    } 

    return cell 
} 

내가 내있는 tableview의 세포가 회색으로 원하는 모든 것이 완벽하다! 그러나 위아래로 스크롤하면 세포가 회색으로 변하기 시작합니다.

결국 내 모든 세포는 회색입니다.

는 여기에 몇 가지 이미지입니다 : 세포가 재사용되기 때문에

before

after

답변

3

시도는, else 문을 추가 할 수 있습니다.

else { 
    cell.backgroundColor = UIColor.white 
} 
+0

감사합니다 많이) 그것은 너무 쉬웠다) :! D –

+0

@AlexVihlayev 없음 문제 메이트. :) –

0

있는 tableview 셀

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

cell.textLabel?.text = "Section: \(indexPath.section). Row: \(indexPath.row)." 

if indexPath.row % 2 == 1 { 
    cell.backgroundColor = UIColor.gray 
}else{ 
    cell.backgroundColor = YOUR_COLOR 
} 
return cell 

}를 재사용하기 때문에

편집 :에서 Gellert 리 처음과

2

매우 간단한 문제는 다시 흰색으로 배경을 설정하지 않을 것입니다 대답했다. 셀이 재사용되고 있기 때문에 어느 시점에서 모든 셀에 대해 회색을 설정합니다. 대신 때마다 셀이 재사용 행 인덱스를 확인해야합니다!

cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.white : UIColor.gray 
관련 문제