editStyleForRowAtIndexPath не вызывается (поэтому появляется кнопка удаления)
-
19-09-2019 - |
Вопрос
Я играю с перемещением uitableviewcells, и по какой-то причине
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingstyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone; //<-- bp set on it
}
не вызывается (я установил для него точку останова) - поэтому в таблице отображается опция удаления, но мне это не нужно.Вот моя реализация:
@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
Никаких предупреждений во время компиляции.
Спасибо!
Решение
Попробуйте правильно написать имя метода с заглавной буквы.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
Это с S
в editingStyleForRowAtIndexPath
с большой буквы.Селекторы ObjC чувствительны к регистру.Табличное представление не считает, что его делегат реагирует на метод, которому вы пытаетесь предоставить возвращаемое значение.
Другие советы
Вам необходимо установить множественный выбор на НЕТ в режиме редактирования.
self.tableview.allowsMultipleSelectionDuringEditing = NO;
Другая причина может заключаться в том, что вам также необходимо реализовать
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
Другая причина, по которой EditStyleForRowAtIndexPath (и другие делегаты) не вызываются, заключается в том, что элемент управления не имеет выхода делегата, подключенного к владельцу файла.
Да, это очевидно, но это легко упустить из виду.