2017-02-06 1 views
1

모든 셀에 그라디언트가있는 tableview 컨트롤러를 만들려고합니다.그라데이션 배경을 UITableViewCell에 설정하십시오.

내 접근 방식은 아래 코드에서 올바른 색상을 설정했지만 색상이 무작위로 설정되면 색상이 스크롤 될 때 표시됩니다.

나는 어디에서 오류인지 알지 못합니다. textLabels은 올바르게 설정되지만 색상은 설정되지 않습니다.

import UIKit 


class ResourceOverViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
let reuseIdentifier = "OverViewCell" 
@IBOutlet weak var prototypeCell: UITableViewCell! 
@IBOutlet weak var tableView:UITableView! 
var resourceInMissions:[ResourceInMission] = [ResourceInMission]() 
var whiteRussian:Int64 = Int64() 
var Caipirinha:[String] = [String]() 
var Avails:[CAGradientLayer] = [CAGradientLayer]() 


override func viewDidLoad() { 
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier) 
    super.viewDidLoad() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 


override func viewWillAppear(_ animated: Bool) { 
      super.viewWillAppear(animated) 
    setView() 

} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return Caipirinha.count 
} 


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    print(indexPath) 

    cell.textLabel?.backgroundColor = UIColor.clear 
    cell.textLabel?.isOpaque = true 
    cell.isOpaque = true 

    var gradient = Avails[indexPath.row] 
    gradient.frame = cell.bounds 

    cell.textLabel?.layer.insertSublayer(gradient, at: 0) 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) 
    cell.textLabel?.text = Caipirinha[indexPath.row] 

    return cell 
} 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 
    var index: IndexPath = indexPath 
    let cell = tableView.cellForRow(at: indexPath) 
    whiteRussian = resourceInMissions[indexPath.row].getID() 



    guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "ShowResource") as UIViewController? else { 
     print("Could not instantiate view controller with identifier of type SecondViewController") 
     return 
    } 

    vc.setObjectID(idToTransfer: whiteRussian) 
    present(vc, animated:true, completion: nil) 

} 

@IBAction func back(){ 
    //Code is in Extension of UIViewController 
    moveBack() 

} 

func sort(resourceInMissions:[ResourceInMission]) -> [ResourceInMission] { 

    var arrayOfArrays = [[ResourceInMission](), [ResourceInMission](), [ResourceInMission](), [ResourceInMission](), [ResourceInMission](), [ResourceInMission]()] 
    // At first take the availability to sort. 
    // It produces availibility lists 
    for element in resourceInMissions { 


     // gets the index of the avail. from availibilities array 
     // for every avail a new list is made in arraysOfArray 


     if let string = element.getAvailability(){ 
      if let av = availibilitys.index(of: string){ 

       arrayOfArrays[av].append(element) 

      } 
     } 

    } 



    // Now iterate through each list and sort for the resourceType 
    // Idea is: get rsIM from one of the arrays above and put it in a type array for each element 
    // after finishing the avail array merge all type arrays for avail list sorted for types 
    var arraySorted:[ResourceInMission] = [ResourceInMission]() 
    for array in arrayOfArrays { 

     // lists have to be initialized for each loop 
     // otherwise the lists would be merged 
     var type1:[ResourceInMission] = [ResourceInMission]() 
     var type2:[ResourceInMission] = [ResourceInMission]() 
     var type3:[ResourceInMission] = [ResourceInMission]() 
     var type4:[ResourceInMission] = [ResourceInMission]() 
     var type5:[ResourceInMission] = [ResourceInMission]() 
     var type6:[ResourceInMission] = [ResourceInMission]() 
     var type7:[ResourceInMission] = [ResourceInMission]() 
     var type8:[ResourceInMission] = [ResourceInMission]() 

     for element in array { 

      if let id = element.getResourceID(){ 
       if let resource = Resource().getByIdFromDB(id: id){ 
        if let typeID = resource.getResourceTypeID(){ 

         switch typeID { 
         case 1: 
          type1.append(element) 
          break 
         case 2: 
          type2.append(element) 
          break 
         case 3: 
          type3.append(element) 
          break 
         case 4: 
          type4.append(element) 
          break 
         case 5: 
          type5.append(element) 
          break 
         case 6: 
          type6.append(element) 
          break 
         case 7: 
          type7.append(element) 
          break 
         case 8: 
          type8.append(element) 
          break 
         default: 
          break 
         } 
        } 
       } 
      } 
     } 

     arraySorted = arraySorted + type1 + type2 + type3 + type4 + type5 + type6 + type7 + type8 


    } 
    return arraySorted 
} 

