は現在、私のコードは、このようなものがあり、どうcellForRowAtスウィフト外のセルを変更する
コードAは
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("DailyTimesTableViewCell", owner: self, options: nil)?.first as! DailyTimesTableViewCell
cell.endTimeTapped = { (button) -> DailyTimesTableViewCell in
cell.datePicker.date = self.convertToDateObject(timeString: endTime as! String)
}
return cell
}
は、私が変更したい作品いくつかのロジックに基づいて、セル内のdatePickerので、コールバックsetTimeInDatePicker
を使用してセル内のdatePickerを変更するには、以下のコードBを試してみると動作していないようです。
私が間違っている可能性のあるアドバイスをしてください。
コードB:あなたが何か他のものとcompletionHandlersが混乱しているように思え
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("DailyTimesTableViewCell", owner: self, options: nil)?.first as! DailyTimesTableViewCell
cell.endTimeTapped = { (button) -> DailyTimesTableViewCell in
self.setTimeInDatePicker(time: endTime!, cell: cell){ modifiedCell in
return modifiedCell
}
}
return cell
}
func setTimeInDatePicker(time: String, cell: DailyTimesTableViewCell, completionHandler: @escaping(DailyTimesTableViewCell) -> DailyTimesTableViewCell) {
tableView.beginUpdates()
let row = cell.tag
let rows = self.timesArray.count
if self.startTimeHighlighted{
// Setting minimum startTime for datePicker
if row == 0{ // For first row
let minStartTimeString = "0:05 am"
cell.datePicker.minimumDate = self.convertToDateObject(timeString: minStartTimeString)
}
else if row > 0 { // For remaining rows
let endTimeStringOfPreviousCell = self.timesArray[row - 1]["endTime"]!
let endTimeObjectOfPreviousCell = self.convertToDateObject(timeString: endTimeStringOfPreviousCell)
cell.datePicker.minimumDate = endTimeObjectOfPreviousCell
}
// Setting maximum startTime for datepicker
let endTimeString = self.timesArray[row]["endTime"]!
let endTimeObject = self.convertToDateObject(timeString: endTimeString)
let maxStartTimeObject = endTimeObject.addingTimeInterval(-5 * 60.0) // removing 5mins
cell.datePicker.maximumDate = maxStartTimeObject
// Setting selected date in as startTime title
let dateString = self.convertDateToString(cell.datePicker.date)
cell.startTime.setTitle(dateString, for: .normal)
// Saving updated date to the timesArray
self.timesArray[row]["startTime"] = dateString
//print("endTime in DatePickerChanged: \(self.timesArray[row]["endTime"])")
print("timesArray when startTimeHighlighted: \(self.timesArray)")
completionHandler(cell)
}
else if self.endTimeHighlighted{
print("datePicker changed 3")
// Setting minimum endTime for datePicker
let startTimeString = self.timesArray[row]["startTime"]
let startTimeObject = self.convertToDateObject(timeString: startTimeString!)
let minEndTimeObject = startTimeObject.addingTimeInterval(5 * 60.0) // adding 5mins
cell.datePicker.minimumDate = minEndTimeObject
// Setting maximum endTime for datePicker
if rows == 1 || row + 1 == self.timesArray.count{ // For first and last row
let maxEndTimeString = "11:55 pm"
cell.datePicker.maximumDate = self.convertToDateObject(timeString: maxEndTimeString)
}
else{ // If more than one row
let nextCellStartTime = self.timesArray[row + 1]["startTime"]!
let nextCellStartTimeObject = self.convertToDateObject(timeString: nextCellStartTime)
cell.datePicker.maximumDate = nextCellStartTimeObject
}
// Setting selected date in as endTime title
let dateString = self.convertDateToString(cell.datePicker.date)
cell.endTime.setTitle(dateString, for: .normal)
// Saving updated date to the timesArray
self.timesArray[row]["endTime"] = dateString
print("timesArray when endTimeHighlighted: \(self.timesArray)")
completionHandler(cell)
}
tableView.endUpdates()
}
コールバッククロージャの仕組みがわからないようです。通常、何も返しません。 – vadian
メソッドがまったく呼び出されてもよろしいですか? – Thomas
また、tableViewのデータソースを更新し、その上で 'didSet'を使用して' tableView.reloadData() 'を呼び出すこともできます。そうすれば、あなたのデータにもアップデートを認識させることができます。 – Honey