如何使用MGSwipeTableCell库检测在滑动时选择的行

时间:2015-12-18 12:17:15

标签: ios swift

在使用MGSwipeTableCell库之前,我使用editActionsForRowAtIndexPath方法来检测选择了哪一行,并根据行信息动态添加UITableViewRowAction。 但是使用MGSwipeTableCell如何做到这一点,即在添加MGSwipeButton之前检测选择了哪一行,而不是在点击之后。

我目前的代码是:

  in cellForRowAtIndexPath: 

    let cell = tableView.dequeueReusableCellWithIdentifier("TaskListCell", forIndexPath: indexPath) as! TaskListTableViewCell


                    cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D
                    cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D
                    cell.rightExpansion.fillOnTrigger = true;
                    cell.rightExpansion.buttonIndex = 0;

    var StatusButton = MGSwipeButton()
                    if(CurrentTaskStatus == "1")  // this is dependent on the row and is now returning wrong values because i don't have the index of the row. 
                  {
                        StatusButton = MGSwipeButton(title: NSLocalizedString("accept_task", comment: ""), backgroundColor: UIColor.redColor(), callback: {
                            (sender: MGSwipeTableCell!) -> Bool in


                            let indexx = self.tableView.indexPathForCell(sender)
                            let index = indexx?.row
                            self.CurrentTaskCode = self.TaskRowArrayList[index!].taskCode
                            self.CurrentTaskStatus = self.TaskRowArrayList[index!].taskStatus
                            self.CurrentTaskSubStatus = self.TaskRowArrayList[index!].taskSubStatus

// i need to get these before clicking the button and not after 

                            return true
                        })
                    }

2 个答案:

答案 0 :(得分:2)

Swift 4

步骤1.将MGSwipeButton及其标签添加到右/左按钮阵列

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
            let cell  = tblPoolList.dequeueReusableCell(withIdentifier: "PoolTableViewCell") as! PoolTableViewCell
            cell.delegate = self
            let btndelete = MGSwipeButton(title: "", icon: UIImage(named:"ic_delete_white_swipe_left"), backgroundColor: .red)
            btndelete.tag = indexPath.row
            let btnedit = MGSwipeButton(title: "", icon: UIImage(named:"ic_edit_white_swipe_left"), backgroundColor: .green)
            btnedit.tag = indexPath.row            
            cell.rightButtons = [btndelete,btnedit]
            cell.rightSwipeSettings.transition = .drag                        
            return cell       

    }

步骤2.您可以在其builtIn库方法中轻松获取表格单元格的索引。

func swipeTableCell(_ cell: MGSwipeTableCell, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {

        print(cell.rightButtons.first!.tag)

        if index == 0 {
            print("Delete")
        } else {
            print(index)   
        }

        return true
    }

答案 1 :(得分:0)

在滑动期间选择的行应与您正在创建的单元格相同,因此从当前的代码段中,您只需将闭包中的indexPath参数用于MGSwipeButton回调。 / p>

self.CurrentTaskCode = self.TaskRowArrayList[indexPath!].taskCode
self.CurrentTaskStatus = self.TaskRowArrayList[indexPath!].taskStatus
self.CurrentTaskSubStatus = self.TaskRowArrayList[indexPath!].taskSubStatus

如果你不能这样做,我会说你需要更新更多关于原因的信息,因为这将是一个明显的解决方案。