0

하나의보기 컨트롤러에는 텍스트 필드가 있고 다른 하나에는보기가있는 응용 프로그램이 있습니다. 컬렉션보기 셀에 입력 한 텍스트를 표시하고 있습니다. 이 위대한 작품 그러나 하드 코딩 된 사용자가 텍스트 위에 표시 할 두 개의 셀을 코딩했습니다. 이 두 세포에서 배경을 바꾸고 싶습니다. 가능합니까? "이름"과 "점수"는 하드 코딩 항목 우리가 기본적으로 추가 된있는 contentView를 얻을 수있는 tableView의 경우 어떤 도움UICollectionview 셀의 배경을 변경하는 방법

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return ScoreArray.count  
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 

    let Mycell = Cell.viewWithTag(1) as! UILabel 

    Mycell.text = ScoreArray[indexPath.row] 

    return Cell 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    ScoreArray.append("NAMES") 
    ScoreArray.append("SCORES") 
} 

답변

0

에 대한 감사 (I가 배경을 변경하고자하는 사람)입니다. collectionView 위해 우리가 너무 코드 예 사용하여 액세스 할 수 있습니다

let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyCell 
myCell.contentView.backgroundColor = .gray // contentView is get only 
myCell.contentView.addSubView(UIView()) 
+1

추가에게 서브 뷰 등의 UIView는()의 기능은 무엇입니까? 이것은 복잡한 것처럼 보입니다. –

+0

귀하의 질문은 나를 혼란스럽게합니다. 배경을 바꾸는 것은 무엇을 의미합니까? 색상을 변경하거나, 무엇인가를 보거나 보여줄 수 있습니까? – amber

0

는 말에 셀의 배경 색상을 변경하려면, 녹색 :

Cell.backgroundColor = UIColor.green 

특정 셀을 변경하려면 다음을 수행해야 indexPath는 셀을 고유하게 식별하기 때문에 indexPath를 살펴보십시오.

indexPath에는 섹션 및 행 속성이 있습니다. 당신은 단지 하나 개의 섹션을 가정 할 때, 당신은 다음과 같이 cellForItemAt에서 행 속성을 볼 수 있습니다 :

if indexPath.row == 0 { 
    // make the first cell green 
    Cell.backgroundColor = UIColor.green 
} 
else { 
    // make all others red 
    Cell.backgroundColor = UIColor.red 
} 
+0

하지만 색상을 변경하려면 특정 셀을 선택해야합니까? – c3pNoah

관련 문제