2014-03-13 49 views
1

我有一个带有自定义单元格的TableView,单元格中有一个标签和一个文本框。我想在选中该行时关注文本框。RowSelected在TableCell中编辑TextField

任何人知道如何在这段代码解决这个问题:

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
{ 
    //EDIT TEXTFIELD FROM SELECTED ROW 
} 

我的文本框确实是一个自定义单元格类。 我试过这个:

CustomCell cell = tableView.CellAt (indexPath) as CustomCell; 
cell.textField.BecomeFirstResponder(); 

但是textField永远找不到。

这是我CustomCell类:

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Forms.iOS 
{ 
public class CustomCell : UITableViewCell 
{ 

    UITextField textField; 
    UILabel label; 

    public CustomCell (NSString cellId) : base (UITableViewCellStyle.Value1, cellId) 
    { 
     textField = new UITextField(); 
     label = new UILabel(); 
     ContentView.Add (label); 
     ContentView.Add (textField); 
    } 

    public void UpdateCell (string textFieldValue, string labelValue) 
    { 
     DetailTextLabel.Text = "Dit is echt nutteloze tekst maar geen idee waarvoor ik dit hier nu neer zet maar zodat het in ieder geval te veel is."; 
     textField.Placeholder = textFieldValue; 
     TextLabel.Text = labelValue; 
    } 

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

     DetailTextLabel.Hidden = true; 
     RectangleF detailFrame = DetailTextLabel.Frame; 
     textField.Frame = detailFrame; 
    } 

} 
} 
+1

为了任何人在看这个。 UITextField textField;需要公开才能在课堂以外访问(或者有吸气)。 – Hobsie

回答

3

我相信你会需要选择行时使文本字段中的第一个响应者。

yourUITextField.BecomeFirstResponder(); 

如果您的UITextField是某种形式的自定义单元格类的,你可以尝试抢细胞在使用CellAt那个位置,它铸造的自定义类和访问它的方式中的一员。

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
{ 
    CustomUITableViewCellClass customCell = tableView.CellAt(indexPath) as CustomUITableViewCellClass; 

    if (customCell !=null) 
    { 
     customCell.yourUITextField.BecomeFirstResponder(); 
    } 
    else 
    { 
     // Cell at indexPath cannot be cast to CustomUITableViewCellClass 
    } 
} 
+0

是的,我也发现,但我的UITextField是在一个不同的类,我不怎么可以在我的RowSelected方法。 –

+0

你的UITextField在哪个类中?如果你有一个单元格的自定义类,并且UITextField是这个类的成员,那么你可以尝试获取该indexPath处的单元格,并将它转换为自定义类来访问成员。 CustomUITableViewCellClass cell = tableView.CellAt(indexPath)as CustomUITableViewCellClass; 我会编辑我的答案。 – Hobsie

+0

我不能评论你的帖子,因为我还没有50分,但我不会想到你已经添加了什么可以编译?你需要指定UITextField的textField; UILabel标签;作为公众或创建获取/设置来访问它们。 – Hobsie

相关问题