gpt4 book ai didi

ios - 向 UiTableViewCell 添加删除按钮

转载 作者:行者123 更新时间:2023-11-29 03:04:43 24 4
gpt4 key购买 nike

由于我在应用程序中使用滑出式菜单 Controller - 在 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];

这添加了按钮,在删除方法中我不确定如何真正删除正确的单元格。

谁能给我指明正确的方向?

谢谢!

最佳答案

基本上,您需要单元格的索引,当按下删除键时必须将其删除。

您可以设置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;
}

关于ios - 向 UiTableViewCell 添加删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com