2017-06-22 53 views
1

我試圖通過執行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上的第一行時,會拋出第三行中的值,並且它們全部混在一起。有誰能告訴我我做錯了什麼?

+2

您的問題是裏面有'cellForRowAt' indexPath.row'之間'沒有聯繫以及當你進入細節時被挖掘的那一排。這意味着您傳遞的數據與屏幕上最後一次更新的單元格相匹配。 –

+1

當您需要添加第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

+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 
}