代码之家  ›  专栏  ›  技术社区  ›  mr-sk

无法调用editingstyleforrowatindexpath(因此将显示一个删除按钮)

  •  2
  • mr-sk  · 技术社区  · 15 年前

    我在玩移动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
    

    编译时没有警告。

    谢谢!

    4 回复  |  直到 9 年前
        1
  •  2
  •   Alex Wayne    15 年前

    尝试正确地大写方法名

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

    就是这样 S 在里面 editingStyleForRowAtIndexPath 大写。objc选择器区分大小写。表视图认为它的委托没有响应您试图向其提供返回值的方法。

        2
  •  4
  •   Voloda2    10 年前

    在编辑模式下,需要将多选设置为否。

    self.tableview.allowsMultipleSelectionDuringEditing = NO;
    
        3
  •  1
  •   theguy    9 年前

    另一个原因可能是您还需要实现

    - (void)tableView:(UITableView *)tableView
      commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
      forRowAtIndexPath:(NSIndexPath *)indexPath
    
        4
  •  0
  •   grantnz    11 年前

    未调用editingstyleforrowatindexpath(和其他委托)的另一个原因是,如果控件没有将其委托出口连接到文件的所有者。

    是的,这很明显,但很容易被忽视。