言葉にするのが難しく、分かりづらい質問になってしまうと思います。
現在、スケジュールアプリを作成しております。
そこで、ユーザーが指定した「タイトル」「開始時刻」「終了時刻」をtableviewcellに表示させたいと思っています。
その際に「開始時刻」のみを昇順にし、「開始時刻」にセットで「終了時刻」と「タイトル」も付随させたいのですが、
開始時刻のみが昇順表示になるため、「タイトル」と「終了時刻」は登録した順番に表示されてしまいます。
SimulatorのScreenShotを添付いたします。
登録した順番
開始時刻:2:00 終了時刻:2:00 タイトル:2
開始時刻:1:00 終了時刻:1:00 タイトル:2
実現したい順番
開始時刻:1:00 終了時刻:1:00 タイトル:2
開始時刻:2:00 終了時刻:2:00 タイトル:1
下記にcellForRowAt内の現在のコードを記します。
その他に確認が必要な箇所のコードがありましたら、お申し付けください。
ご教示のほど、お願い申しあげます。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell func dateString(date: NSDate) -> String { let dateFormatter = DateFormatter() dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)! as Calendar! dateFormatter.locale = NSLocale.system dateFormatter.timeZone = NSTimeZone.system dateFormatter.dateFormat = "H:mm" let dateString: String = dateFormatter.string(from: date as Date) return dateString } let sortArray = dateArray1.sorted { $0 < $1 } strBox.append(dateString(date: sortArray[indexPath.row] as NSDate)) strBox2.append(dateString(date: dateArray2[indexPath.row] as NSDate)) dateInStr = dateString(date: sortArray[indexPath.row] as NSDate) dateInStr2 = dateString(date: dateArray2[indexPath.row] as NSDate) strCount = dateInStr.characters.count strCount2 = dateInStr2.characters.count if strCount < 5 { timeLabelText1 = " " + strBox[indexPath.row]! + " " } else { timeLabelText1 = strBox[indexPath.row]! + " " } if strCount2 < 5 { timeLabelText2 = " " + strBox2[indexPath.row]! } else { timeLabelText2 = " " + strBox2[indexPath.row]! } let attrText = NSMutableAttributedString(string: timeLabelText1 + ">>>" + timeLabelText2) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(7, 3)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(0, 5)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(12, 5)) cell.timeLabel?.attributedText = attrText cell.titleLabel?.text = " " + titleArray[indexPath.row]! // 枠のカラー cell.titleLabel.layer.borderColor = UIColor.darkGray.cgColor // 枠の幅 cell.titleLabel.layer.borderWidth = 1.0 // 枠を角丸にする cell.titleLabel.layer.cornerRadius = 10.0 cell.titleLabel.layer.masksToBounds = true return cell
}