2012-01-24 16 views
72

मेरे पास UIViewController है जो TableViews प्रतिनिधि और डेटास्रोत प्रोटोकॉल लागू करता है। अब मैं कोशिकाओं को इशारा करने के लिए "हटाने के लिए स्वाइप" जोड़ना चाहता हूं।तालिका दृश्य में सेल को हटाने के लिए स्वाइप कैसे सक्षम करें?

मुझे इसके बारे में कैसे जाना चाहिए।

मैंने commitEditingStyle विधि का खाली कार्यान्वयन दिया है और संपादन संपत्ति को YES पर भी सेट किया है।

अभी भी स्वाइप सुविधा नहीं आ रही है।

अब क्या मुझे प्रत्येक सेल में अलग-अलग UISwipeGesture जोड़ने की आवश्यकता है?

या क्या मुझे कुछ याद आ रहा है?

+0

में कार्रवाई कर सकते हैं लिंक उल्लेख कर सकते हैं http://stackoverflow.com/questions/3309484/uitableviewcell- शो-डिलीट-बटन-ऑन-स्वाइप – Sharme

+2

मैंने प्रतिबद्ध एडिटिंग स्टाइल को ओवरराइड किया है और संपादन को YES पर सेट किया है और अब भी मैंने CanEditRowAtIndexPath को ओवरराइड कर दिया है। सामान्य संपादन नियंत्रण आते हैं जिस पर डिलीट बटन क्लिक करने पर क्लिक होता है। लेकिन स्वाइप करने पर नहीं! ऐसा इसलिए है क्योंकि मेरी कक्षा UIViewController का उप-वर्ग है और केवल प्रतिनिधि और डेटास्रोत विधियों को लागू करती है लेकिन UITableViewController की नहीं जो स्वाइप इशारा सक्षम करने के लिए कुछ कर रही हो। –

+0

मैंने कल UIViewController में एक आईआईओयूलेट के रूप में एक UITableView सेट अप किया है, और उसी पोस्ट के ठीक बाद मैंने दिखाया है, मैंने हटाने के लिए स्वाइप किया है। तो मैं कह सकता हूं कि आपको स्वाइप जेश्चर सक्षम करने की आवश्यकता नहीं है, हालांकि यदि आप वास्तव में इसे किसी अन्य तरीके से काम नहीं कर पा रहे हैं, तो आपको इसका सहारा लेना पड़ सकता है। – gurooj

उत्तर

51

यदि आपको सेल स्वाइप पर डिलीट बटन दिखाना है तो आपको editing:YES सेट करने की आवश्यकता नहीं है। आपको tableView:canEditRowAtIndexPath: को लागू करना होगा और उन पंक्तियों के लिए वहां से YES वापस करना होगा जिन्हें आपको संपादित/हटाने की आवश्यकता है। यह आवश्यक नहीं है जब आपकी तालिका दृश्य डेटा स्रोत UITableViewContoller का उप-वर्ग है - यदि यह विधि ओवरराइड नहीं है, तो यह विधि डिफ़ॉल्ट रूप से YES लौटाती है। अन्य सभी मामलों में आपको इसे लागू करना होगा।

संपादित करें: हम एक साथ समस्या पाया है - tableView:editingStyleForRowAtIndexPath:UITableViewCellEditingStyleNone लौटे, तो तालिका संपादन मोड में नहीं था।

+1

सब कुछ किया लेकिन अभी भी काम नहीं करता है! –

+0

क्या आपने '[tableView setEditing: YES] को हटा दिया;'? –

+0

अगर मैं इसे हटा देता हूं, तो मूल हटाएं बटन बिल्कुल नहीं आता है! –

5

कोशिश जोड़ने को अपनी कक्षा में निम्नलिखित:

// Override to support conditional editing of the table view. 
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return(YES); 
} 
+5

मेरे लिए काम नहीं कर रहा है :( क्या ऐसा इसलिए है क्योंकि मेरी कक्षा UIViewController का उप-वर्ग है और मुझे कुछ अतिरिक्त करने की आवश्यकता है जो UITabelViewController करता है? –

+1

@ अमोगतालपल्लीकर, आपने अपनी समस्या का समाधान कैसे किया? क्या आप अपना समाधान पोस्ट कर सकते हैं? मेरे पास भी एक ही समस्या है। मेरी कक्षा UIViewController का उप-वर्ग है, और मेरी तालिका के सभी कक्ष 'UITableViewCell के उप-वर्ग हैं। – Shamsiddin

3

Kyr Dunenkoff के निष्कर्ष चैट है

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

} 
यदि आप कड़ी चोट पर प्रकट करने के बटन हटाना होगा

परिभाषित नहीं किया जाना चाहिए।

  1. tableView:canEditRowAtIndexPath:
  2. tableView:commitEditingStyle:forRowAtIndexPath:

नोट::

59

Dan जैसा कि ऊपर टिप्पणी की है, तो आपको निम्न तालिका दृश्य प्रतिनिधि तरीकों को लागू करने की जरूरत है मैं iOS 6 और iOS 7 में इस की कोशिश की है

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return YES - we will be able to delete all rows 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Perform the real delete action here. Note: you may need to check editing style 
    // if you do not perform delete only. 
    NSLog(@"Deleted row."); 
} 
+2

