2014-07-23 3 views
8

मुझे क्लिक करने पर मेरी तालिका की एक पंक्ति को आकार बदलना होगा। मैं यह कैसे कर सकता हूं? कोई मेरी मदद कर सकता है?क्लिक किए जाने पर एकल सेल की ऊंचाई को कैसे बदला जाए?

मेरा विचार नियंत्रक वर्ग:

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet var daysWorkPointTable: UITableView 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil) 

     self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell") 
    } 

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat { 
     return 75 
    } 

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
     var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell 

     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

    } 
} 

उत्तर

31

सबसे पहले आप एक संपत्ति में वर्तमान में चयनित सेल के indexPath का ट्रैक रखने के लिए है:,

var selectedCellIndexPath: NSIndexPath? 

यह एक वैकल्पिक होना चाहिए तुम हो सकता है, क्योंकि कोई सेल चयनित नहीं है।

let selectedCellHeight: CGFloat = 88.0 
let unselectedCellHeight: CGFloat = 44.0 

अब आप tableView(_:, heightForRowAtIndexPath:) लागू करना: आप की जाँच करने के लिए है अपने tableView(_:, didSelectRowAtIndexPath:) विधि में

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if selectedCellIndexPath == indexPath { 
     return selectedCellHeight 
    } 
    return unselectedCellHeight 
} 

अब अगला चयनित और अचयनित किया गया राज्य के लिए ऊंचाइयों की घोषणा (मूल्यों जो कुछ भी करने के लिए आप चाहते हैं बदल) की सुविधा देता है चयनित पंक्ति या एक अचयनित पंक्ति मौसम उपयोग किया गया है:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath { 
     selectedCellIndexPath = nil 
    } else { 
     selectedCellIndexPath = indexPath 
    } 

    tableView.beginUpdates() 
    tableView.endUpdates() 

    if selectedCellIndexPath != nil { 
     // This ensures, that the cell is fully visible once expanded 
     tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true) 
    } 
} 

beginUpdates() और 012,कॉल आपको एनिमेटेड ऊंचाई परिवर्तन दे रहे हैं।

आप ऊंचाई परिवर्तन एनीमेशन की अवधि बदलने के लिए आप एक एनीमेशन ब्लॉक UIView.animationWithDuration(...) में beginUpdates() और endUpdates() कॉल लपेट और जो कुछ भी मूल्य तुम चाहते हो के लिए सेट कर सकते हैं।

आप this sample project देख सकते हैं जो इस कोड को क्रिया में प्रदर्शित करता है।

+0

अरघ, बस पढ़ लें कि आप इसे केवल एक ही सेल पर काम करना चाहते हैं। मेरा कार्यान्वयन थोड़ा अधिक है, क्योंकि यह किसी भी कोशिका के लिए काम करता है। –

+0

अगर मैं घोषित किया गया है तो सत्यापन में चयनित CellIndexPath कारण को मान निर्दिष्ट नहीं कर सकता, इसलिए मैंने इसे var को बदलने की कोशिश की, लेकिन त्रुटि के कारण जारी रखें क्योंकि मैं NSIndexPath को शून्य निर्दिष्ट नहीं कर सकता। क्या आप मेरी मदद कर सकते हैं? मैं क्या गलत कर रहा हूं? –

+0

मैंने असाइनमेंट को स्वयं को स्पष्ट रूप से असाइन करने के लिए बदल दिया। चयन किया गया CellIndexPath। यह अब काम करना चाहिए, उम्मीद है;)। –

संबंधित मुद्दे

 संबंधित मुद्दे