简体   繁体   English

自定义UITableViewCell按钮单击

[英]Custom UITableViewCell button click

In a UIViewController there is a UITableView which consists custom UITableViewCell with a UIButton. 在UIViewController中,有一个UITableView,其中包含带有UIButton的自定义UITableViewCell。

When the user clicks the button (in the custom UITableViewCell), I want to display a message in the hosting UIViewController - what is the right way to achieve that? 当用户单击按钮(在自定义UITableViewCell中)时,我想在托管UIViewController中显示一条消息- 实现该目标的正确方法是什么? How should I notify the UIViewController that the button was clicked? 我应该如何通知UIViewController该按钮被单击?

您应该使用委托来告诉UIViewController单元中发生了某些事情。

I'm not exactly sure what kind of message you are referring to but if you mean a you want to show a popup just create a UIAlertView and show it when the button is clicked in the cell. 我不确定您要指的是哪种消息,但是如果您要显示的是弹出窗口,则只需创建UIAlertView并在单元格中单击按钮时显示即可。

However, if you mean change a UIlabel text or whatever as a message on the ViewController you can always pass whatever you need to change into the TableView and Cell or the UIViewController itself into it. 但是,如果您要更改UIlabel文本或作为ViewController上的消息的内容,则始终可以将需要更改的内容传递到TableView和Cell或UIViewController本身。

I usually create a method on the custom cells class and pass it whatever I need into the custom TableView's constructor. 我通常在自定义单元格类上创建一个方法,并将所需的任何方法传递给自定义TableView的构造函数。 Then in the TableView's GetCell() method pass whatever I need to change into that method I created on the cell and save it as a variable on the cell. 然后在TableView的GetCell()方法中,将需要更改的所有内容传递到我在单元格上创建的方法中,并将其另存为单元格中的变量。

If you've initiated your UITableViewCell, and added a button to the cell, you can use a Delegate when the button is clicked, something like this: 如果已启动UITableViewCell并向该单元格添加了一个按钮,则单击该按钮时可以使用Delegate,如下所示:

UIButton button = new UIButton (cell.Bounds.X, cell.Bounds.y, cell.Bounds.Width, cell.Bounds.Height);
button.TouchUpInside += (object sender, EventArgs e) => 
{
    label.text = "Blablabla";
};

Where the label is, is up to you. 标签的位置取决于您。 I hope this helps, good luck! 希望对您有帮助,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM