5

मेरे पास PFQueryTableViewController है जिसमें मैं कुछ तालिका सीखने की कोशिश कर रहा हूं।दो क्रियाओं को करने के लिए UITableViewCell पर डबल और एकल टैप का पता लगाने

क्या मैं वर्तमान में है: tableView सेल एक नल didSelectRowAtIndexPath पर इसकी ऊंचाई बढ़ जाती है या मैं मूल रूप से एक टैप का उपयोग कर एक साथ performSegueWithIdentifier का उपयोग कर prepareForSegue के साथ अगले ViewController करने के लिए सेल से संबंधित डेटा segue कर सकते हैं।

नीचे दिए गए कोड में, यदि मैं "तालिका दृश्य कक्ष की ऊंचाई बढ़ाने के लिए कोड टैप करें" कोड पर टिप्पणी करता हूं, तो मैं डबल टैप का पता लगा सकता हूं। अन्यथा एक ही टैप ऊंचाई बढ़ाता है क्योंकि कोड अंदर है ब्लॉक का चयन करें।

मुझे क्या चाहिए: एकल टैप पर, सेल ऊंचाई बढ़ जाती है या घट जाती है। जबकि सेल ऊंचाई बढ़ जाती है, अगर मैं दो बार टैप करता हूं, तो इसे सेल डेटा को UIViewController पर सीगू करना चाहिए।

यदि नहीं, तो एकल टैप ऊंचाई को बढ़ा या घटाएगा।

var selectedCellIndexPath: NSIndexPath? 
var SelectedCellHeight = CGFloat() // currently set to 480.0 tableView height 
var UnselectedCellHeight = CGFloat() // currently set to 300.0 tableView unselected hight 
    .... 

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

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! 
    if cell == nil { 
     cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 
    } 

// Below is the double Tap gesture recognizer code in which 
// The single tap is working, double tap is quite difficult to get 
// as the cell height increases on single tap of the cell 

    let doubleTap = UITapGestureRecognizer() 
     doubleTap.numberOfTapsRequired = 2 
     doubleTap.numberOfTouchesRequired = 1 
     tableView.addGestureRecognizer(doubleTap) 

     let singleTap = UITapGestureRecognizer() 
     singleTap.numberOfTapsRequired = 1 
     singleTap.numberOfTouchesRequired = 1 
     singleTap.requireGestureRecognizerToFail(doubleTap) 
     tableView.addGestureRecognizer(singleTap) 

// Tap code to increases height of tableView cell 

    if let selectedCellIndexPath = selectedCellIndexPath { 
     if selectedCellIndexPath == indexPath { 
      self.selectedCellIndexPath = nil 
      cell.postComment.fadeIn() 
      cell.postCreatorPicture.alpha = 1 
      cell.postDate.fadeIn() 
      // cell.postTime.fadeIn() 
      cell.postCreator.fadeIn() 
     } else { 
      self.selectedCellIndexPath = indexPath 
     } 
    } else { 
     selectedCellIndexPath = indexPath 
    } 
    tableView.beginUpdates() 
    tableView.endUpdates() 
} 



func doubleTap() { 
     print("Double Tap") 
    } 

func singleTap() { 
     print("Single Tap") 
    } 

अन्य तरह से मैं यह काम है कर सकते हैं, अगर मैं didSelectRowAtIndexPath कोड के बाहर चयनित सेल की NSIndexPath प्राप्त कर सकते हैं, मैं: और डबल टैप अगले UIViewController

संहिता के सेल डेटा रूप में नीचे है segue चाहिए यदि आवश्यक कार्रवाई प्राप्त करने के लिए और अधिक करने के लिए मूल रूप से डबल टैप का उपयोग कर सकते हैं। क्या आप तालिका के बाहर NSIndexPath प्राप्त कर सकते हैं डिफ़ॉल्ट ब्लॉक देखें?

+0

आप प्रतिनिधि के साथ आपको सेल ऑब्जेक्ट पास कर सकते हैं और इंडेक्सपैथ ढूंढ सकते हैं: एनएसआईएनएक्सएक्सपाथ * इंडेक्सपाथ = [self.tableView indexPathForCell: cell]; – Mohamad

उत्तर

3

यह काम कर रहा है!

पहला: var customNSIndexPath = NSIndexPath() को परिभाषित करें और नीचे दिए गए कोड को didSelectRowAtIndexPath कोड ब्लॉक के अंदर जोड़ें।

customNSIndexPath = indexPath 
     print(customNSIndexPath) 

दूसरा: निकालें didSelectRowAtIndexPath से कोड "टैप कोड को tableView सेल की ऊंचाई बढ़ जाती है" और singleTap() समारोह में जोड़ें। और doubleTap() का उपयोग कर segue प्रदर्शन करें।

func doubleTap() { 
     print("Double Tap") 
     performSegueWithIdentifier("MainToPost", sender: self) 
    } 

    func singleTap() { 
     print("Single Tap") 
     var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! 
     if cell == nil { 
      cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 
     } 

     if let selectedCellIndexPath = selectedCellIndexPath { 
      if selectedCellIndexPath == customNSIndexPath { 
       self.selectedCellIndexPath = nil 

      } else { 
       self.selectedCellIndexPath = customNSIndexPath 
      } 
     } else { 
      selectedCellIndexPath = customNSIndexPath 
     } 
     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 

इस तरह आप सोने के बिना देर से काम कर रहे हैं। यह करना सबसे आसान काम था। धन्यवाद

नोट: अगर किसी के पास बेहतर समाधान है या यह कुछ xyz मामले में गलत है, तो कृपया टिप्पणी करें। यदि आपका बेहतर है तो यह वास्तव में दूसरों की मदद करेगा।

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