You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改MGSwipeTableCell中MGSwipeButton的图片?点击修改无效求助

问题

使用MGSwipeTableCell时,点击右滑菜单中的“isTop”按钮后,尝试修改该按钮的图片,但修改代码未能生效。

相关实现代码:

-(nullable NSArray<UIView*>*) swipeTableCell:(nonnull MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
                           swipeSettings:(nonnull MGSwipeSettings*) swipeSettings expansionSettings:(nonnull MGSwipeExpansionSettings*) expansionSettings {
    swipeSettings.transition = MGSwipeTransition3D; // 滑出动画
    if (direction == MGSwipeDirectionRightToLeft) {
        expansionSettings.fillOnTrigger = YES;
               expansionSettings.threshold = 1.5;
               CGFloat padding = 10;
               MGSwipeButton *isTop = [MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"top"] backgroundColor:[UIColor colorWithRed:243/256.0 green:243/256.0 blue:243/256.0 alpha:1] padding:padding callback:^BOOL(MGSwipeTableCell *sender) {
                   NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
                   TrackNumbersCellModel*model = self.dataSource[indexPath.row];
                   if (model.isTop) {
                      
                       model.isTop = NO;
                       [self.dataSource removeObject:model];
                       [self.dataSource insertObject:model atIndex:self.dataSource.count - 1];
                       [[TrackNumbersNetTools shareIntance] trackTable_MR_update_top_Entity:model.t_number isTop:NO];
                       [self.tableView reloadData];
                        
                   }else{
                       model.isTop = YES;
                       [self.dataSource removeObject:model];
                       [self.dataSource insertObject:model atIndex:0];
                       [[TrackNumbersNetTools shareIntance] trackTable_MR_update_top_Entity:model.t_number isTop:YES];
                       [self.tableView reloadData];
                   }
                    
                   return YES;
               }];
               [isTop centerIconOverTextWithSpacing:7];
               
               MGSwipeButton *isDelete = [MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"delet"] backgroundColor:[UIColor colorWithRed:243/256.0 green:243/256.0 blue:243/256.0 alpha:1] padding:padding callback:^BOOL(MGSwipeTableCell *sender) {
                   NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
                   TrackNumbersCellModel*model = self.dataSource[indexPath.row];
                   [self.dataSource removeObjectAtIndex:indexPath.row];
                   [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                   [[TrackNumbersNetTools shareIntance] trackTable_MR_deleteEntity:model.t_number onFinished:^{
                       if (self.deleteBlock) {
                           self.deleteBlock();
                       }
                   }];
                  
                   return YES;
               }];
               [isDelete centerIconOverTextWithSpacing:7];
               return @[isTop,isDelete];
    }
    
    return nil;
    
}

尝试修改图片的代码:

-(BOOL)swipeTableCell:(MGSwipeTableCell*)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion{
    if (direction == MGSwipeDirectionRightToLeft && index == 0) {
        id button = cell.leftButtons[index];
        MGSwipeButton * pressedBtn = button;
        [pressedBtn setImage:[UIImage imageNamed:@"yy"] forState:UIControlStateNormal];
        NSLog(@"pressedBtn title: %@",pressedBtn.titleLabel.text);}
    return YES;
}
解决方案

问题出在两个核心点:

  1. 按钮回调里调用了reloadData,这会重新创建所有cell及对应的滑动按钮,你在tappedButtonAtIndex里修改的按钮会被全新的按钮覆盖,自然看不到效果。
  2. 右滑菜单的按钮存在cell.rightButtons数组中,你用leftButtons获取的是左滑方向的按钮,根本没拿到目标按钮。

正确的做法是由数据源驱动UI显示,也就是在创建按钮时,根据model.isTop的状态直接设置对应图片,这样reloadData后按钮会自动匹配数据源状态:

修改swipeTableCell:swipeButtonsForDirection:swipeSettings:expansionSettings:方法,创建isTop按钮前先获取当前cell对应的model,根据状态选择图片:

-(nullable NSArray<UIView*>*) swipeTableCell:(nonnull MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
                           swipeSettings:(nonnull MGSwipeSettings*) swipeSettings expansionSettings:(nonnull MGSwipeExpansionSettings*) expansionSettings {
    swipeSettings.transition = MGSwipeTransition3D; // 滑出动画
    if (direction == MGSwipeDirectionRightToLeft) {
        expansionSettings.fillOnTrigger = YES;
        expansionSettings.threshold = 1.5;
        CGFloat padding = 10;
        
        // 获取当前cell对应的model
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        TrackNumbersCellModel*model = self.dataSource[indexPath.row];
        // 根据model状态选择对应图片
        UIImage *topIcon = model.isTop ? [UIImage imageNamed:@"yy"] : [UIImage imageNamed:@"top"];
        
        MGSwipeButton *isTop = [MGSwipeButton buttonWithTitle:@"" icon:topIcon backgroundColor:[UIColor colorWithRed:243/256.0 green:243/256.0 blue:243/256.0 alpha:1] padding:padding callback:^BOOL(MGSwipeTableCell *sender) {
            // 原回调逻辑保持不变
            NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
            TrackNumbersCellModel*model = self.dataSource[indexPath.row];
            if (model.isTop) {
                model.isTop = NO;
                [self.dataSource removeObject:model];
                [self.dataSource insertObject:model atIndex:self.dataSource.count - 1];
                [[TrackNumbersNetTools shareIntance] trackTable_MR_update_top_Entity:model.t_number isTop:NO];
                [self.tableView reloadData];
            }else{
                model.isTop = YES;
                [self.dataSource removeObject:model];
                [self.dataSource insertObject:model atIndex:0];
                [[TrackNumbersNetTools shareIntance] trackTable_MR_update_top_Entity:model.t_number isTop:YES];
                [self.tableView reloadData];
            }
            return YES;
        }];
        [isTop centerIconOverTextWithSpacing:7];
        
        MGSwipeButton *isDelete = [MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"delet"] backgroundColor:[UIColor colorWithRed:243/256.0 green:243/256.0 blue:243/256.0 alpha:1] padding:padding callback:^BOOL(MGSwipeTableCell *sender) {
            NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
            TrackNumbersCellModel*model = self.dataSource[indexPath.row];
            [self.dataSource removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [[TrackNumbersNetTools shareIntance] trackTable_MR_deleteEntity:model.t_number onFinished:^{
                if (self.deleteBlock) {
                    self.deleteBlock();
                }
            }];
            return YES;
        }];
        [isDelete centerIconOverTextWithSpacing:7];
        return @[isTop,isDelete];
    }
    return nil;
}

修改完成后,你可以删掉tappedButtonAtIndex里修改图片的代码,因为按钮图片现在会根据数据源状态自动更新。

内容的提问来源于stack exchange,提问作者dongshilei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:09:59