2012-01-30 10 views
7

मैं डिलीट बटन दिखाए बिना एनीमेशन के सेल को मिटाने के लिए टेबल व्यू सेल में कहीं भी बाएं या दाएं स्वाइप करने में सक्षम होना चाहता हूं। मैं यह कैसे कर सकता हूँ?कोई हटाए गए बटन वाले सेल को हटाने के लिए UITableViewCell में कहीं भी बाएं या दाएं स्वाइप करें?

+0

यदि आप कर सकते हैं, तो अपना कोड पोस्ट करें? –

उत्तर

6

मैंने इसे आजमाया और कार्यान्वित नहीं किया है, लेकिन मैं इसे एक शॉट दूंगा। सबसे पहले, एक कस्टम UITableViewCell

  • एक indexPath, नोट बनाने के लिए, और यह 2 गुण है कि आप tableView को

    1. एक संदर्भ का उपयोग कर सकते उस में किया जा रहा है डालते हैं। TableView में सेल खोजने के लिए। (जब भी आप सभी कक्षों के लिए सेल हटाते हैं, तो इसे अपडेट करने की आवश्यकता होती है)

    अपने cellForRowAtIndexPath: में, जहां आप कस्टम सेल बनाते हैं, इन गुणों को सेट करें। इसके अलावा सेल

    cell.tableView=tableView; 
    cell.indexPath=indexPath; 
    UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
    [cell addGestureRecognizer:swipeGestureRecognizer]; 
    [swipeGestureRecognizer release]; 
    

    सुनिश्चित करें कि इशारा केवल क्षैतिज स्वाइप प्राप्त करता है बनाने के लिए एक UISwipeGestureRecognizer जोड़ें।

    -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
    { 
        if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
         ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
         ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
    } 
    

    अपने deleteCell:

    -(void) deleteCell:(UIGestureRecognizer*)gestureRec 
    { 
        UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
        CustomCell *cell=[swipeGestureRecognizer view]; 
        UITableView *tableView=cell.tableView; 
        NSIndexPath *indexPath=cell.indexPath; 
        //you can now use these two to perform delete operation 
    } 
    
  • +0

    भूलें कि आपको इशारा पहचानकर्ता के लिए दिशा निर्धारित करने की आवश्यकता है जब इसे सेल में जोड़ने के लिए पहली बार शुरू किया जाए। ऐसा कुछ: swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris

    +0

    ऐप्पल, आपको '(शून्य) रद्द करने के लिए नाराज हैप्रतिफॉर्मफॉर्मफॉर्म्सथिथ लक्ष्य: (आईडी) एक लक्ष्य चयनकर्ता: (एसईएल) एक चयनकर्ता'। –

    -2

    इसके लिए आपको अपनी परियोजना में यह कोड जोड़ना होगा।

    // Override to support conditional editing of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
        // Return YES if you want the specified item to be editable. 
        return YES; 
    } 
    
    // Override to support editing the table view. 
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
        if (editingStyle == UITableViewCellEditingStyleDelete) { 
         //add code here for when you hit delete 
        }  
    } 
    

    वरना मोर की जानकारी यू इस लिंक के माध्यम से जा सकते हैं enter link description here

    +0

    विवेक2012 और आईविल - आपके उत्तर के लिए धन्यवाद लेकिन मैं एक डिलीट बटन नहीं दिखाना चाहता हूं, स्टैक ओवरफ्लो में पहले से ही उत्तर हैं। मैं चाहता हूं कि सेल को उस समय से मिटा दिया जाए जब उसे स्वाइप बाएं या दाएं इशारे मिल जाए।कृपया मेरा विवरण पढ़ें – carbonr

    -1

    ,

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 
    

    इन दोनों तरीकों को लागू करता है, तो UITableViewCellEditingStyle UITableViewCellEditingStyleDelete है, तो कर sth

    साथ अपने सेल को मिटाने के लिए
    - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 
    

    यह सब कुछ है।

    3

    समाधान @MadhavanRP द्वारा पोस्ट की में काम करता है, लेकिन यह अधिक जटिल की तुलना में यह करने की जरूरत है। आप एक सरल दृष्टिकोण ले सकते हैं और एक इशारा पहचानकर्ता बना सकते हैं जो तालिका में होने वाली सभी स्वाइप को संभालता है, और उसके बाद यह निर्धारित करने के लिए स्वाइप का स्थान प्राप्त करें कि उपयोगकर्ता किस सेल को स्वाइप करता है।

    सेट करने के लिए

    इशारा पहचानकर्ता:

    - (void)setUpLeftSwipe { 
        UISwipeGestureRecognizer *recognizer; 
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(swipeLeft:)]; 
        [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
        [self.tableView addGestureRecognizer:recognizer]; 
        recognizer.delegate = self; 
    } 
    

    कॉल viewDidLoad

    में इस विधि कड़ी चोट संभाल करने के लिए:

    - (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
        CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
        ... do something with cell now that i have the indexpath, maybe save the world? ... 
    } 
    

    ध्यान दें: अपने कुलपति इशारा पहचानकर्ता प्रतिनिधि को लागू करना चाहिए ।

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

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