2014-03-13 45 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

相關問題