2017-08-15 12 views
16

मेरा शीर्षक कहता है कि मैं UITableView की पहली पंक्ति को बाएं से दाएं स्वाइप करना चाहता हूं जब उपयोगकर्ता ViewController पर आएगा।स्पर्श के बिना UITableViewCell स्वाइप करें

मेरे ViewController में मेरे पास एक UITableView है, प्रत्येक पंक्ति में दो बटन "अधिक" और "हटाएं" कार्रवाई होती है। नीचे दिए गए कोड को देखें

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
    } 
} 


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in 
     // Edit Button Action 
    } 
    deleteButton.backgroundColor = UIColor.red 

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in 
     // Delete Button Action 
    } 
    editButton.backgroundColor = UIColor.lightGray 

    return [deleteButton, editButton] 
} 

सभी अच्छे काम कर रहे हैं। लेकिन मैं चाहता हूं कि अंत उपयोगकर्ता इस ViewController पर पहली बार आता है, इसलिए वे सूचित करेंगे कि स्वाइप कार्रवाई उपलब्ध है ताकि वे इसे निष्पादित कर सकें।

प्रश्न है: मैं पहली पंक्ति के लिए स्वचालित रूप से बाएं और दाएं स्वाइप कैसे कर सकता हूं?

मैंने क्या किया है?

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell 
    UIView.animate(withDuration: 0.3, animations: { 
     cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0) 
    }) { (finished) in 
     UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: { 
      cell.transform = CGAffineTransform.identity 
     }, completion: { (finished) in 
     }) 
    } 
} 

उपरोक्त कोड स्वाइप/चलती सेल काम कर रही है लेकिन "हटाएं" और "अधिक" बटन प्रदर्शित नहीं करती है।

तो कृपया मुझे सही दिशा में मार्गदर्शन करें।

+0

आपका दृष्टिकोण बाएं और दाएं स्वाइप करने के लिए सही लगता है। हालांकि, मुझे यकीन नहीं है कि निर्मित बटनों को इस तरह हाइलाइट किया जा सकता है या नहीं। आप निश्चित रूप से क्या कर सकते हैं, 'UIView' को अपनी सामग्री के नीचे मूल नियंत्रण की तरह दिखाना है, उन्हें प्रकट करने के लिए स्वाइप करें, और उन्हें सेल से हटाएं/छुपाएं। यह उत्तर देखें: https://stackoverflow.com/questions/5301728/is-it-possible-to-programmatically-show-the-red-delete-button-on-a-uitableviewce – dirtydanee

+0

आप इस पुराने ढांचे का उपयोग कर सकते हैं: https://github.com/CEWendel/SWTableViewCell। फिर दृश्यमान में लिखें: [सेल शोराइटयूटीलिटीबटन्सएनिएटेड: हाँ]; –

+0

तो, आप क्या लक्ष्य रखते हैं कि पहली पंक्ति स्वचालित रूप से स्वाइप करें, ताकि आप संपादन बटन देख सकें। क्या वह सही है? –

उत्तर

6

मैं एक तरह से इसे प्राप्त करने के लिए मिला।

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if arrStudent.count > 0 { 
     let indexPath = NSIndexPath(row: 0, section: 0) 
     let cell = tblStudent.cellForRow(at: indexPath as IndexPath); 

     let swipeView = UIView() 
     swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height) 
     swipeView.backgroundColor = .clear 

     let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height)) 
     swipeEditLabel.text = "Edit"; 
     swipeEditLabel.textAlignment = .center 
     swipeEditLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want 
     swipeEditLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeEditLabel) 

     let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height)) 
     swipeDeleteLabel.text = "Delete"; 
     swipeDeleteLabel.textAlignment = .center 
     swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want 
     swipeDeleteLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeDeleteLabel) 

     cell!.addSubview(swipeView) 

     UIView.animate(withDuration: 0.60, animations: { 
      cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height) 
     }) { (finished) in 
      UIView.animate(withDuration: 0.60, animations: { 
       cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height) 

      }, completion: { (finished) in 
       for subview in swipeView.subviews { 
        subview.removeFromSuperview() 
       } 
       swipeView.removeFromSuperview() 
      }) 

     } 
    } 
} 

सेल के अंत में कस्टम UIView जोड़ें और इसे हटाने के बाद एनीमेशन किया जाता है।

  • आप के रूप में आप
  • जोड़ें लेबल (रों) पृष्ठभूमि रंग और शीर्षक के साथ चाहते हैं के रूप में आप चाहते हैं कस्टम दृश्य के फ्रेम सेट कर सकते हैं।

यहां आपको प्रत्येक कोड को viewDidAppear में कॉल करने के लिए कोड लिखना चाहिए, इसे केवल उपयोगकर्ता संकेत के लिए कॉल करना चाहिए।

+0

मैं आपकी तर्क आशा की कोशिश करूंगा कि यह मेरी मदद करेगा। – Johnty

+0

आपको बहुत बहुत धन्यवाद :) मैंने कोशिश की और सही काम करने के रूप में काम किया। धन्यवाद agin पुरुषों। – Johnty

+0

@ जॉनी आपको मदद करने में प्रसन्न हैं :) – Govaadiyo

1

बाईं ओर एक सेल को ट्रांसफॉर्म करने से एक्शन बटन दिखाई नहीं देगा।

आप सेल पर setEditing विधि को फोन करके संपादन मोड में है कि सेल भेजने की जरूरत है, अपने viewDidAppear में रखते:

let cell = posTblView.cellForRow(at: IndexPath(row: 0, section: 0)) 
cell.setEditing(true, animated: true) 
+0

कोशिश की लेकिन काम नहीं कर रहा। – Johnty

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