2015-07-13 21 views
0

所以,我在我的UITableView中有幾個像刪除,塊等Swipe動作。我想添加標題來分隔我的兩個部分。所以,我添加了一個原型單元格,將其命名爲HeaderCell,然後進入視圖。我添加了一個名爲headerLabe的標籤。我的問題是,當我滑動行動時,標題單元格也在移動,這看起來很糟糕。我研究過,並找到了一個解決方案,只是返回單元格的contentView。但是,當我這樣做時,標籤沒有出現。我已經嘗試了十幾種不同的解決方案,並沒有任何成效,所以我轉向了SO。誰能幫我?當我滑動UITableViewCell時,標題視圖也會移動

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerCell : CustomHeaderTableViewCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderTableViewCell 

    if section == 0 { 
     headerCell.headerLabel.text = "Thank You's" 
    } else if section == 1 { 
     headerCell.headerLabel.text = "Conversations" 
    } 

    return headerCell.contentView 
} 

非常感謝。

回答

0

您可以使用部分標題作爲@ozgur建議。如果您仍想使用單元格。

請參閱此數據源的方法

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    if indexPath = YourHeaderCellIndexPath{ 
     return false 
    } 
    return true 
} 
-1
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    //UITableViewCell 

    let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! SecJobCCHeaderTableViewCell 

    // Cell Rect 
    var cellRect : CGRect = headerCell.frame 
    cellRect.size.width = screenBounds.width 

    // Header Footer View 
    let headerFooterView = UITableViewHeaderFooterView(frame : cellRect) 

    //Adding Gesture 
    let swipeGestRight = UISwipeGestureRecognizer(target: self, action:#selector(AddSecJobCostCentreViewController.draggedViewRight(_:))) 
    swipeGestRight.enabled = true 
    swipeGestRight.direction = UISwipeGestureRecognizerDirection.Right 
    headerFooterView.addGestureRecognizer(swipeGestRight) 

    // Update Cell Rect 
    headerCell.frame = cellRect 

    // Add Cell As Subview 
    headerCell.tag = 1000 
    headerFooterView.addSubview(headerCell) 

    // Return Header Footer View 
    return headerFooterView 
} 

func draggedViewRight(sender:UISwipeGestureRecognizer) { 

    // Swipe Gesture Action 
    let currentHeaderView = sender.view?.viewWithTag(1000) as! SecJobCCHeaderTableViewCell 


} 
0

檢查下列方法

在UIViewController使用以下

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 60 

    } 


    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

      let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! WishListHeaderCell 
      headerCell.lblTitle.text = cartsData.stores_Brand_Name 
      let imgVw = UIImageView() 
      imgVw.frame = CGRectMake(8, 18, 25, 25) 
      imgVw.image = UIImage(named: "location.png") 

      let title = UILabel() 
      title.frame = CGRectMake(41, 10, headerCell.viwContent.frame.width - 49, 41) 
      title.text = cartsData.stores_Brand_Name 
      title.textColor = UIColor.whiteColor() 
      headerCell.viwContent.addSubview(imgVw) 
      headerCell.viwContent.addSubview(title) 
      return headerCell.viwContent 

     } 

在你的UITableViewCell使用以下

import UIKit 

    class HeaderCell: UITableViewCell { 

     @IBOutlet weak var viwContent: UIView! 
     @IBOutlet weak var imgIcn: UIImageView! 
     @IBOutlet weak var lblTitle: UILabel! 


     override func awakeFromNib() { 
      super.awakeFromNib() 
      self.viwContent.backgroundColor = UIColor.grayColor() 

     } 

     override func setSelected(selected: Bool, animated: Bool) { 
      super.setSelected(selected, animated: animated) 

      // Configure the view for the selected state 
     } 

    } 
相關問題