यह काम आईओएस 8 – iOSDeveloper

+0

के साथ होगा, यह वास्तव में आईओएस 8 – Reaper

+3

पर काम नहीं कर रहा है मैंने अभी आईओएस में इसका परीक्षण किया है 8.1.2 एक आईफोन 6 पर और यह काम करता है। मुझे केवल '- (शून्य) तालिका दृश्य को लागू करने की आवश्यकता है: (UITableView *) तालिका देखें प्रतिबद्ध करें संपादन: (UITableViewCellEditingStyle) संपादन स्टाइल के लिए RowAtIndexPath: (NSIndexPath *) indexPath' विधि संपादन के रूप में होना चाहिए डिफ़ॉल्ट। – neonhomer

25
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not 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) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
+5

मेरे जैसे नए शौक के लिए उल्लेखनीय/टिप्पणी: टी वह पंक्ति को अभी भी हटाया जाना चाहिए, साथ ही साथ डेटासोर्स (उदा। सरणी) कम करने की जरूरत है। – BananaAcid

-2

आप XCode 5 में UITableViewController क्लास (अस्थायी) बनाकर सभी आवश्यक विधियां देख सकते हैं और फिर उस विधि की प्रतिलिपि बना सकते हैं जिसका आप उपयोग करना चाहते हैं। जिन तरीकों की आपको आवश्यकता है, उन्हें पूर्व-भर वाली लाइनों के साथ टिप्पणी की जाएगी।

+1

खैर, यह उत्तर अन्य लोगों के लिए है जो उत्तर की तलाश में हैं, निश्चित रूप से। :) –

0

यह मेरे लिए भी एक समस्या थी ... मैं केवल 10 या तो प्रयासों में एक बार काम करने के लिए हटने के लिए स्वाइप कर सकता था। यह gesture को टीवी पर मूल दृश्य नियंत्रक में किसी अन्य इशारा द्वारा अवरोधित किया जा रहा था। टीवी MMDrawerController (स्वाइप सक्षम ड्रॉवर लेआउट) में घोंसला था।

दराज नियंत्रक में इशारा पहचानकर्ता को बस कॉन्फ़िगर करने के लिए फ़्लैंकिंग ड्रॉर्स में करीबी संकेतों का जवाब न देने के लिए बस मेरे टीवी में काम करने के लिए स्वाइप करने की अनुमति दी गई है।

तुम भी gesture delegate साथ कुछ इस तरह कर रही कोशिश कर सकते:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 
} 
+0

मुझे पता है कि यह पुराना है, लेकिन मुझे MMDrawerController के साथ एक ही सटीक समस्या है .. क्या आप "दराज नियंत्रक में इशारा पहचानकर्ता को कॉन्फ़िगर करना चाहते हैं ताकि फ़्लैंकिंग ड्रॉर्स में बंद जेश्चर का जवाब न दें, मेरे टीवी में काम करें "। आप उसे कैसे करते हैं? – Mark

-1
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
      editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 
    NSUInteger count = [posts count]; 

    if (row < count) { 
     return UITableViewCellEditingStyleDelete; 
    } else { 
     return UITableViewCellEditingStyleNone; 
    } 
} 

- (void)tableView:(UITableView *)tableView 
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
        forRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 
    NSUInteger count = [posts count]; 

    if (row < count) { 

     [posts removeObjectAtIndex:row]; 
    } 
} 
0

यह तेज संस्करण

// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the specified item to be editable. 
    return true 
} 

// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     // Delete the row from the data source 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    }  
} 
13

तेजी में इस कोड का प्रयास करें है,

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // let the controller to know that able to edit tableView's row 
    return true 
} 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    // if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code) 
} 

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
    // add the action button you want to show when swiping on tableView's cell , in this case add the delete button. 
    let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in 

    // Your delete code here..... 
    ......... 
    ......... 
    }) 

    // You can set its properties like normal button 
    deleteAction.backgroundColor = UIColor.redColor() 

    return [deleteAction] 
} 
0

यदि आप एक NSFetchedResultsControllerDelegate उपयोग कर रहे हैं तालिका दृश्य को भरने के लिए, यह मेरे लिए काम किया:

  • ,
  • अपने tableView:commitEditingStyle:forRowAtIndexPath कार्यान्वयन में सच यकीन tableView:canEditRowAtIndexPath रिटर्न सुनिश्चित हमेशा तालिका दृश्य से सीधे पंक्ति को हटा नहीं है। इसके बजाय, यह आपके प्रबंधित वस्तु संदर्भ का उपयोग करते हुए, उदा हटाना .:

    if editingStyle == UITableViewCellEditingStyle.Delete { 
        let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word 
        self.managedObjectContext.deleteObject(word) 
        self.saveManagedObjectContext() 
    } 
    
    func saveManagedObjectContext() { 
        do { 
         try self.managedObjectContext.save() 
        } catch { 
         let saveError = error as NSError 
         print("\(saveError), \(saveError.userInfo)") 
        } 
    } 
    
0

मेरे अनुभव में, लगता है आपके पास काम करने के लिए स्वाइप के लिए NO को UITableView सेट पर editing होना आवश्यक है। , यह आपकी मदद कर

self.tableView.editing = NO;

0

आईओएस 8.0 के बाद, आप कस्टम आप

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
संबंधित मुद्दे

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