简体   繁体   English

单击表格视图中的按钮时,如何更改表格视图之外的标签文本

[英]How to change label text that is outside the tableview when a button in the tableview is click

The problem is i have a tableview with custom cells. 问题是我有一个带有自定义单元格的tableview。 Inside the cell, i have a button. 在单元内,我有一个按钮。 When the button is click, it should change the label text that is outside the tableview. 单击按钮时,它应更改表格视图之外的标签文本。

Edit: the label's text that I'm trying to change is on a different view. 编辑:我要更改的标签文本位于不同的视图上。 The ViewController has a tableview and a label view. ViewController有一个表视图和一个标签视图。 Inside the tableview is a button and when it is click it changes the label's view text. 在tableview内部是一个按钮,单击该按钮会更改标签的视图文本。

CustomCell and TableSource: CustomCell和TableSource:

namespace Testing.iOS
{
    public partial class CustomCell : UITableViewCell
    {

        // Code omitted

        public class TableSource : UITableViewSource
        {  
            ViewController vc = new ViewController();

            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell;

                if (cell == null) 
                {
                    cell = new CustomCell ();
                    var views = NSBundle.MainBundle.LoadNib("CustomCell", cell, null);
                    cell = Runtime.GetNSObject( views.ValueAt(0) ) as CustomCell;
                }

                cell.btn.TouchUpInside += (object sender, EventArgs e) => 
                {     
                    // Change label that is outside tableview
                    vc.updateLabel();
                };

                // Code omitted

                return cell;
            }
        }
    }
}

View Controller: 视图控制器:

namespace Testing.iOS
{
    public partial class ViewController : UIViewController
    {    
        public ViewController () : base ("ViewController", null)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            tableView.RegisterNibForCellReuse (CustomCell.Nib, "CustomCell");
            tableView.Source = new CustomCell.TableSource();
        }

        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
        }

        public void updateLabel()
        {
            label.Text = "testing";
        }
    }
}

Use protocols and delegates. 使用协议和委托。 First write a CustomCellDelegate protocol in CustomCell class with method updateLabel() also pass the self as one of the argument. 首先使用方法updateLabel()CustomCell类中编写CustomCellDelegate协议, CustomCell self作为参数之一传递。

-(void)updateLabelForCell:(UITabelViewCell *)cell;

and in the updateLabel() method, call the delegate method 然后在updateLabel()方法中,调用委托方法

[self updateLabelForCell:self];

then in the ViewController class, implement the delegate method. 然后在ViewController类中,实现委托方法。 based on the function called update the label. 基于称为更新标签的功能。 set your ViewController as delegate for the tableviewcell. 将您的ViewController设置为tableviewcell的委托。

In cellForRowAtIndexPath method add following code 在cellForRowAtIndexPath方法中添加以下代码

[CustomCell.buttonOutlet addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];


Then handle your logic in BtnClicked method. 然后使用BtnClicked方法处理您的逻辑。

-(void)BtnClicked:(UIButton *)sender{

       labelOutlet.text = @"new text";

}


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

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