gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:41:40 26 4
gpt4 key购买 nike

我的 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);
}
}

最佳答案

它不起作用,因为您正在删除一个新创建的手势,而不是它可能已有的手势。您必须使用 ValueLabel.gestureRecognizers 检索手势数组,然后使用 for 循环删除每个手势。

关于ios - 如何从 UITableViewCell 中的标签中删除手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39332859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com