2015-10-13 41 views
0

我使用tableView,裏面有UIImage,UILabel和FloatRatingView。 FloatRatingView是一個UIView,星級評分,你可以評分1-5。 所以tableView有多個單元格,當我按FloatRatingView時我想獲得電影標題。Swift:從選定的UIView獲取tableView indextPath/textLabel

FloatRatingView有兩個委託方法。

func floatRatingView(ratingView: FloatRatingView, didUpdate rating: Float) { 
    } 
    func floatRatingView(ratingView: FloatRatingView, isUpdating rating: Float) { 

    } 

所以在我的自定義遠的TableCell我:

var delegate: FloatRatingViewDelegate? 
@IBOutlet weak var userRating: FloatRatingView!{ 
     didSet { 
      if userRating.rating>0 { 
      self.delegate!.floatRatingView(userRating, didUpdate: userRating.rating) 
     }} 
    } 

,並在TableViewController:

class MoviesViewController: PFQueryTableViewController ,FloatRatingViewDelegate { 



func floatRatingView(ratingView: FloatRatingView, isUpdating rating: Float) { 
     print("is updating") 

    } 
    func floatRatingView(ratingView: FloatRatingView, didUpdate rating: Float) { 
     print("did update") 
    } 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? { 

     let cellIdentifier:String = "cell" 

     var cell:TableCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? TableCell 

     if(cell == nil) { 
      cell = TableCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier) 
     } 
     cell?.delegate = self 

回答

2

。假定該FloatRatingView裏面的細胞,最簡單的方法,其一是:

  • 添加一個委託協議,以你的類中,實現在類FloatRatingViewDelegate和回調轉發到細胞委託。在視圖控制器中實現單元委託。
  • 公開您的單元類的FloatRatingView屬性,並將其委託直接設置到視圖控制器(這將需要實現FloatRatingViewDelegate)。 。遍歷可見細胞,並找到浮動額定值視圖是其中一個

然而,更現代的解決辦法是:

  • 替換功能性質委託協議(var didUpdate: (Float -> Void)?),設置那些在視圖控制器中出列你的單元格時。在那裏,您可以直接關閉indexPath參考。

這將導致整體代碼更少。

+0

如何將回調轉發給單元委託? – PoolHallJunkie

+0

我更新了我的代碼,但似乎我錯過了它背後的邏輯 – PoolHallJunkie

相關問題