【问题标题】:Adding custom edit and delete button in UITableViewCell在 UITableViewCell 中添加自定义编辑和删除按钮
【发布时间】:2015-10-12 05:53:11
【问题描述】:

我有一个UITableView,其中包含所有国家/地区的名称。 用户可以随时点击cell删除或编辑国家名称。

我的 UITableView 单元格最初看起来像这样:

现在当用户点击它时,我正在改变它:

我认为我正在遵循一种非常蹩脚的方法。这就是我所做的:

声明要添加的全局按钮:

 UIButton *btnDeleteWithImage,*btnDeleteWithText,*btnEditWithImage,*btnEditWihtText; //buttons

还有一个NSMutableArray 来跟踪indexPath.row

现在在我的 didSelectMethod 中我正在这样做:

 //To change the background 
 UIView *selectionBackground = [[UIView alloc] init];

  selectionBackground.backgroundColor = [UIColor customColor];

 cell.selectedBackgroundView = selectionBackground;

 // to check which cell is pressed

if([indexPathCollection containsObject:index])
{
    [btnDeleteWithImage removeFromSuperview];
    [btnDeleteWithText removeFromSuperview];
    [btnEditWihtText removeFromSuperview];
    [btnEditWithImage removeFromSuperview];
    [indexPathCollection removeObject:index];

    [cell addSubview:btnDeleteWithImage];
    [cell addSubview:btnDeleteWithText];
    [cell addSubview:btnEditWithImage];
    [cell addSubview:btnEditWihtText];
    [indexPathCollection addObject:index];
}
else
{

    [cell addSubview:btnDeleteWithImage];
    [cell addSubview:btnDeleteWithText];
    [cell addSubview:btnEditWithImage];
    [cell addSubview:btnEditWihtText];
    [indexPathCollection addObject:index];
}

但这不好用。当我滚动表格时,随机出现编辑和删除按钮。

是否有人有更好的想法如何以非常有效的方式实现这一目标。

【问题讨论】:

  • 您的方法不起作用,因为您可能在 cellforrowatindexpath 委托方法中重用了单元格。
  • 我该怎么做...@elbuild 请帮助我

标签: ios objective-c uitableview tableviewcell


【解决方案1】:

您可以通过使用您的属性创建自定义单元格来实现此目的

CustomCell.h

@interface CustomCell : UITableViewCell

@property (nonatomic, strong) UIButton *btnDeleteWithImage;
@property (nonatomic, strong) UIButton *btnDeleteWithText;
@property (nonatomic, strong) UIButton *btnEditWithImage;
@property (nonatomic, strong) UIButton *btnEditWithText;

@end

在单元格的 init 方法中初始化它们,首先将它们隐藏起来,或者你可以这样做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *customCellIdentifier = @"customCellIdentifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

    if (!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:customCellIdentifier];
        // or you can initialize and add them to the cell here
    }
    //here you can modify them accordingly
    return cell;
}

那么委托方法可以是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.btnDeleteWithImage setHidden:NO];
    [cell.btnEditWithImage setHidden:NO];
}

【讨论】:

    【解决方案2】:

    最简单的方法是不要重复使用单元格。并使用相同的 indentifire 注册您的自定义单元格。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    customCell* cell = [[customCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"indentifire"] ;                                      
    
    // manage your plooting here ..
    
    return cell;
    }
    

    希望它对你有用。

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多