TTTableViewController showMenu:forCell: example

青春壹個敷衍的年華 提交于 2019-12-12 03:40:08

问题


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.



来源:https://stackoverflow.com/questions/4620167/tttableviewcontroller-showmenuforcell-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!