editingStyleForRowAtIndexPath non è sempre chiamato (Quindi un pulsante Elimina sta rivelando)

StackOverflow https://stackoverflow.com/questions/2226509

  •  19-09-2019
  •  | 
  •  

Domanda

Sto giocando intorno con UITableViewCells in movimento, e per qualsiasi motivo,

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

non è sempre chiamato (Ho impostato un punto di interruzione su di esso) - quindi la tabella mostra l'opzione di eliminazione, ma io non voglio questo. Ecco la mia realizzazione:

@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];
}

@end

Non ci sono avvertimenti in fase di compilazione.

Grazie!

È stato utile?

Soluzione

Prova capitalizzare il nome del metodo corretto

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

Questa è la S in editingStyleForRowAtIndexPath capitalizzati. selettori objc sono case sensitive. La vista tabella non pensare che sia delegato risponde al metodo che si sta tentando di fornire un valore di ritorno a.

Altri suggerimenti

È necessario impostare la selezione multipla di NO in modalità di modifica

self.tableview.allowsMultipleSelectionDuringEditing = NO;

Un altro motivo potrebbe essere che è anche necessario per implementare

- (void)tableView:(UITableView *)tableView
  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  forRowAtIndexPath:(NSIndexPath *)indexPath

Un altro motivo per editingStyleForRowAtIndexPath (e altri delegati) non essere chiamato è se il controllo non dispone di essa la presa delegato collegato al proprietario del file.

Sì, questo è ovvio, ma è facilmente trascurato.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top