2016-09-01 1 views
2

내 tableviewcell에 투명 그라디언트 색상을 설정하려고합니다. 첫 번째로드에서는 모든 것이 잘 작동하지만 각 스크롤과 함께 색상이 다시로드되고 그라디언트가 각 스크롤에서 어둡고 어둡게 보이고 각 스크롤에서 tableview가 더 어둡게 보입니다. 처럼 셀에 하위 뷰를 추가하고보기 컨트롤러에 연결tableviewcell의 알파가있는 그래디언트가 각 스크롤이 더 어둡게됩니다.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.contentView.layer.insertSublayer(gradient(cell.bounds), atIndex: 1) 
} 

func gradient(frame:CGRect) -> CAGradientLayer { 
    let layer = CAGradientLayer() 
    layer.frame = frame 
    layer.startPoint = CGPointMake(0,0.68) 
    layer.endPoint = CGPointMake(0,0.75) 
    layer.colors = [UIColor.clearColor().CGColor,UIColor.blackColor().colorWithAlphaComponent(0.2).CGColor] 
    return layer 
} 
+0

재사용 할 수없는 셀로 생각합니다. 반복적으로 할당하고 있습니다. 뷰를 스크롤하는 동안 더 어두워지고 있습니다. –

+0

셀은 재사용 가능한 객체이므로 초기화시 레이어를 한 번만 삽입해야합니다. * ('tableView : cellForRowAtIndexPath :'해당 레이어를 추가하기 좋은 위치 일 수 있습니다. 사용자 정의 셀 클래스를 작성하지 않으려는 경우) *. 그런 다음 해당 셀의'willDisplayCell' 또는'layoutSubviews' 메소드에서 레이어 프레임을 업데이트 할 수 있습니다. – ozgur

+0

확실히 세포를 재사용 할 때 문제가됩니다. 셀을 재사용하는 경우 (셀해야 함) 스크롤 할 때마다 셀의 투명도 = cell.trasparency + 0.1 (증분을 표시하는 의사 코드)을 설정하면 투명도가 증가합니다. – BlackM

답변

1

1) :

enter image description here

class MyCell : UITableViewCell {  
    @IBOutlet weak var bgView: UIView!   
} 

2) - : 여기에 그라데이션 색상을 구현하기위한 내 코드는에서 귀하의 회선을 제거하십시오 willDisplayCell 방법 이동 귀하의 cellForRowAt() 메소드는 다음과 같이 업데이트했습니다.

override func tableView(tableView: UITableView, cellForRowAtIndexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell 

    if(cell.bgView.layer.sublayers == nil) { 
     cell.bgView.layer.insertSublayer(gradient(frame: cell.bounds), at: 1) 
    } 
    return cell 
} 
+0

죄송합니다. 신속한 불일치 문제로 어려움을 겪었을 때 Swift 3.0에 배포하고있었습니다. – pedrouan

+1

고마워요,이게 실제로 문제를 해결했습니다! – Parion