通过 Perform Segue 将特定值从 TableViewCell 发送到 ViewController
Sending specific values from a TableViewCell to a ViewController by Perform Segue
我正在尝试通过执行 Segue 将带有来自 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
}
我的问题是出现在 Details View Controller 上的数据不正确,当我单击 HomewTableViewCell 上的第一行时抛出第三行出现的值,而且它们都混在一起了。有谁能告诉我我做错了什么吗?
cellForRowAt
函数的目的是创建一个作为视图的单元格对象,配置它然后显示它们。当您向下滚动时,将调用此方法以便准备并显示下一个单元格。因此,如果您像这样在此函数中设置变量,则这些变量中的值来自 tableView 准备显示的最后一个单元格。
例如,table 视图有 10 行,变量 foo 值从 0 到 9。您的屏幕只能显示 3 个单元格。当您的视图加载时,您的 table 视图将调用 cellForRow
三次以准备要显示的单元格 0 1 和 2 的视图。由于最后处理了单元格 2,从您的代码来看,您的局部变量的值为 2。然后当您单击第一个单元格时,您将值 2 传递给下一个视图控制器。
为了使它正确,为了这个函数的目的,首先你需要将数组的定义移到外面,因为你不想每次准备一个单元格时都重新创建。此外,您还需要在单击单元格时从另一个函数访问数组。鉴于你的数组数据来自http请求,首先在外面创建变量为空数组。
var coupons: [String] = []
var couponsTitle: [String] = []
var couponsDescription: [String] = []
var couponsCategory: [String] = []
然后在您的 HTTP 回调中,为这些数组添加值,然后调用 tableView.reloadData() 以使用服务器
中的数据重新加载 table
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,从 table view 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
}
我正在尝试通过执行 Segue 将带有来自 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
}
我的问题是出现在 Details View Controller 上的数据不正确,当我单击 HomewTableViewCell 上的第一行时抛出第三行出现的值,而且它们都混在一起了。有谁能告诉我我做错了什么吗?
cellForRowAt
函数的目的是创建一个作为视图的单元格对象,配置它然后显示它们。当您向下滚动时,将调用此方法以便准备并显示下一个单元格。因此,如果您像这样在此函数中设置变量,则这些变量中的值来自 tableView 准备显示的最后一个单元格。
例如,table 视图有 10 行,变量 foo 值从 0 到 9。您的屏幕只能显示 3 个单元格。当您的视图加载时,您的 table 视图将调用 cellForRow
三次以准备要显示的单元格 0 1 和 2 的视图。由于最后处理了单元格 2,从您的代码来看,您的局部变量的值为 2。然后当您单击第一个单元格时,您将值 2 传递给下一个视图控制器。
为了使它正确,为了这个函数的目的,首先你需要将数组的定义移到外面,因为你不想每次准备一个单元格时都重新创建。此外,您还需要在单击单元格时从另一个函数访问数组。鉴于你的数组数据来自http请求,首先在外面创建变量为空数组。
var coupons: [String] = []
var couponsTitle: [String] = []
var couponsDescription: [String] = []
var couponsCategory: [String] = []
然后在您的 HTTP 回调中,为这些数组添加值,然后调用 tableView.reloadData() 以使用服务器
中的数据重新加载 tablefunc 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,从 table view 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
}