gpt4 book ai didi

ios - 在 cellForRowAt 中手动触发 didSelectRowAtIndexPath 会导致实际 didSelectRowAtIndexPath 委托(delegate)方法中的单元格为零

转载 作者:行者123 更新时间:2023-11-28 11:35:04 28 4
gpt4 key购买 nike

我正在尝试在单元格创建 cellForRowAt 中手动调用 didSelectRowAtIndexPath,因为这将更新详细 View 而无需实际触摸该行,但是当我这样做时所以我在实际委托(delegate)方法 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

}

最佳答案

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

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
}

关于ios - 在 cellForRowAt 中手动触发 didSelectRowAtIndexPath 会导致实际 didSelectRowAtIndexPath 委托(delegate)方法中的单元格为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55627717/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com