2015-10-13 44 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

相关问题