Ios 通过执行Segue将特定值从TableViewCell发送到ViewController

Ios 通过执行Segue将特定值从TableViewCell发送到ViewController,ios,uitableview,swift3,uistoryboardsegue,Ios,Uitableview,Swift3,Uistoryboardsegue,我试图通过执行Segue将促销优惠券的详细信息显示在视图控制器中,其中包含来自TableViewCel的信息 这是我在cellForRowAt indexPath方法中的代码,也是反执行Segue函数: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let coupons = [couponImage1, couponImage2,

我试图通过执行Segue将促销优惠券的详细信息显示在视图控制器中,其中包含来自TableViewCel的信息

这是我在cellForRowAt indexPath方法中的代码,也是反执行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上的第一行时,显示在“详细信息”视图控制器上的数据不正确,这会抛出第三行的值,并且这些值都混淆了。有人能告诉我我做错了什么吗?

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
}

您的问题是
indexPath.row
内部的
cellForRowAt
与转到详细信息时点击的行之间没有连接。这意味着您传递的数据与屏幕上上次更新的单元格匹配。当您需要添加第20张或第100张优惠券时,您会怎么做?这是不可伸缩的。不要对信息使用4个数组-而是使用这些属性创建优惠券类或结构。您已经获得了一个JSON对象数组,因此将这些对象映射到
优惠券
s,使用优惠券实例数组作为TableView
数据源
,然后基于
couponArray[indexPath.row]
设置TableCell属性。要传递数据,请在两个VCs中添加
selected优惠券
属性。在
didSelectRowAtIndexPath
中设置表视图-然后在序列中只设置
destinationVC.toucon=self.selectedtoucon
;非常感谢你的回答,你完全正确,这对我帮助很大!