2015-07-02 8 views
10

मैं इसUIMenuController विधि setTargetRect: ध्यान में रखते हुए: UITableView

enter image description here

की तरह मेरे tableview में कस्टम UIMenuController प्रदर्शित कर रहा हूँ, लेकिन मुद्दा यह है कि यह केंद्र में प्रदर्शित कर रहा है है में काम नहीं कर रहा के शीर्ष पर प्रदर्शित करना चाहते हैं label जो नारंगी रंग है। label के शीर्ष पर प्रदर्शित करने के लिए मैंने यह [menu setTargetRect:CGRectMake(10, 10, 0, 0) inView:self.lbl]; नीचे दिया है पूरा कोड है।

लेकिन यदि मैं UIMenuControllerUITableViewsetTargetRect के बिना ठीक काम करता हूं तो मैं ठीक काम करता हूं।

setTargetRectUITableView के साथ क्यों काम नहीं कर रहा है?

setTargetRect डॉक्टर का कहना है:

(क) यह लक्ष्य आयत (targetRect) आम तौर पर एक चयन के सीमांकन आयत है। UIMenuController इस आयताकार के ऊपर संपादन मेनू को स्थान देता है; यदि वहां मेनू के लिए पर्याप्त जगह नहीं है, तो यह आयताकार के नीचे स्थित है। मेनू के सूचक को उचित आयत के शीर्ष या नीचे के केंद्र पर रखा गया है।

(ख) ध्यान दें कि यदि आप चौड़ाई या लक्ष्य आयत शून्य की ऊंचाई बनाने, UIMenuController एक पंक्ति या स्थिति के लिए बिंदु के रूप में लक्षित क्षेत्र व्यवहार करता है (उदाहरण के लिए, एक प्रविष्टि कैरट या एक बिंदु)।

(सी) एक बार यह सेट हो जाने के बाद, लक्ष्य आयताकार दृश्य को ट्रैक नहीं करता है; यदि दृश्यों को देखें (जैसे स्क्रॉल व्यू में होगा), तो आपको तदनुसार लक्ष्य आयताकार अपडेट करना होगा।

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

MyViewController

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    TheCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cid" forIndexPath:indexPath]; 

    cell.lbl.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 

    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { 
    // required 
} 

MyCustomCell

- (void)awakeFromNib { 
    // Initialization code 

    UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)]; 
    UIMenuController *menu = [UIMenuController sharedMenuController]; 
    [menu setMenuItems: @[testMenuItem]]; 

    [menu setTargetRect:CGRectMake(10, 10, 0, 0) inView:self.lbl]; 

    [menu update]; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender { 
    return (action == @selector(copy:) || action == @selector(test:)); 
} 

/// this methods will be called for the cell menu items 
-(void) test: (id) sender { 
    NSLog(@"test"); 
} 

-(void) copy:(id)sender { 
    UIMenuController *m = sender; 
    NSLog(@"copy"); 
} 
+0

साथ मैं यकीन है कि जब से तुम है कि आप 'shouldShowMenuForRowAtIndexPath' के लिए नहीं लौटना चाहिए एक कस्टम रेक्ट उपयोग करने के लिए कोशिश कर रहे हैं रहा हूँ, जो भी होगा 'canPerformAction' और 'performAction' विधियों को अनावश्यक भी बनाएं। –

+0

मेरी एकमात्र अन्य चिंता यह है कि जब आप 'UIMenuController' को कॉन्फ़िगर करते हैं तो लेबल दृश्य पदानुक्रम में डाला नहीं गया होता, जो कि एक समस्या हो सकती है। आपको _may_ को 'UIMenuController' कॉन्फ़िगरेशन कोड को' tableView: willDisplayCell', या यहां तक ​​कि सेल की 'UIView' विधि' में भी 'UMenToSuperview'] (https://developer.apple.com/) में एक अलग प्रतिनिधि विधि में स्थानांतरित करने की आवश्यकता है। लाइब्रेरी/आईओएस/प्रलेखन/UIKit/संदर्भ/UIView_Class/index.html # // apple_ref/occ/instm/UIView/didMoveToSuperview) यह सुनिश्चित करने के लिए कि सेल आपके कस्टम रीक्ट सेट करने से पहले स्क्रीन पर वास्तव में स्क्रीन पर है। –

+0

@ सांताक्लॉस नहीं कुछ भी काम नहीं कर रहा है, मेरा मेनू अभी भी केंद्र में दिखाई दे रहा है। –

उत्तर

14

1) पहले, कृपया अपनी दृश्य नियंत्रक के viewDidLoad विधि में करते हैं।

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" 
    action:@selector(test:)]; 
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]]; 
[[UIMenuController sharedMenuController] update]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil]; 

2) फिर, इन दो तरीकों को जोड़ें। और अपने सेल

-(void)menuControllerWillShow:(NSNotification *)notification{ 
//Remove Will Show Notification to prevent 
// "menuControllerWillShow" function to be called multiple times 
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil]; 

//Hide the Original Menu View 
UIMenuController* menuController = [UIMenuController sharedMenuController]; 
CGSize size = menuController.menuFrame.size; 
CGRect menuFrame; 
menuFrame.origin.x = self.tableView.frame.origin.x; 
menuFrame.size = size; 
[menuController setMenuVisible:NO animated:NO]; 

//Modify its target rect and show it again to prevent glitchy appearance 
    [menuController setTargetRect:menuFrame inView:cell]; 
    [menuController setMenuVisible:YES animated:YES]; 
} 

-(void)menuControllerWillHide:(NSNotification *)notification{ 
    //re-register menuControllerWillShow 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) 
name:UIMenuControllerWillShowMenuNotification object:nil]; 
} 

3) tableView प्रतिनिधि को लागू करें और इन तरीकों को लागू करने के menuFrame inView निर्धारित किया है।

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; 

    return YES; 
} 
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { 
    return NO; 
} 
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { 
    // required 
} 

4) सेटअप कोशिकाओं (UITableViewCell उपवर्गीकरण)

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender { 
    return (action == @selector(copy:) || action == @selector(test:)); } /// this methods will be called for the cell menu items 
-(void) test: (id) sender { 
    NSLog(@"test"); } 

-(void) copy:(id)sender { 
    NSLog(@"copy"); } 
+0

'menuControllerWillShow' में' cell' पाने के लिए, यहाँ कोड है: 'जाने menuOrigin = self.view.convertPoint (menuController.menuFrame.origin, toView: self.tableView) जाने indexPath = _chatTableView.indexPathForRowAtPoint (menuOrigin) जाने सेल = _chatTableView.cellForRowAtIndexPath (indexPath!) '- स्विफ्ट कोड – D4ttatraya

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