手动触发cellForRowAt中的didSelectRowAtIndexPath会导致实际的didSelectRowAtIndexPath委托方法中的单元格为空

时间:2019-04-11 08:13:02

标签: ios swift tableview

我正在尝试在单元格创建didSelectRowAtIndexPath中手动调用cellForRowAt,因为这将更新详细视图,而无需实际触摸该行,但是当我这样做时会出现nil错误在实际的委托方法didSelectRowAtIndexPath内的单元格定义上,而如果我不在cellForRowAt内调用它,它将按预期工作。我假设在单元格创建时实际上没有要读取的单元格,因此nil值。 然后如何在单元格创建时执行didSelectRowAtIndexPath内部的逻辑?我考虑过打开大括号,然后将逻辑放在这里就可以了,但是在那里不接受括号。

您能看到我是否正确实施了对didSelectRowAtIndexPath的呼叫?一如既往的感谢。 这些是功能:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "bookingCell", for: indexPath) as! BookingTableViewCell
        let booking = self.fetchedResultController?.object(at: indexPath)
        // Configure the cell...

        cell.cellId = booking!.bookingId

        cell.bookingId = booking!.bookingId
        print(booking!.bookingId)
        cell.bookingIdInfoLabel.text = booking!.bookingId

        cell.bookingDate = booking!.bookingDate
        cell.bookingDateInfoLabel.text = booking?.bookingDate

        cell.bookingStart = booking!.bookingStart
        cell.bookingStartInfoLabel.text = booking?.bookingStart

        cell.bookingEnd = booking!.bookingEnd
        cell.bookingEndInfoLabel.text = booking?.bookingEnd

        cell.bookingPrice = booking!.bookingPrice
        cell.worksDescription = booking!.worksList

        cell.customerName = booking!.customerName
        cell.customerNameInfoLabel.text = booking?.customerName

        cell.cellView.layer.cornerRadius = cornerRadius
        cell.cellView.clipsToBounds = true

        cell.bookingIdInfoLabel.layer.cornerRadius = cornerRadius
        cell.bookingIdInfoLabel.clipsToBounds = true

        cell.bookingDateInfoLabel.layer.cornerRadius = cornerRadius
        cell.bookingDateInfoLabel.clipsToBounds = true

        cell.bookingStartInfoLabel.layer.cornerRadius = cornerRadius
        cell.bookingStartInfoLabel.clipsToBounds = true

        cell.bookingEndInfoLabel.layer.cornerRadius = cornerRadius
        cell.bookingEndInfoLabel.clipsToBounds = true

        cell.customerNameInfoLabel.layer.cornerRadius = cornerRadius
        cell.customerNameInfoLabel.clipsToBounds = true

        // set the corresponding row for the selected time slot's booking as selected
        if cell.cellId == self.bookingId {
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.middle) // select timeslot's corresponding row
            self.tableView(self.bookingTableView, didSelectRowAt: indexPath)

        }
        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! BookingTableViewCell

        print("selected cell id is : \(cell.cellId!)")
        self.bookingIdInfoLabel.text = cell.bookingId
        self.bookingDateInfoLabel.text = cell.bookingDate
        self.bookingStartInfoLabel.text = cell.bookingStart
        self.bookingEndInfoLabel.text = cell.bookingEnd
        self.priceInfoLabel.text = cell.bookingPrice
        self.customerInfoLabel.text = cell.customerName
        self.worksDescriptionInfoTextVIew.text = cell.worksDescription

    }

2 个答案:

答案 0 :(得分:2)

创建一个新功能,您可以根据预订选择更改标签文本

func updateSelection(_ selectedBooking: Booking) {
    print("selected cell id is : \(booking.bookingId!)")
    self.bookingIdInfoLabel.text = booking.bookingId
    self.bookingDateInfoLabel.text = booking.bookingDate
    self.bookingStartInfoLabel.text = booking.bookingStart
    self.bookingEndInfoLabel.text = booking.bookingEnd
    self.priceInfoLabel.text = booking.bookingPrice
    self.customerInfoLabel.text = booking.customerName
    self.worksDescriptionInfoTextVIew.text = booking.worksList
}

didSelectRowAt中像这样调用此方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.updateSelection(self.fetchedResultController?.object(at: indexPath))
}

cellForRowAt

中调用相同的方法
if booking!.bookingId == self.bookingId {
    self.updateSelection(self.fetchedResultController?.object(at: indexPath))
}
    return cell
}

答案 1 :(得分:0)

这就是我的建议

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "bookingCell", for: indexPath) as! BookingTableViewCell
    let booking = self.fetchedResultController?.object(at: indexPath)
    cell.config(booking:booking)
    if booking.bookingId == self.bookingId {
        self.performChanges(forBooking:booking)
    }
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! BookingTableViewCell
    let booking = cell.booking
    self.performChanges(forBooking:booking)
}

func performChanges(booking:Booking) {
    //perform all the changes that you did in didSelectRow before
}

//Inside cell
var booking:Booking?
func config(booking:Booking) {
    self.booking = booking
    //set all the data and manipulate the ui here
}

未测试此代码,但希望您能理解。如果您需要更多信息或无法完全理解要做什么,请发表评论,我可以详细说明。