英文:
Execute button click event on button in UICollectionViewCell
问题
以下是您提供的文本的翻译部分:
我有一个包含按钮的集合视图单元格,现在我需要编写按钮点击事件来处理点击。我应该在哪里进行这个操作?我尝试了集合视图源、单元格代码和其他各种位置,但都不起作用,事实上,我甚至无法通过智能感知。有人知道我怎么做吗?
我的单元格代码:
public DeptMemberCell(CGRect frame) : base(frame)
{
btnRemoveFromDept.SetTitle("Remove From Dept.", UIControlState.Normal);
btnRemoveFromDept.BackgroundColor = UIColor.FromRGB(255, 215, 0);
btnRemoveFromDept.SetTitleColor(UIColor.Black, UIControlState.Normal);
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.White;
ContentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
imageView = new UIImageView(UIImage.FromBundle("placeholder.png"));
imageView.Center = ContentView.Center;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
lblName = new UILabel();
lblName.Frame = new CGRect(0, ContentView.Bounds.Height - 26, ContentView.Bounds.Width + 25, 26);
btnRemoveFromDept.Frame = new CGRect(0, ContentView.Bounds.Height, ContentView.Bounds.Width + 25, 26);
UIView MyContentView = new UIView();
MyContentView.Add(imageView);
MyContentView.Add(lblName);
MyContentView.Add(btnRemoveFromDept);
MyContentView.Frame = new CGRect(0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height);
MyContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
MyContentView.AddConstraints(
imageView.AtTopOf(MyContentView, 0),
imageView.AtLeftOf(MyContentView, 0),
lblName.Below(imageView, 2),
lblName.AtLeftOf(MyContentView, 0),
lblName.Width().EqualTo(frame.Width),
btnRemoveFromDept.Below(lblName, 2),
btnRemoveFromDept.AtLeftOf(MyContentView, 0)
);
ContentView.AddSubview(MyContentView);
}
所有东西都出现在应该出现的地方,并且按设计的方式运行,除了我似乎无法弄清楚在哪里/如何处理按钮点击事件。我正在使用Visual Studio v17.6.2。
英文:
I have a collection vew cell which contains a button, I now need to code the button click event to handle the click. Where would I do that? I tried the collection view source, the cell code and various other places but nothing works, in fact I can't even get past intellisense. Anyone know how I can do this?
My Cell Code:
public DeptMemberCell(CGRect frame) : base(frame)
{
btnRemoveFromDept.SetTitle("Remove From Dept.", UIControlState.Normal);
btnRemoveFromDept.BackgroundColor = UIColor.FromRGB(255, 215, 0);
btnRemoveFromDept.SetTitleColor(UIColor.Black, UIControlState.Normal);
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.White;
ContentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
imageView = new UIImageView(UIImage.FromBundle("placeholder.png"));
imageView.Center = ContentView.Center;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
lblName = new UILabel();
lblName.Frame = new CGRect(0, ContentView.Bounds.Height - 26, ContentView.Bounds.Width + 25, 26);
btnRemoveFromDept.Frame = new CGRect(0, ContentView.Bounds.Height, ContentView.Bounds.Width + 25, 26);
UIView MyContentView = new UIView();
MyContentView.Add(imageView);
MyContentView.Add(lblName);
MyContentView.Add(btnRemoveFromDept);
MyContentView.Frame = new CGRect(0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height);
MyContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
MyContentView.AddConstraints(
imageView.AtTopOf(MyContentView, 0),
imageView.AtLeftOf(MyContentView, 0),
lblName.Below(imageView, 2),
lblName.AtLeftOf(MyContentView, 0),
lblName.Width().EqualTo(frame.Width),
btnRemoveFromDept.Below(lblName, 2),
btnRemoveFromDept.AtLeftOf(MyContentView, 0)
);
ContentView.AddSubview(MyContentView);
}
Everything appears where it should and functions as designed except I cannot seem to figure out where/how to handle the button click event. I'm using Visual Studio v17.6.2.
答案1
得分: 0
以下是代码的翻译部分:
嗯,显然我并没有尝试 EVERYTHING。我将以下代码行放入了我的 DeptMemberCell 中:
this.btnRemoveFromDept.TouchUpInside += (object sender, EventArgs e) =>
{
int x = 0;
x++;
};
我在 `x++` 上设置了断点,并测试了代码,它完美运行。我的代码现在如下:
public DeptMemberCell(CGRect frame) : base(frame)
{
btnRemoveFromDept.SetTitle("从部门中删除", UIControlState.Normal);
btnRemoveFromDept.BackgroundColor = UIColor.FromRGB(255, 215, 0);
btnRemoveFromDept.SetTitleColor(UIColor.Black, UIControlState.Normal);
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.White;
ContentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
imageView = new UIImageView(UIImage.FromBundle("placeholder.png"));
imageView.Center = ContentView.Center;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
lblName = new UILabel();
lblName.Frame = new CGRect(0, ContentView.Bounds.Height - 26, ContentView.Bounds.Width + 25, 26);
btnRemoveFromDept.Frame = new CGRect(0, ContentView.Bounds.Height, ContentView.Bounds.Width + 25, 26);
UIView MyContentView = new UIView();
MyContentView.Add(imageView);
MyContentView.Add(lblName);
MyContentView.Add(btnRemoveFromDept);
MyContentView.Frame = new CGRect(0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height);
MyContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
MyContentView.AddConstraints(
imageView.AtTopOf(MyContentView, 0),
imageView.AtLeftOf(MyContentView, 0),
lblName.Below(imageView, 2),
lblName.AtLeftOf(MyContentView, 0),
lblName.Width().EqualTo(frame.Width),
btnRemoveFromDept.Below(lblName, 2),
btnRemoveFromDept.AtLeftOf(MyContentView, 0)
);
ContentView.AddSubview(MyContentView);
this.btnRemoveFromDept.TouchUpInside += (object sender, EventArgs e) =>
{
int x = 0;
x++;
};
}
希望对你有所帮助。
英文:
Well, apparently I didn't try EVERYTHING. I put the following lines of code in my DeptMemberCell:
this.btnRemoveFromDept.TouchUpInside += (object sender, EventArgs e) =>
{
int x = 0;
x++;
};
I put a break on the x++
and tested the code, worked like a charm. My code now is as follows:
public DeptMemberCell(CGRect frame) : base(frame)
{
btnRemoveFromDept.SetTitle("Remove From Dept.", UIControlState.Normal);
btnRemoveFromDept.BackgroundColor = UIColor.FromRGB(255, 215, 0);
btnRemoveFromDept.SetTitleColor(UIColor.Black, UIControlState.Normal);
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.White;
ContentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
imageView = new UIImageView(UIImage.FromBundle("placeholder.png"));
imageView.Center = ContentView.Center;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
lblName = new UILabel();
lblName.Frame = new CGRect(0, ContentView.Bounds.Height - 26, ContentView.Bounds.Width + 25, 26);
btnRemoveFromDept.Frame = new CGRect(0, ContentView.Bounds.Height, ContentView.Bounds.Width + 25, 26);
UIView MyContentView = new UIView();
MyContentView.Add(imageView);
MyContentView.Add(lblName);
MyContentView.Add(btnRemoveFromDept);
MyContentView.Frame = new CGRect(0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height);
MyContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
MyContentView.AddConstraints(
imageView.AtTopOf(MyContentView, 0),
imageView.AtLeftOf(MyContentView, 0),
lblName.Below(imageView, 2),
lblName.AtLeftOf(MyContentView, 0),
lblName.Width().EqualTo(frame.Width),
btnRemoveFromDept.Below(lblName, 2),
btnRemoveFromDept.AtLeftOf(MyContentView, 0)
);
ContentView.AddSubview(MyContentView);
this.btnRemoveFromDept.TouchUpInside += (object sender, EventArgs e) =>
{
int x = 0;
x++;
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论