단추의 가로 컬렉션보기가 있습니다. 그 중 하나가 클릭되면 다른 뷰 컨트롤러로 이동합니다. 종종 모든 버튼이 보이지 않을 때가 있기 때문에, 내가 원했던 것은 선택된 버튼이 콜렉션 뷰의 가장 왼쪽에있는 것이 었습니다.ScrollToItem이 제대로 작동하지 않습니다.
scrollToItem 함수를 사용하여이 작업을 시도하고있었습니다. 문제는 매번 컬렉션보기를 매번 오른쪽으로 계속 스크롤하는 것입니다.
관련 코드 : 어떤 도움을 크게 감상 할 수
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return labelArray.count
}
//frees up the collection view when the scroll is active
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isScrolling = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
//possible problem
if isScrolling == false {
collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
}
let myLabel = cell.viewWithTag(1) as! UILabel
let myArray = labelArray[indexPath.row]
myLabel.text = labelArray[indexPath.row]
myLabel.textColor = UIColor.white
if myArray == labelArray[1] {
cell.backgroundColor = UIColor.black
} else {
cell.backgroundColor = UIColor(red: 56/255, green: 120/255, blue: 195/255, alpha: 1)
}
return cell
}
.
그래서 이것을 didSelectItemAt 메소드에서 구현하려고 시도했습니다. 콜렉션 뷰가 처음부터 오른쪽으로 스크롤하는 것을 멈추게합니다. 문제는 아무 것도하지 않는 것입니다. –
위의 코드는 내가 편집 한 간단한 콜렉션 뷰 앱에서 작동한다는 것을 테스트했습니다. didSelectItemAt 메서드 또는 클래스의 나머지 코드를 어떻게 구현하는지 보지 않고 문제가 어디에 있는지 확신 할 수 없습니다. – bjd23
cellForItemAt 메소드에서 작동하도록했습니다. 항목 색인을 하드 코딩해야했습니다. 나는 대답을 게시 할 것이다. 도와 줘서 고마워. –