Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 完成后更改滑动按钮标题_Swift - Fatal编程技术网

Swift 完成后更改滑动按钮标题

Swift 完成后更改滑动按钮标题,swift,Swift,我有以下代码: cell.rightButtons = [MGSwipeButton(title: "Save", backgroundColor:UIColor.redColor(),callback: { (sender: MGSwipeTableCell!) -> Bool in print("Saved") return true }))] cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D

我有以下代码:

cell.rightButtons = [MGSwipeButton(title: "Save", backgroundColor:UIColor.redColor(),callback: {
  (sender: MGSwipeTableCell!) -> Bool in
  print("Saved")
  return true
}))]
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D

return cell

这很好,但我想把标题从“保存”改为“删除”。尝试了很多不同的方法,但似乎都不起作用

如官方项目所述: 您可以通过调用委托方法获得按下的按钮:

-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion;
/**
 * Delegate method to setup the swipe buttons and swipe/expansion settings
 * Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
 * Setting up buttons with this delegate instead of using cell properties improves memory usage because buttons are only created in demand
 * @param swipeTableCell the UITableVieCel to configure. You can get the indexPath using [tableView indexPathForCell:cell]
 * @param direction The swipe direction (left to right or right to left)
 * @param swipeSettings instance to configure the swipe transition and setting (optional)
 * @param expansionSettings instance to configure button expansions (optional)
 * @return Buttons array
 **/
Swift中,您可以编写:

func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
    if let button = cell.rightbuttons[index] {
        print(button.titleLabel.text)
        button.setTitle("Button Title", forState: UIControlState.Normal)
        ... (do whatever you want with your tapped button..)
    }
}
为了帮助您启动bundle源中包含的MGSwipeDemo项目,并在objective c中添加以下行,如以下屏幕截图所示:

-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
{
    ...
    if (direction == MGSwipeDirectionRightToLeft && index == 1) {
        NSLog(@"more pressed");
        id button = cell.rightButtons[index];
        UIButton *pressedBtn = button;
        [pressedBtn setTitle:@"Less" forState:UIControlStateNormal];
        NSLog(@"pressedBtn title: %@",pressedBtn.titleLabel.text);

    }
    ... 
}

备注:更新:添加此部分是为了在评论期间提供帮助:

关于您的个人项目,您必须向数据源添加任何布尔变量,我举一个例子(使用Swift代码):

因此,在填充数据源后,可以使用委托:

func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
...
    if (direction == MGSwipeDirection.RightToLeft && index == 2){ 
        if let _= cell?.rightButtons[index]{
           let cellData:rowData = myDataSource[index]
           if cellData.isSaved { 
                cellData.buttonTitle1 = "Just saved"
                cellData.isSaved = true
           } else {
                cellData.buttonTitle1 = "Save"
                cellData.isSaved = false
           }
           myDataSource[index] = cellData
           // datasource is modified so launch reload data to refresh layout and call cellForRow..
          // There are other softly methods than reloadData to update tableView layout but I use it just for example..
          self.tableView.reloadData()
        } 
    }
...
}

获取按钮按下事件不是问题所在。问题是,我想在保存项目后将标题从“保存”更改为“删除”。功能正常,但标题保持不变。当我再次刷卡时,我会得到相同的标题。首先,试着看看你是否获得了按钮。下一个工作尝试更改标题,我在这里帮助你。PS我有一个工作项目在这里,我可以修改与我的答案中描述的方法按钮标题。如果无法获取此委托中的按钮,则可能是按钮创建和委托设置有问题。谢谢。你的例子很有效。我是否需要手动将委托方法绑定到按钮上?我刚刚在Swift中实现了相同的代码,但它似乎不起作用。不客气,请查找mgswipateblecell/mgswipateblecell.h报告“委托”解释注释的行。如果你喜欢这个答案,请报告为这个问题选择的答案。我用这个方法遇到了另一个问题。我似乎无法访问
let cell=tableView.dequeueReusableCellWithIdentifier(“MyViewCell”,forIndexPath:indexPath)作为!MyViewCell
是否还需要委托方法?
func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
...
    if (direction == MGSwipeDirection.RightToLeft && index == 2){ 
        if let _= cell?.rightButtons[index]{
           let cellData:rowData = myDataSource[index]
           if cellData.isSaved { 
                cellData.buttonTitle1 = "Just saved"
                cellData.isSaved = true
           } else {
                cellData.buttonTitle1 = "Save"
                cellData.isSaved = false
           }
           myDataSource[index] = cellData
           // datasource is modified so launch reload data to refresh layout and call cellForRow..
          // There are other softly methods than reloadData to update tableView layout but I use it just for example..
          self.tableView.reloadData()
        } 
    }
...
}