2017-08-21 1 views
-1

Sie möchten meinen Code vereinfachen, indem Sie eine Funktion in meiner Tabellenansichtsfunktion verwenden. Ich denke, es ist möglich, aber ich kann nicht herausfinden, wie ich überhaupt anfangen soll. Ich habe versucht, eine Funktion zum Ausführen der gewünschten Aktionen zu erstellen, aber ich kann nicht herausfinden, wie die benutzerdefinierten Zellenvariablen in meiner Funktion aufgerufen werden.Vereinfachen von Tabellenansichtscode mit Funktion zum Aufrufen von benutzerdefinierten Tabellenansichtszellen

Hier ist der Code, den ich vereinfachen wollen:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.section == 0 { 
     // PART 1 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath) as! PaydayDetailCellA1 
     let (jobDescription, day1, day2, day3, day4, day5, day6, day7, _) = tempPaydayDailyJobs[indexPath.row] 

     cell.tallyView.layer.cornerRadius = cell.tallyView.bounds.height/6.4 
     cell.tallyView.layer.masksToBounds = true 
     cell.tallyView.layer.borderColor = UIColor.lightGray.cgColor 
     cell.tallyView.layer.borderWidth = 0.5 

     cell.jobDesc.text = jobDescription 

     cell.dayColor1.text = day1 
     switch day1 { 
     case "1": 
      cell.dayColor1.text = "" 
      cell.dayColor1.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor1.text = "" 
      cell.dayColor1.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor1.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor1.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor1.textColor = UIColor.white 
     } 

     cell.dayColor2.text = day2 
     switch day2 { 
     case "1": 
      cell.dayColor2.text = "" 
      cell.dayColor2.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor2.text = "" 
      cell.dayColor2.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor2.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor2.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor2.textColor = UIColor.white 
     } 

     cell.dayColor3.text = day3 
     switch day3 { 
     case "1": 
      cell.dayColor3.text = "" 
      cell.dayColor3.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor3.text = "" 
      cell.dayColor3.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor3.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor3.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor3.textColor = UIColor.white 
     } 

     cell.dayColor4.text = day4 
     switch day4 { 
     case "1": 
      cell.dayColor4.text = "" 
      cell.dayColor4.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor4.text = "" 
      cell.dayColor4.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor4.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor4.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor4.textColor = UIColor.white 
     } 

     cell.dayColor5.text = day5 
     switch day5 { 
     case "1": 
      cell.dayColor5.text = "" 
      cell.dayColor5.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor5.text = "" 
      cell.dayColor5.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor5.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor5.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor5.textColor = UIColor.white 
     } 

     cell.dayColor6.text = day6 
     switch day6 { 
     case "1": 
      cell.dayColor6.text = "" 
      cell.dayColor6.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor6.text = "" 
      cell.dayColor6.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor6.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor6.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor6.textColor = UIColor.white 
     } 

     cell.dayColor7.text = day7 
     switch day7 { 
     case "1": 
      cell.dayColor7.text = "" 
      cell.dayColor7.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
     case "X": 
      cell.dayColor7.text = "" 
      cell.dayColor7.backgroundColor = UIColor.red 
     case "E": 
      cell.dayColor7.backgroundColor = UIColor.lightGray 
     default: 
      cell.dayColor7.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1) 
      cell.dayColor7.textColor = UIColor.white 
     } 

     return cell 

    } else { 
     // PART 2 
     let cell2 = tableView.dequeueReusableCell(withIdentifier: "CustomCell2", for: indexPath) as! PaydayDetailCellA2 
     let (_, pointAmount) = tempDailyChoresSummary[indexPath.row] 

     cell2.dailyChoresNumber.text = "\(pointAmount)" 
     cell2.jobConsistencyBonusNumber.text = "\(pointAmount)" 
     cell2.previousUnpaidAmountsNumber.text = "\(pointAmount)" 
     cell2.dailyChoresSubtotalNumber.text = "\(pointAmount)" 

     return cell2 
    } 
} 

ich einen Block von Etiketten in einer benutzerdefinierten Tableview Zelle haben. Ich möchte, dass die Hintergrundfarbe der Beschriftungen abhängig von den Daten in einem externen Array die Farbe ändert. Ich habe ein Bild von dem, was es aussieht, angehängt (wenn das hilft). Here is the picture of the tableview with colored labels.

Oder gibt es eine einfachere Möglichkeit zu tun, was ich versuche zu tun?

Antwort

0

Dieser Ansatz kann den Code in der Tabellenfunktion reduzieren. Denken Sie daran, das ist ein Konzept. Sie können einige der Einstellungen für sich wiederholende Eigenschaften ausbrechen, indem Sie eine Erweiterung für UITableViewCell erstellen.

Erstellen Sie zunächst ein Protokoll

protocol Settable { 
    func setData(val: Int) 
} 

Dann

extension Settable { 
    func setData(jobDescription: String, jobSubtotal: String, cornerRadius: Float, maskToBounds: Bool, borderColor: CGColor, borderWidth: Float) { 
     self.tallyView.layer.cornerRadius = cornerRadius 
     self.tallyView.layer.masksToBounds = maskToBounds 
     self.tallyView.layer.borderColor = borderColor 
     self.tallyView.layer.borderWidth = borderWidth 
     self.jobDesc.text = jobDescription 
     self.jobSubtotal.text = jobSubtotal 
    } 
} 

Dann wird Ihr UITableViewCell

class PaydayNewCellA1 : UITableViewCell, Settable { 

} 
class PaydayNewCellB1 : UITableViewCell, Settable { 

} 
class PaydayNewCellC1 : UITableViewCell, Settable { 

} 

Dann erweitern eine Erweiterung machen Sie folgende in Ihrer Funktion tun können:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if (tableView == self.tableView1) { 
     if indexPath.section == 0 { 
      let cell = tableView1.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath) as! PaydayNewCellA1 
      // Now use the function 
      cell.setData(jobDescription: "daily job \(indexPath.row + 1)", jobSubtotal: "50", cornerRadius: cell.tallyView.bounds.height/6.4, maskToBounds: true, borderColor: UIColor.lightGray.cgColor, borderWidth: 0.5) 
      return cell 
     } else { 
      let cell = tableView1.dequeueReusableCell(withIdentifier: "CustomCell2", for: indexPath) as! PaydayNewCellA2 
      return cell 
     } 
    } else if (tableView == tableView2) { 

    // rest of code etc 

} 
+0

Danke, aber wie ein vorheriger Benutzer bemerkt, ich hatte den falschen Code gepostet, und so löst Ihre Antwort, wenn auch ausgezeichnet, nicht mein Problem. Bitte beachten Sie den aktualisierten Code. (Ich habe es etwa zur selben Zeit gepostet, als Sie Ihre Antwort gepostet haben, und so haben Sie den aktualisierten Code nie gesehen). –

Verwandte Themen