2012-06-09 43 views
4

我需要在目标c,ios5中的tableview单元格上设置样式默认的删除按钮。总的想法是,你可以做这样的事情的假设:如何设计objective-c ios5中的默认删除按钮?

UIImage *addImage = [UIImage imageNamed:@"greenButtonDark.png"]; 
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    addButton.frame = CGRectMake(0, 0, addImage.size.width, addImage.size.height); 

    [addButton setImage:addImage forState:UIControlStateNormal]; 
    [addButton addTarget:self action:@selector(pushAddItem) forControlEvents:UIControlEventTouchUpInside]; 

    UIBarButtonItem *addBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton] ; 

    self.navigationItem.rightBarButtonItem = addBarButtonItem; 

上面的代码是所谓的viewDidLoad方法,并覆盖在头部右边的按钮。我假设系统默认添加了一些按钮,但我不知道如何访问这个特定的按钮。

如果我未能阐明这一点,删除按钮,我指的是与此代码自动生成一个...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     //do something when someone hits delete 
    } 
} 

如果我需要澄清什么让我知道。谢谢。

enter image description here

+0

你有没有尝试子类UITableView?它*可能*工作。你将不得不发现哪个方法调用' - tableView:commitEditingStyle:forRowAtIndexPath:'无论如何,为什么要删除防弹背心? – apple16

+0

不,我没有。按照类似的方式访问默认按钮似乎是合乎逻辑的。不管怎样,我希望你删除防弹背心,因为它表明你已经获得了它,并且不再需要它在你的名单上。 –

+0

您无法访问该按钮(除了更改标题)。你可以做的是使用你自己的单元格编辑附件视图,但是你需要做一些额外的工作来实现“编辑模式”删除模式,并实现轻扫删除等。 – jrturton

回答

0

看看这个答案iPhone UITableView - Delete Button,应该基本上回答你的问题。总之,你可能需要继承的UITableViewCell和覆盖- (空)willTransitionToState:(UITableViewCellStateMask)状态- (空)didTransitionToState:(UITableViewCellStateMask)状态这里修改按钮。

另一种方法是继承了的UITableViewCell和实施自己的删除按钮的功能(识别轻扫手势,目前的按钮,当它以类似的方式的UITableView委托的默认方法做的pressed处理)。

相关问题