2017-01-12 1 views

답변

1

내가 생각할 수있는 가장 쉬운 방법 : 그래서보기 컨트롤러가있는 경우 UIScrollView의 서브 클래스 (및 UITableViewDelegate 프로토콜 UIScrollViewDelegate 프로토콜에서 상속)입니다 UITableView 명심

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 50 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = "Cell #\(indexPath.row)" 
     return cell 
    } 

    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
     if let tableView = scrollView as? UITableView { 
      for cell in tableView.visibleCells { 
       adjustCellColor(cell: cell) 
      } 
     } 
    } 

    func adjustCellColor(cell: UITableViewCell) { 
     let cellFrame = tableView.convert(cell.frame, to: view) 
     if cellFrame.contains(view.center) { 
      cell.textLabel?.textColor = UIColor.red 
     } else { 
      cell.textLabel?.textColor = UIColor.black 
     } 
    } 
} 

UITableView에 대한 대표는 과 같은 UIScrollViewDelegate 메서드를 구현할 수 있습니다. 이 메서드는 테이블 뷰를 스크롤 할 때 호출됩니다. 보이는 셀을 모두 반복하고 셀이 뷰의 가운데에 있으면 텍스트 색상을 빨간색으로 설정하고 그렇지 않으면 검정색으로 설정합니다.

관련 문제