我試圖通過執行Segue來將來自TableViewCel的信息的促銷優惠券的詳細信息顯示到View Controller中。通過執行Segue從TableViewCell發送特定值到ViewController
這是我在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
}
我的問題是,出現在Details View Controller上的數據不是正確的,當我點擊HomewTableViewCell上的第一行時,會拋出第三行中的值,並且它們全部混在一起。有誰能告訴我我做錯了什麼?
您的問題是裏面有'cellForRowAt' indexPath.row'之間'沒有聯繫以及當你進入細節時被挖掘的那一排。這意味着您傳遞的數據與屏幕上最後一次更新的單元格相匹配。 –
當您需要添加第20張或第100張優惠券時,您會做什麼?這不可擴展。不要爲信息使用4個數組 - 創建一個Coupon類或Struct與這些屬性代替。您已經獲得了一組JSON對象,因此將它們映射到'Coupon's,使用Coupon實例數組作爲TableView'dataSource',然後基於'couponArray [indexPath.row]'設置TableCell屬性。要傳遞數據,請在兩個VC中添加'selectedCoupon'屬性。將TableView設置爲'didSelectRowAtIndexPath' - 然後在segue中設置'destinationVC.coupon = self。selectedCoupon'; – mc01
非常感謝您的回答,您完全正確,幫助我很多! –