2017-09-13 10 views
1

日付と項目のテキストに基づいてセパレータ(UIView)の色を設定しようとしています。セクションの日付が今日の文字列の前にある場合、セパレータの色はグレーに変わり、jobDetail.textが「キャンセル」のときにセパレータの色は赤に変わります。出来ますか?Swift 3の日付とテキストに基づいてセパレータ(UIView)の色を設定します。

これはコードですが、私はすべてのセパレータの色を変更しましたが、具体的ではありません。セクションに基づいてセパレータを変更する方法は?またはセパレータを作るための他の方法?

let section = ["Mon 18 May", "Tue 19 May", "Fri 22 May"] 

let items = [["Work", "Work", "Cancel"], ["Off Day"], ["Work", "Work", "Work", "mushrooms"]] 


func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return self.section[section] 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return self.section.count 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.items [section].count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "rosterId", for: indexPath) as! RosterCell 

    cell.timeLabel.text = "10:00AM\n15:00PM" 
    cell.jobDetail?.text = self.items[indexPath.section][indexPath.row] 

    let today = "19 May 2017" 
    let headerDate = "18 May 2017" 

    if headerDate < today{ 
     cell.separator.backgroundColor = UIColor(red: 1, green: 0.8314, blue: 0.1569, alpha: 1) 
    }else if cell.jobDetail.text == "Cancel"{ 
     cell.separator.backgroundColor = UIColor.red 
    } 
    return cell 
} 

アプリのレイアウト enter image description here

マイコード結果 enter image description here

+0

今日の日付と比較する必要がある日付形式は何ですか。 –

+0

https://github.com/MatthewYork/DateTools – Vakas

答えて

1

私はあなたがこれはこれを試して動作していない理由です、その文字列オブジェクトを比較していると思う: -

let today = Date() 
    let headerDate = "18 May 2017" 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "dd MMM yyyy" //Your date format 
    dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") 
//Current time zone 
    let date = dateFormatter.date(from: headerDate) //according to date 
    format your date string 
    print(date ?? "") 

    if today < date{ 
    cell.separator.backgroundColor = UIColor(red: 1, green: 0.8314, 
blue: 0.1569, alpha: 1) 
    }else if cell.jobDetail.text == "Cancel"{ 
      cell.separator.backgroundColor = UIColor.red 
} 
+0

を使用しないでください。ただし、それでもセパレータの色はすべて変更されますか?どのようにインデックスのパスまたはセクションに基づいて区切りを変更する..? –

+0

必要に応じて色を設定します。> headerDate //色を設定します。その他の場合はdate Pushpendra

+0

他の方法で修正しましたが、あなたのコードに触発されました。私のコードをyrの回答に更新できますか? –

0

注:Alwayあなたの文字列を日付に変換してからcそれを変えてください

let today = "19 May 2017" // Date() for today 
    let headerDate = "18 May 2017" 

    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "dd MMM yyyy" 
    let todayDate = dateFormatter.date(from: today)! 

    let headerDateDate = dateFormatter.date(from: headerDate)! 
    if headerDateDate < todayDate{ 
     cell.separator.backgroundColor = UIColor(red: 1, green: 0.8314, blue: 0.1569, alpha: 1) 
    }else if cell.jobDetail.text == "Cancel"{ 
     cell.separator.backgroundColor = UIColor.red 
    } 
    return cell 
関連する問題