ja wyświetlania niestandardowych UIMenuController
w moim tableview
jak tenUIMenuController metoda setTargetRect: INview: nie działa w UITableView
ale problemem jest to, że jest wyświetlany w centrum chcę wyświetlić go na górze label
, który jest w kolorze pomarańczowym. Aby wyświetlić na górze label
, zrobiłem ten [menu setTargetRect:CGRectMake(10, 10, 0, 0) inView:self.lbl];
poniżej jest cały kod.
Ale jeśli wyświetlam UIMenuController
bez UITableView
setTargetRect
działa dobrze.
Dlaczego setTargetRect
nie działa z UITableView
?
setTargetRect Doc mówi:
(a) Ten prostokąt docelowy (targetRect) zwykle prostokąta opisanego z wyboru. UIMenuController ustawia menu edycji powyżej tego prostokąta ; jeśli nie ma wystarczająco dużo miejsca na menu, to ustawia je poniżej prostokąta. Wskaźnik menu znajduje się w środku górnej lub dolnej części docelowego prostokąta.
(b) Należy zauważyć, że jeśli to szerokość lub wysokość prostokąta docelowej zero UIMenuController traktuje docelowy obszar jak linia lub punkt dla położenia (na przykład, daszek wstawiania lub jeden punkt).
(c) Po ustawieniu docelowy prostokąt nie śledzi widoku; Jeśli przesunie się widok (taki jak w widoku przewijania), musisz odpowiednio zaktualizować docelowy prostokąt.
Czego mi brakuje?
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");
}
jestem całkiem pewny, ponieważ próbujesz użyć niestandardowego rect, że należy powrócić do 'shouldShowMenuForRowAtIndexPath' NO, który będzie również sprawić, że metody 'canPerformAction' i' performAction' również nie będą potrzebne. –
Moim jedynym zmartwieniem jest to, że etykieta nie zostałaby wstawiona do hierarchii widoków podczas konfigurowania 'UIMenuController', co może być problemem. Musisz "być może" przenieść kod konfiguracyjny 'UIMenuController' na inną metodę delegata, taką jak' tableView: willDisplayCell', a może nawet w komórce 'UIView' method [' didMoveToSuperview'] (https://developer.apple.com/ biblioteka/ios/documentation/UIKit/Reference/UIView_Class/index.html # // apple_ref/occ/instm/UIView/didMoveToSuperview), aby upewnić się, że komórka rzeczywiście znajduje się na ekranie przed ustawieniem niestandardowego prostokąta. –
@SantaClaus Nie, nic nie działa, moje menu wciąż pojawia się w środku. –