그래서 X가 여기에 + 1. 내 collectionView을 위해 사용하고있는 위임 기능있는 변수 matchesFromSelectedSession.count에 의해 설정되는 간단한 UICollectionView, X 행 3 열, 함께 일하고 :UICollectionView.reloadData()는 셀 순서를 변경합니다. 아이폰 OS 스위프트
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellHeight = 15.0 as CGFloat
return CGSizeMake(collectionView.bounds.size.width/3, cellHeight)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return matchesFromSelectedSession.count + 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let reuseIdentifier = "MatchCollectionViewCell"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MatchCollectionViewCell
cell.matchCollectionLabel = UILabel(frame: CGRectMake(0,0,collectionView.bounds.width/3 - 3.0,15))
cell.matchCollectionLabel.textColor = UIColor.blackColor()
cell.systemLayoutSizeFittingSize(cell.frame.size, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)
cell.matchCollectionLabel.font = UIFont(name: "Helvetica Neue", size:12)
if indexPath.section == 0 {
switch indexPath.row {
case 0:
cell.matchCollectionLabel.text = "Match #"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
break
case 1:
cell.matchCollectionLabel.text = "Record"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
break
case 2:
cell.matchCollectionLabel.text = "Outcome"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
break
default:
cell.matchCollectionLabel.text = ""
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
break
}
} else {
switch indexPath.row {
case 0:
cell.matchCollectionLabel.text = "\(indexPath.section)"
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
break
case 1:
cell.matchCollectionLabel.text = matchesFromSelectedSession[indexPath.section - 1].matchRecord()
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
break
case 2:
let outcome = matchesFromSelectedSession[indexPath.section - 1].matchOutcome()
switch outcome {
case "W":
cell.matchCollectionLabel.text = "Win"
cell.matchCollectionLabel.textColor = UIColor.greenColor()
break
case "D":
cell.matchCollectionLabel.text = "Draw"
cell.matchCollectionLabel.textColor = UIColor.blueColor()
break
case "L":
cell.matchCollectionLabel.text = "Loss"
cell.matchCollectionLabel.textColor = UIColor.redColor()
break
default:
cell.matchCollectionLabel.textColor = UIColor.blackColor()
break
}
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
break
default:
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.text = ""
break
}
}
//add subview if new cell otherwise tag cell to know to reuse next time
if cell.tag != 19 {
cell.addSubview(cell.matchCollectionLabel)
}
cell.tag = 19
return cell
}
을
내가 사용하는 사용자 정의 셀, MatchCollectionViewCell에는 matchCollectionLabel이라는 레이블 하나만 포함되어 있습니다. 보조 뷰가 테이블을 변경하고 원래 뷰로 다시 닫을 때 뷰의 viewDidAppear 함수에서 self.matchCollectionView.reloadData()를 사용합니다.
모든 viewDidAppear 후에 셀에 불필요한 하위 뷰를 지속적으로 추가하지 않고 첫 번째 사용 후 셀에 태그를 지정합니다.
초기 설정 후에 테이블이 좋아 보이지만 reloadData를 호출하면 collectionView의 셀 순서가 변경되고 뒤죽박죽이됩니다. 셀 수를 편집하면 reloadData() 업데이트가 올바르게 셀 번호 변경을 표시하지만 셀의 순서가 올바르지 않습니다. 여기
는 어떤 도움을 크게 감상 할 수있다 이전과 reloadData후 사진입니다! 감사!
수 collectionView의 전후 사진을 게시 하시겠습니까? 그것은 약간의 단서를 제공 할 수 있습니다. – DJohnson
매번 새 레이블을 작성합니다. 태그가 19가 아닌 경우에만 하위보기로 추가하지만 항상 새 레이블을 할당합니다. 즉, 셀을 다시 사용할 때 셀에 추가 된 레이블이 아닌 새 레이블을 업데이트하므로 변경 내용이 표시되지 않습니다. 하단의 태그 체크는 함수의 상단에 있어야하며, 태그가 19가 아닌 경우 새 라벨을 만들어야합니다. 'cell.matchCollectionLabel'을 옵션으로 정의하고 nil을 확인해 볼 수도 있습니다 – Paulw11
@ DJohnson 문제의 그림을 추가했지만 Paulw11이 필요한 답변을 제공했습니다. 고마워요! –