我创建了一个自定义UITableViewCell
,其中包含一个UIButton
。在 iOS 6 中,它始终按预期运行。在 iOS 7 中,第一次加载视图后它看起来是正确的。但是在TableView.ReloadData()
按钮上的任何文本消失之后,直到我触摸按钮并滑开以不触发点击事件。
您可以在电影中看到描述的行为:http: //youtu.be/9SrKfouah7A
ButtonCellNew.cs
using System;
using MonoTouch.UIKit;
using System.Drawing;
namespace B2.Device.iOS
{
public class ButtonCellNew : UITableViewCell
{
private string _reuseIdentifier;
private bool _eventRegistered;
public Action<object, EventArgs> ButtonClickedAction;
public UIButton Button { get; set; }
public ButtonCellNew(string reuseIdentifier) : base()
{
_reuseIdentifier = reuseIdentifier;
Initialize();
}
public override string ReuseIdentifier
{
get
{
return _reuseIdentifier;
}
}
private void Initialize()
{
// Cell
SelectionStyle = UITableViewCellSelectionStyle.None;
// Button
Button = new UIButton(UIButtonType.Custom);
Button.Frame = Bounds;
Button.Font = UIFont.BoldSystemFontOfSize(15);
Button.SetTitleColor(Colors.ButtonTitle, UIControlState.Normal);
Button.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
ContentView.AddSubview(Button);
}
public override void PrepareForReuse()
{
base.PrepareForReuse();
Button.TitleLabel.Text = string.Empty;
}
public void RegisterEvents()
{
ButtonClickedAction = null;
if (!_eventRegistered)
{
Button.TouchUpInside += ButtonClicked;
_eventRegistered = true;
}
}
private void ButtonClicked(object sender, EventArgs e)
{
if (ButtonClickedAction != null)
ButtonClickedAction(sender, e);
}
}
}
MyTableViewController.cs
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
else if (indexPath.Section == (int)SupportTableViewSection.Button && indexPath.Row == (int)SupportTableViewButtonRow.Close)
{
var cell = tableView.DequeueReusableCell("buttonCell") as ButtonCellNew;
if (cell == null)
cell = new ButtonCellNew("buttonCell");
cell.Button.SetTitle("Support Einstellungen schliessen", UIControlState.Normal);
cell.RegisterEvents();
cell.ButtonClickedAction = ExitSupportSettings;
return cell;
}
}