简体   繁体   English

尝试滑动以删除当前单元格时,表视图单元格开始晃动

[英]Table View Cells start shaking when trying to swipe to delete current cell

Here's my code 这是我的代码

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        print("editingStyle")
        if editingStyle == .delete {

           //Doing my task  

        }
}

Every time I swipe left, the other cells start shaking and creates a really bad user experience. 每次我向左滑动时,其他单元格就会开始晃动,并带来非常糟糕的用户体验。 I have tried some hacks like 我尝试过一些技巧

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: kCellReuseCardIdentifier) as? MyCustomTableViewCell {

            let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 16, height: 125)
            cell.frame = frame
            cell.contentView.frame = frame
//            Cells get squished when editing mode is enabled. To prevent that from happening , we create a dummy cell
            let dummyCell = tableView.dequeueReusableCell(withIdentifier: kDummyCellIdentifier) as! DummySpaceTableViewCell
            dummyCell.addSubview(cell.contentView)
            dummyCell.clipsToBounds = true

            return dummyCell
}

The funny thing is that this hack works in iOS 11 beta. 有趣的是,此黑客可在iOS 11 Beta中使用。 In iOS 10 this causes the cell shaking problem. 在iOS 10中,这会导致单元抖动问题。 Do note that my tableView has n sections and each section has 1 row. 请注意,我的tableView有n个部分,每个部分都有1行。 Please help. 请帮忙。

I have used MGSwipeTableCell to prevent from above issue.
You can find the code & examples from below link.
https://github.com/MortimerGoro/MGSwipeTableCell

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    experienceCell * experienceCell = [tableView dequeueReusableCellWithIdentifier:@"experienceCell"];
     MGSwipeButton *DeleteButton=[MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"delete_white"] backgroundColor:THEME_COLOR padding:15];

                DeleteButton.titleLabel.font=ROBOTO_REGULAR_FONT16;

                experienceCell.rightSwipeSettings.transition=MGSwipeTransitionStatic;
                experienceCell.rightExpansion.buttonIndex=0;
                experienceCell.rightExpansion.fillOnTrigger=true;
                experienceCell.rightButtons=@[DeleteButton];
                experienceCell.delegate = self;
                experience = true;
     return experienceCell;
    }
    -(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
 {
if(direction == MGSwipeDirectionRightToLeft && index == 0)
{
}

return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM