我正试图通过执行瞄准来显示促销优惠券的详细信息,其中包含来自TableViewCel的信息到视图控制器中。
这是我在cellForRowAt indexPath方法中的代码,也是de Perform Segue功能:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let coupons = [couponImage1, couponImage2, couponImage3, couponImage4, couponImage5, couponImage6, couponImage7, couponImage8, couponImage9, couponImage10]
let couponsTitle = [couponTitle1, couponTitle2, couponTitle3, couponTitle4, couponTitle5, couponTitle6, couponTitle7, couponTitle8, couponTitle9, couponTitle10]
let couponsDescription = [couponDesc1, couponDesc2, couponDesc3, couponDesc4, couponDesc5, couponDesc6, couponDesc7, couponDesc8, couponDesc9, couponDesc10]
let couponsCategory = [couponCat1, couponCat2, couponCat3, couponCat4, couponCat5, couponCat6, couponCat7, couponCat8, couponCat9, couponCat10]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
cell.couponImg.image = coupons[indexPath.row]
cell.couponTitle.text = couponsTitle[indexPath.row]
cell.couponDescription.text = couponsDescription[indexPath.row]
cell.couponCategory.text = couponsCategory[indexPath.row]
if indexPath.row == 0 {
self.sendCouponImg = couponImage1
self.sendCouponTitle = couponTitle1
self.sendCouponDesc = couponDesc1
self.sendCouponCat = couponCat1
} else if indexPath.row == 1 {
self.sendCouponImg = couponImage2
self.sendCouponTitle = couponTitle2
self.sendCouponDesc = couponDesc2
self.sendCouponCat = couponCat2
} else if indexPath.row == 2 {
self.sendCouponImg = couponImage3
self.sendCouponTitle = couponTitle3
self.sendCouponDesc = couponDesc3
self.sendCouponCat = couponCat3
} else if indexPath.row == 3 {
self.sendCouponImg = couponImage4
self.sendCouponTitle = couponTitle4
self.sendCouponDesc = couponDesc4
self.sendCouponCat = couponCat4
} else if indexPath.row == 4 {
self.sendCouponImg = couponImage5
self.sendCouponTitle = couponTitle5
self.sendCouponDesc = couponDesc5
self.sendCouponCat = couponCat5
} else if indexPath.row == 5 {
self.sendCouponImg = couponImage6
self.sendCouponTitle = couponTitle6
self.sendCouponDesc = couponDesc6
self.sendCouponCat = couponCat6
} else if indexPath.row == 6 {
self.sendCouponImg = couponImage7
self.sendCouponTitle = couponTitle7
self.sendCouponDesc = couponDesc7
self.sendCouponCat = couponCat7
} else if indexPath.row == 7 {
self.sendCouponImg = couponImage8
self.sendCouponTitle = couponTitle8
self.sendCouponDesc = couponDesc8
self.sendCouponCat = couponCat8
} else if indexPath.row == 8 {
self.sendCouponImg = couponImage9
self.sendCouponTitle = couponTitle9
self.sendCouponDesc = couponDesc9
self.sendCouponCat = couponCat9
} else if indexPath.row == 9 {
self.sendCouponImg = couponImage10
self.sendCouponTitle = couponTitle10
self.sendCouponDesc = couponDesc10
self.sendCouponCat = couponCat10
}
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "couponsDetails"){
let viewController:DetailCouponsViewController = segue.destination as! DetailCouponsViewController
viewController.getCouponsImage = self.sendCouponImg
viewController.getCouponsTitle = self.sendCouponTitle
viewController.getCouponsDescription = self.sendCouponDesc
viewController.getCouponsCategory = self.sendCouponCat
}
}
这是第二视图控制器的代码:
class DetailCouponsViewController: UIViewController {
@IBOutlet weak var couponCategory: UILabel!
@IBOutlet weak var couponTitle: UILabel!
@IBOutlet weak var couponImage: UIImageView!
@IBOutlet weak var couponDescription: UITextView!
@IBOutlet weak var couponLongDescription: UITextView!
@IBOutlet var starButtons: [UIButton]!
var getCouponsTitle : String = ""
var getCouponsDescription : String = ""
var getCouponsCategory : String = ""
var getCouponsImage : UIImage? = nil
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(false)
self.couponCategory.text = getCouponsCategory
self.couponDescription.text = getCouponsDescription
self.couponLongDescription.text = getCouponsDescription
self.couponTitle.text = getCouponsTitle
self.couponImage.image = getCouponsImage
}
我的问题是详细视图控制器上出现的数据不正确,当我点击HomewTableViewCell上的第一行抛出第三行的值时,它们都混淆了。有谁能告诉我,我做错了什么?
答案 0 :(得分:1)
cellForRowAt
函数的目的是创建一个单元格对象,该对象是一个视图,配置它然后显示它们。向下滚动时,将调用此方法,以便准备然后显示下一个单元格。因此,如果您在此函数中设置变量,则这些变量中的值来自tableView准备显示的最后一个单元格。
例如,表视图有10行,其变量foo值从0到9.您的屏幕只能显示3个单元格。加载视图后,表视图将调用cellForRow
三次以准备要显示的单元格0 1和2的视图。由于单元格2最后被处理,因此您的本地变量将具有值2.然后当您单击第一个单元格时,您将值2传递到下一个视图控制器。
为了使其正确,出于此功能的目的,首先需要将数组的定义移到外部,因为每次准备单元格时都不想重新创建。另外,单击单元格时还需要从其他函数访问数组。鉴于您的数组数据来自http请求,首先在外部创建变量为空数组。
var coupons: [String] = []
var couponsTitle: [String] = []
var couponsDescription: [String] = []
var couponsCategory: [String] = []
然后在您的HTTP回调中,向这些数组添加值,然后调用tableView.reloadData()以使用来自服务器的数据重新加载表
func yourHTTPCallBack() {
//coupons = [foo1, foo2 ...]
tableView.reloadData()
}
在cellForRow
中,您只显示单元格,而不是将任何值保存到局部变量。因为点击还没有发生!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
cell.couponImg.image = coupons[indexPath.row]
cell.couponTitle.text = couponsTitle[indexPath.row]
cell.couponDescription.text = couponsDescription[indexPath.row]
cell.couponCategory.text = couponsCategory[indexPath.row]
return cell
}
然后说你想在点击一个单元格时触发segue,从表格视图委托中使用这个功能
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.sendCouponImg = coupons[indexPath.row]
self.sendCouponTitle = couponsTitle[indexPath.row]
self.sendCouponDesc = couponsDescription[indexPath.row]
self.sendCouponCat = couponsCategory[indexPath.row]
//Do your segue here
}