如何修改cellForRowAt Swift之外的单元格

时间:2017-07-12 16:45:24

标签: ios swift uitableview uidatepicker

目前我的代码是这样的,我在单元格中设置了datePicker

代码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时,使用回调setTimeInDatePicker来修改单元格中的datePicker它似乎没有工作

请告诉我可能出错的地方。

代码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()

}

2 个答案:

答案 0 :(得分:1)

看起来你正在混淆completionHandlers与其他东西。

通常在任务异步时使用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 = { [unowned self](_) in
        // Use unowned to make sure you don't create a retain-cycle
        self.setTimeInDatePicker(time: endTime!, cell: cell)
    } 
    return cell
}


func setTimeInDatePicker(time: String, cell: DailyTimesTableViewCell) {

    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)")

    }
    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)")

    }

}

注意endTimeTapped闭包如何使[unowned self]的用户防止保留周期。

您还可以注意到已删除beginUpdate / endUpdate。原因是您没有做任何影响相关单元格高度的事情,您只是更改日期,因此您不需要触发刷新tableView,因为更改将自动应用。 / p>

检查这是否适合您?!

N.B。单元格是一个类,你作为参数发送给setTimeInDatePicker的是对它的引用,所以你真的不需要返回任何东西,所以你已经拥有它。

答案 1 :(得分:1)

如果我理解正确,你只是想在一些回调之后或在其他一些功能中弄乱一个单元格。您可以随时使用tableView.cellForRow(at: IndexPath)来实现此目标。请注意,此函数返回UITableViewCell而不是您拥有的任何自定义单元格类型,因此您必须将其强制转换为(cell as? YourCustomCellClass)?.whateverFunctionOrPropertyHere。想想您希望如何传递信息 - 在单元格上设置完成块并不是一个坏主意,但还有其他方法可以实现。