目前我的代碼是這樣的,當我在小區設置一個datepicker如何修改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基於某些邏輯,因此當我嘗試下面的代碼B時,使用回調修改單元格中的datePicker setTimeInDatePicker
似乎並不奏效。
請指教,我可能會出錯。
代碼B:不工作
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