问题是我有自定义单元格的tableview。在单元格里面,我有一个按钮。单击该按钮时,它应该更改tableview之外的标签文本。
编辑: 我试图改变的标签文字是另一种观点。 ViewController有一个tableview和一个标签视图。在tableview内部是一个按钮,单击它时会更改标签的视图文本。
CustomCell和TableSource:
//Stores the response object that will be sent back to the android client
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
String description = "User added";
response.StatusCode = System.Net.HttpStatusCode.OK;
//Tries to add the new user
try
{
userTable.Insert(username,firstname,lastname,email,hash);
}
catch (SqlException e)
{
//Default response is a conflict
response.StatusCode = System.Net.HttpStatusCode.Conflict;
description = "Bad Request (" + e.Message + ")";
//Check what the conflict is
if (userTable.GetData().AsEnumerable().Any(row => username == row.Field<String>("username")))
{
description = "Username in use";
}
else if (userTable.GetData().AsEnumerable().Any(row => email == row.Field<String>("email")))
{
description = "Email address in use";
}
else
{
response.StatusCode = System.Net.HttpStatusCode.BadRequest;
}
}
//display and respond with the description
Console.WriteLine(description);
response.StatusDescription = description;
}
查看控制器:
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;
}
}
}
}
答案 0 :(得分:0)
使用协议和代理。
首先在CustomCellDelegate
类中使用方法CustomCell
编写updateLabel()
协议,并将self
作为参数之一传递。
-(void)updateLabelForCell:(UITabelViewCell *)cell;
并在updateLabel()
方法中调用委托方法
[self updateLabelForCell:self];
然后在ViewController
类中实现委托方法。基于名为update的标签的函数。
将ViewController
设置为tableviewcell的委托。
答案 1 :(得分:0)
在cellForRowAtIndexPath方法中添加以下代码
[CustomCell.buttonOutlet addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
然后用BtnClicked方法处理你的逻辑。
-(void)BtnClicked:(UIButton *)sender{
labelOutlet.text = @"new text";
}