如何从UITableViewCell中的标签中删除手势识别器?

时间:2016-09-05 14:34:28

标签: ios uitableview

我的ViewController中有底部工具栏,上面有一个TableView。工具栏中间有一个日期标签,左侧和右侧有下一个和上一个按钮。基于所选日期tableview内容的变化..

现在TableViewCell包含一个UILabel。我想只在选定的日期是今天时才将Gesture添加到标签中。

所以我在我的单元格更新方法中写道

UITapGestureRecognizer gesture = new UITapGestureRecognizer();
gesture.AddTarget(() => HandleValueLabelClick());
if (source.parentController.selectedDateTime.Day == DateTime.Now.Day)
{
    AddEditAction();
    ValueLabel.AddGestureRecognizer(gesture);
}
else
{
    ValueLabel.RemoveGestureRecognizer(gesture);
}

但是如果所选日期不是今天,则删除手势,但不起作用。任何帮助表示赞赏..

编辑:

public partial class ProgramCalendarCell : UITableViewCell
{
    NSIndexPath indexPath;

    ProgramVitalsCalendarTableSource source;
    ProgramVital vital;
    ProgramVitalCalendar calendar;

    public ProgramCalendarCell (IntPtr handle) : base (handle)
    {
    }

    public void UpdateCell(ProgramVital vital, ProgramVitalCalendar calendar, NSIndexPath indexPath, ProgramVitalsCalendarTableSource source)
    {
        this.source = source;
        this.indexPath = indexPath;
        this.vital = vital;
        this.calendar = calendar;

        InitVitalName();
        InitVitalValue();

        NewValueTextField.Hidden = true;
        ValueLabel.Hidden = false;

        UIView separatorLine = new UIView(new CoreGraphics.CGRect(0, 44, 1200f, 0.5f));
        separatorLine.BackgroundColor = AZConstants.SeparatorColor;
        ContentView.AddSubview(separatorLine);

        UITapGestureRecognizer gesture = new UITapGestureRecognizer();
        gesture.AddTarget(() => HandleValueLabelClick());
        if (source.parentController.selectedDateTime.Day == DateTime.Now.Day)
        {
            AddEditAction();
            ValueLabel.AddGestureRecognizer(gesture);
        }
        else
        {
            ValueLabel.RemoveGestureRecognizer(gesture);
        }
    }

    void InitVitalName()
    {
        string name = vital.vitalName;
        if (!String.IsNullOrEmpty(vital.unitName))
            name += " (" + System.Net.WebUtility.HtmlDecode(vital.unitName) + ")";
        VitalNameLabel.Text = name;
    }

    void InitVitalValue()
    {
        string value = "";
        string color = "";
        if (calendar != null)
        {
            value = calendar.values[0].value;
            color = calendar.values[0].color;
        }
        UIHelper.SetVitalValueTileBackGround(ValueLabel, value, color);
    }

    void HandleValueLabelClick()
    {
        ValueLabel.Hidden = true;
        NewValueTextField.Hidden = false;
        NewValueTextField.BecomeFirstResponder();
    }

    void AddEditAction()
    {
        ValueLabel.UserInteractionEnabled = true;
        NewValueTextField.ShouldReturn = (textField) =>
        {
            textField.ResignFirstResponder();
            ValueLabel.Hidden = false;
            NewValueTextField.Hidden = true;
            Console.WriteLine("Row: " + indexPath.Row);
            return true;
        };

        UIToolbar toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, (float)UIScreen.MainScreen.Bounds.Size.Width, 44.0f));
        toolbar.BarTintColor = AZConstants.PrimaryColor;
        toolbar.TintColor = UIColor.White;
        toolbar.Items = new UIBarButtonItem[]{
            new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
                new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate {
                    Console.WriteLine("Row: " + indexPath.Row);
                    SaveReading();
                    NewValueTextField.ResignFirstResponder();
                })
            };
        toolbar.BarTintColor = AZConstants.PrimaryColor;
        toolbar.TintColor = UIColor.White;
        toolbar.Translucent = true;
        toolbar.SizeToFit();
        NewValueTextField.InputAccessoryView = toolbar;

        int vId = Int32.Parse(vital.vitalId);

        if (vId == 20 || vId == 5 || vId == 496)
            NewValueTextField.KeyboardType = UIKeyboardType.DecimalPad;
        else
            NewValueTextField.KeyboardType = UIKeyboardType.NumberPad;
    }

    async void SaveReading()
    {

        var hud = UIHelper.GetProgressHud(source.parentController.View, "");
        hud.Show(animated: true);
        Status status = await VitalHelper.postVitalValue(Constants.__IOS__, vital, NewValueTextField.Text, 0,
                                                         DateTime.Now.ToString("MM/dd/yyyy"), DateTime.Now.ToString("hh:mm tt"), "");

        if (status.status)
        {
            source.parentController.FetchAndDisplayVitalValues();
        }
        else
        {
            new UIAlertView("Error", status.message, null, "OK", null).Show();
        }
        hud.Hide(animated: true, delay: 0);
    }
}

1 个答案:

答案 0 :(得分:1)

它不起作用'因为你要删除新创建的手势,而不是它已经拥有的手势。 您必须使用ValueLabel.gestureRecognizers检索手势数组,然后使用for循环删除每个手势数组。