【问题标题】:editingStyleForRowAtIndexPath isn't getting called (So a delete button is showing up)没有调用editingStyleForRowAtIndexPath(因此出现了删除按钮)
【发布时间】:2011-01-14 15:40:04
【问题描述】:

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

编译时没有警告。

谢谢!

【问题讨论】:

    标签: iphone uitableview tablecell


    【解决方案1】:

    尝试将方法名正确大写

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

    S 中的 editingStyleForRowAtIndexPath 大写。 ObjC 选择器区分大小写。表格视图不认为它的委托响应您尝试提供返回值的方法。

    【讨论】:

    • 该死 - 我喜欢三重检查!哈哈谢谢。我希望 xcode 发出警告,例如类似的方法签名……我认为它通常会这样做。 +1 谢谢。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      编辑模式下需要将多选设置为NO

      self.tableview.allowsMultipleSelectionDuringEditing = NO;
      

      【讨论】:

      • - 我已将 'allowsMultipleSelectionDuringEditing' 设置为 YES 以允许 2 指选择。我需要在表格编辑期间将其设置为 NO。
      【解决方案4】:

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

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

      【讨论】:

      • 那是我的问题:.. 当你删除这个方法 (commitEditingStyle) 然后在editingStyleForRowAtIndexPath 中放置一个断点,它将不再被调用并且默认的编辑样式变为无
      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多