func setView(){ 
    var count = 0 


    resourceInMissions = [ResourceInMission]() 
    resourceInMissions = try! ResourceInMission().getOverviewList() 
    count = resourceInMissions.count 


    resourceInMissions = sort(resourceInMissions: resourceInMissions) 

    Caipirinha = [String]() 
    Avails = [CAGradientLayer]() 
    Caipirinha.removeAll() 
    for index in 0 ..< resourceInMissions.count { 
     Caipirinha.append(resourceInMissions[index].toString()) 
     let gradient: CAGradientLayer = CAGradientLayer() 

     if let av = resourceInMissions[index].getAvailability(){ 

      switch av { 
      case availibilitys[0]: 
       //blue 
       let blue = [hexStringToUIColor(hex: "#E5EEFF"), hexStringToUIColor(hex: "#93B7FF"), hexStringToUIColor(hex: "#4F8EFF")] 
       gradient.colors = blue.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      case availibilitys[1]: 
       //red 
       let red = [hexStringToUIColor(hex: "#FFE5E5"), hexStringToUIColor(hex: "#FF8E8B"), hexStringToUIColor(hex: "#FF4F4F")] 
       gradient.colors = red.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      case availibilitys[2]: 
       //rose 
       let rose = [hexStringToUIColor(hex: "#FFFFFF"), hexStringToUIColor(hex: "#FDEADA"), hexStringToUIColor(hex: "#FAC090")] 
       gradient.colors = rose.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      case availibilitys[3]: 
       //yellow 
       let yellow = [hexStringToUIColor(hex: "#F5FFE6"), hexStringToUIColor(hex: "#F0FB85"), hexStringToUIColor(hex: "#FBF622")] 
       gradient.colors = yellow.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      case availibilitys[4]: 
       //green 
       let green = [hexStringToUIColor(hex: "#F5FFE6"), hexStringToUIColor(hex: "#C8FB85"), hexStringToUIColor(hex: "#BCFB5F")] 
       gradient.colors = green.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      case availibilitys[5]: 
       //grey 
       let gray = [hexStringToUIColor(hex: "#F2F2F2"), hexStringToUIColor(hex: "#E9E9E9"), hexStringToUIColor(hex: "D9D9D9")] 
       gradient.colors = gray.map { $0.cgColor } 
       Avails.append(gradient) 
       break 
      default: 
       break 
      } 
     } 


    } 
    self.tableView.reloadData() 
    } 
}  

답변

0

에게 그라디언트 무작위 나타나는 이유를 도움이 될 것입니다 세포

희망 때문에 세포 재사용이다.

cell.textLabel?.layer.insertSublayer(gradient, at: 0) 

이 줄은 사용자가 셀을 표시 할 때마다 호출, 그래서 처음 작동하지만 당신의 세포를 다시 얻을로 새로운 그라디언트는 이전 내용 뒤에 추가지고지고 있습니다.

새 그라데이션 레이어를 추가하기 전에 이전 그라디언트 레이어를 제거하거나 업데이트 할 수있는 그라데이션 레이어가있는 UITableViewCell 하위 클래스를 만들 수 있습니다.

+0

예. 재사용을 제거하고 필요할 때마다 새 셀을 만듭니다. 매우 효율적이지는 않지만 내 요구에 부합합니다. – me2

1

귀하의 접근 방식이 잘못되었습니다.

당신은 UIView의 가진 이름 GradientBackgroundView의 하위 클래스를 만들고 스토리 보드에서이보기 2)의의 drawRect에 그라데이션을 그릴, 당신의 tableview에있는 UITableViewCell을 추가하고의 배경으로 GradientBackgroundView를 추가 할 것을 1) 이하로 할 필요가 이것은 당신

관련 문제