[英]Adding a delete button to UiTableViewCell
由于我在应用程序中使用滑出式菜单控制器-在UITablewViewCell上进行删除的滑动不再起作用,因为使用平移手势可以打开/关闭侧边菜单。
因此,我正在考虑添加删除按钮以始终显示在每个单元格上-因此用户只需点击删除即可删除该单元格。
我将此代码添加到UItableView cellforRowAtIndexPath方法:
/* Remove Button */
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:@"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:@selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
这将添加按钮,并且在remove方法中,我不确定如何实际删除正确的单元格。
有人可以在这里指出正确的方向吗?
谢谢!
基本上,您需要按删除键时必须删除的cell
索引。
您可以设置tag
属性,当按下按钮时,您可以检查正在发送事件的按钮的标签正确性。
参见下面的代码,
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.tag = indexPath.row;
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:@"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:@selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
-(void) removeItem:(id) sender
{
UIButton *button = (UIButton*)sender;
int index = button.tag;
}
尝试这个,
- (IBAction)removeItem:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
//Remove the cell at indexPath
}
// 1。 在按钮上添加标签,以便以后识别
removeButton.tag = indexPath.row;
// 2。 获取细胞
UITableViewCell *cellToBeDeleted = [tableView cellForRowAtIndexPath:sender.tag];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.