2013-03-18 6 views
12

मैं अपने सेल पर एक कस्टम बटन जोड़ना चाहता हूं जो स्वाइप-टू-डिलीट फ़ंक्शन जैसा ही काम करता है। तो जब मेरे कस्टम बटन पर क्लिक करते हैं, तो यह आधिकारिक लाल "हटाएं" बटन दिखाई देने के लिए छिपा होगा।प्रोग्रामेटिक रूप से UITableViewCell "हटाएं" बटन

तो मैं ऐसा ही कुछ किया:

/// Controller.m 
/// 
/// @brief Delete icon button pressed. Trigger display of Delete full button 
/// 
- (IBAction)deleteDrug:(id)sender event:(id)event { 
    NSIndexPath *indexPath = [self indexPathForButton:sender event:event]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    [cell setEditing:YES animated:YES]; 
} 

/// CustomCell.m 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 

    // hide/show "modify" button when entering in edit mode 
    switch (editing) { 
     case YES: 
      self.deleteButton.hidden = YES; 
      break; 
     case NO: 
      self.deleteButton.hidden = NO; 
      break; 
     default: 
      break; 
    } 
} 

इस समय, अपने कस्टम बटन जब उन पर क्लिक लेकिन सरकारी लाल "हटाएं" बटन दिखाई दे रहा है छिपाने हो रही है।

क्या किसी को यह पता है कि इसे कैसे संभालना है?

उत्तर

5

मेरा मानना ​​है कि हटाएं बटन तालिका दृश्य द्वारा अधिक संभाला जाता है। तो अपने सेल संपादन को सेट करने के बजाय आपको तालिका को देखने की आवश्यकता हो सकती है, इसे पता है कि इसे संपादित करना है।

- (IBAction)deleteDrug:(id)sender event:(id)event { 
    selectedButtonIndex = [self indexPathForButton:sender event:event]; 

    [tableView setEditing:YES animated:YES]; 
} 

तो आपको संपादन करने के लिए तालिका सेट करने जैसे कुछ करने की आवश्यकता हो सकती है। फिर अपने टेबलव्यू के डेटासोर्स में आप इस विधि को कार्यान्वित कर सकते हैं, जहां चयनित बटन उचित सेल के लिए सूचकांक पथ है।

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath == selectedButtonIndex) { 
     return YES; 
    } 

    return NO; 
} 

आपको अपने डेटा स्रोत के लिए इस विधि को लागू करने की आवश्यकता हो सकती है।

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
+6

'[tableView setEditing: हाँ एनिमेटेड: हाँ]; 'सेल के बाईं ओर स्थित लाल आइकन की आशंका का कारण बनता है (वह जो आपको दाईं ओर लाल" हटाएं "बटन लाने की अनुमति देता है)। और मुझे यह नहीं चाहिए। मैं वास्तव में चाहता हूं कि मेरा कस्टम बटन सीधे दाईं ओर लाल "हटाएं" बटन लाए। – Yaman

+0

मुझे लगता है कि यह सच है। यह संभव है कि जब से आप अपेक्षित व्यवहार को ओवरराइड करने की कोशिश कर रहे हैं, तो आपको पूर्ण दूरी पर जाने और हटाने बटन को संभालने की आवश्यकता हो सकती है। –

+0

हाँ मुझे डर था कि एकमात्र तरीका होगा ... आपकी सहायता के लिए Thx रयान। – Yaman

-3

कृपया UITableViewcell के दाईं ओर अपना डिलीट बटन काम करने के लिए ऐसा करें।

- (void)tableView:(UITableView *)tableView 
     commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
        withRowAnimation:UITableViewRowAnimationTop]; 
     [self.**yourarray** removeObjectAtIndex:indexPath.row]; 
     [tableView endUpdates]; 
     [tableView reloadData]; 
    } 
} 
+0

अनुचित उत्तर। –

+0

प्रश्न का उत्तर नहीं देता है। – Camsoft

1

मैं इस सवाल का कोई भी उचित समाधान नहीं मिला है, इसलिए मैं अपने खुद के बना दिया। मैंने फर्जी डिलीट बटन बनाया है और यह मूल की तरह बहुत सुंदर काम करता है। मैं यहां मुश्किल हटाना बटन भाग दिखाता हूं।

आप जानना चाहेंगे कि डिफ़ॉल्ट शून्य से बदलना चाहते हैं (हटाना) संपादित करें नियंत्रण, इसके बारे में अच्छा लेख है, मेरा सुझाव है: http://vinsol.com/blog/2015/01/06/custom-edit-control-for-uitableviewcell/

तो, मान लें कि आपके लेख से पहले भाग बना दिया है और आप देखना चाहते हैं जब आपका कस्टम एडिट कंट्रोल बटन दबाया जाता है तो बटन हटाएं। कोड स्विफ्ट पर है, लेकिन आप ऑब्जेक्टिव-सी में इस बनाने के लिए समान चरणों का उपयोग कर सकते हैं

func deleteButtonShow(){ 

    let deleteButton = UIButton(frame: CGRect(x: self.frame.width, y: 0, width: 80, height: self.frame.height)) 
    deleteButton.backgroundColor = UIColor.redColor() // ! note: original delete have another color 
    deleteButton.setTitle("Delete", forState: UIControlState.Normal) 
    deleteButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    deleteButton.addTarget(self, action: "actionForDelete", forControlEvents: UIControlEvents.TouchUpInside) 
    // action for your button will call delegate that will do the delete work. in most cases 
    self.contentView.addSubview(deleteButton) 

    var movedSubviews = self.subviews   
    var i = 0 
    for subview in movedSubviews { 
     // NOTE: I added tag = 1 in the storyboard to Content View of my cell 
     if subview.tag == 1 { 
      movedSubviews.removeAtIndex(i) // we don't need to move it. 
      break 
     } 
     ++i 
    } 
    movedSubviews.extend(self.contentView.subviews)// add subviews from content view 

    UIView.animateWithDuration(0.3, animations: {() -> Void in 

     for movedView in movedSubviews as! [UIView] { 

      movedView.frame = CGRect(origin: CGPoint(x: movedView.frame.origin.x - 80, y: movedView.frame.origin.y), size: movedView.frame.size) 
     //animate the things  
     } 

    }) 


} 

हम सामग्री दृश्य स्थानांतरित करने के लिए के रूप में मैंने पाया नहीं करना चाहती है कि अगर हमारे बटन सामग्री के ऊपर पर नहीं है देखो यह अस्पृश्य है, मेरे पास कुछ विचार हैं क्यों, लेकिन यकीन नहीं है। यदि आप जानते हैं, तो कृपया इस जानकारी को टिप्पणियों में जोड़ें, मुझे यह जानना अच्छा लगेगा।

मैं यह भी ध्यान रखना चाहता हूं कि यदि आप मेरे रूप में ऑटोलाउट का उपयोग करते हैं, तो आपको एनीमेशन में बाधाओं को बदलने की जरूरत है, न कि यह फ्रेम है, इसलिए आपको extend भाग की आवश्यकता नहीं हो सकती है।

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