ios - 通过 Perform Segue 将特定值从 TableViewCell 发送到 ViewController

标签 ios uitableview swift3 uistoryboardsegue

我正在尝试通过执行 Segue 将带有来自 TableViewCel 的信息的促销优惠券的详细信息显示到 View Controller 中。

这是我在 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
    }
}

这是第二个 View Controller 的代码:

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
}

我的问题是详细信息 View Controller 上显示的数据不正确,当我单击 HomewTableViewCell 上的第一行时抛出第三行出现的值,并且它们都混在一起了。有谁能告诉我我做错了什么吗?

最佳答案

cellForRowAt 函数的目的是创建一个作为 View 的单元格对象,配置它然后显示它们。当您向下滚动时,将调用此方法以便准备并显示下一个单元格。因此,如果您像这样在此函数中设置变量,则这些变量中的值来自 tableView 准备显示的最后一个单元格。

例如, TableView 有 10 行,变量 foo 值从 0 到 9。您的屏幕只能显示 3 个单元格。加载 View 时, TableView 将调用 cellForRow 三次,为要显示的单元格 0 1 和 2 准备 View 。由于最后处理了单元格 2,根据您的代码,您的局部变量的值为 2。然后当您单击第一个单元格时,您将值 2 传递给下一个 View Controller 。

为了使它正确,为了这个函数的目的,首先你需要将数组的定义移到外面,因为你不想每次准备一个单元格时都重新创建。此外,您还需要在单击单元格时从另一个函数访问数组。鉴于你的数组数据来自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,使用 TableView 委托(delegate)中的这个函数

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
}

关于ios - 通过 Perform Segue 将特定值从 TableViewCell 发送到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707675/

相关文章:

ios - Apple Swift 教程 : Rating buttons layout

ios - 如何以编程方式在 UITableViewCells 中居中图像?

swift - 如何将完整的 UserDefaults 列表放入 tableview - Swift 3

iphone - CIFilter 集成仅适用于 CISepiaTone

iphone - 一旦 App 进入后台,AVAssetWriter 就会失败

iOS : how to enable/disable textview's editable property on click of button

c# - 如何从 TableView 传递数据? (Xamarin.iOS)

swift - Swift 4.0.2 中的 NSObject setValuesForKeys 编码投诉错误

ios - 如何在 UIView swift 3 的两侧添加圆角

ios - 无法对 SCNPlane 阵列使用多个图像