添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
沉着的烈马  ·  impala mybatis ...·  3 月前    · 
强健的沙发  ·  java restTemplate ...·  1 年前    · 
逃跑的高山  ·  C++ Trusted publisher ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm playing around with moving uitableviewcells, and for whatever reason,

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingstyleForRowAtIndexPath:(NSIndexPath *)indexPath
    return UITableViewCellEditingStyleNone; //<-- bp set on it

isn't getting called (I've set a breakpoint on it) - so the table is showing the delete option, but I don't want that. Here's my implementation:

@implementation MoveItController
@synthesize mList;
- (IBAction)moveButton
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:(self.tableView.editing)? @"Done" : @"Move"];
- (void)viewDidLoad
    if (mList == nil)
        mList = [[NSMutableArray alloc] initWithObjects:@"$1", @"$2", @"$5", @"$10", @"$20", @"$50", @"$100", nil];
    UIBarButtonItem *mvButton = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Move" 
                                 style:UIBarButtonItemStyleBordered 
                                 target:self 
                                 action:@selector(moveButton)];
    self.navigationItem.rightBarButtonItem = mvButton;
    [mvButton release];
    [super viewDidLoad];
// Table datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return [mList count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *moveItCellId = @"moveItCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:moveItCellId];
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moveItCellId] autorelease];
        cell.showsReorderControl = YES;
    cell.textLabel.text = [mList objectAtIndex:[indexPath row]];
    return cell;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingstyleForRowAtIndexPath:(NSIndexPath *)indexPath
    return UITableViewCellEditingStyleNone;
- (BOOL)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)indexPath
    return YES;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
    id object = [[mList objectAtIndex:[fromIndexPath row]] retain];
    [mList removeObjectAtIndex:[fromIndexPath row]];
    [mList insertObject:object atIndex:[toIndexPath row]];
    [object release];                 
- (void) dealloc
    [mList release];
    [super dealloc];

No warnings at compile time.

Thanks!

- I had set 'allowsMultipleSelectionDuringEditing' to YES to allow for 2-finger selection. I needed to set it to NO during table editing. – Zatman Aug 28, 2021 at 10:07

Try capitalizing the method name properly

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

That's with the S in editingStyleForRowAtIndexPath capitalized. ObjC selectors are case sensitive. The table view does not think it's delegate responds to the method you are trying to provide a return value to.

Damn - I like tripple checked that! haha, thanks. I wish xcode threw a warning, like for a similar method signature...which I thought it usually does. +1 thanks. – mr-sk Feb 9, 2010 at 3:25

Another reason might be that you also need to implement

- (void)tableView:(UITableView *)tableView
  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  forRowAtIndexPath:(NSIndexPath *)indexPath
                That was my issue:.. when you remove this method (commitEditingStyle) and then put a breakpoint in editingStyleForRowAtIndexPath, it won't be called any longer and the default editing style becomes none
– matrix4use
                Sep 22, 2015 at 23:45

Another reason for editingStyleForRowAtIndexPath (and other delegates) not being called is if the control does not have it's delegate outlet connected to the File's Owner.

Yes, this is obvious but it's easily overlooked.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.