TTTableViewController showMenu:forCell: example

2019-09-01 00:23发布

问题:

I'm looking for an example of how to create an animated row menu like the Facebook & twitter apps for iphone have. I see the TTTableViewController has the showMenu:forCell: method, but I have not been able to find any examples of how to use it. Specifically in the context of a URL Navigator selector, but any example would be great.

回答1:

Were you able to find an example on this.

I, too, was stuck with the same problem as you did. I couldn't find an example of using TTTableViewController showMenu:forCell: method a week ago. After messing with the code, I came up with this...

Create a subclass of TTTableViewCell and add a UIButton (as a trigger to launch the menu view) to the view.

@interface MyViewCell : TTTableViewCell {
}

- (id) initWithName:(NSString *)name target:(id)target action:(SEL)action {
  // ...

    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreButton.frame = CGRectMake(268.0f, 6.0f, 32.0f, 32.0f);
    [moreButton setImage:TTIMAGE(@"bundle://Icon_More.png")
                     forState:UIControlStateNormal];
    [moreButton addTarget:target action:action 
         forControlEvents:UIControlEventTouchUpInside];
    [self moreButton];

  // ...
}

Next, create a subclass of TTTableViewController and add custom TTTableViewCells to the data source.

@interface MyTableViewController : TTTableViewController {
}

- (void) createModel {  
  self.dataSource = [TTListDataSource dataSourceWithObjects:
    [[[ContactViewCell alloc] initWithName:@"Cell 1"
                                    target:self 
                                    action:@selector(moreButtonDidPress:)]
                                    autorelease],
    [[[ContactViewCell alloc] initWithName:@"Cell 2"
                                    target:self 
                                    action:@selector(moreButtonDidPress:)]
                                    autorelease],
    nil];
}

In the action handler, that's where showMenu:forCell: is called. The trick is to determine which cell the button belongs to and consequently replace that cell with the menu view. This is what I did.

- (void) moreButtonDidPress:(id)sender {
  // Load our custom menu view from a nib.
  UIView *menuView = [[UIView alloc] initWithFrame:cell.contentView];

  UIButton *moreButton = (UIButton *) sender;
  // Convert plusButton bounds to the the coordinate system of table view
  // and then get the cell containing the button.
  CGRect coord = [plusButton convertRect:moreButton.bounds toView:self.tableView];
  NSIndexPath *path = [self.tableView indexPathForRowAtPoint:coord.origin];
  TTTableViewCell* cell = (TTTableViewCell*) [self.tableView 
                                              cellForRowAtIndexPath:path]; 

  // Now call showMenu with the menu to display on the associated cell.
  [self showMenu:menuView forCell:cell animated:YES]; 
}

It's not exactly using a URL Navigator selector, but it's functional.