[英]Get UItextFields text from custom UITableView
I have a custom UITableViewCell
with a UITextField
(which is linked to the custom cells class). 我有一个带有
UITextField
的自定义UITableViewCell
(链接到自定义单元格类)。 I am trying to access the textField
from my VC class. 我正在尝试从我的VC类访问
textField
。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if ([indexpath row] == 2) {
menuCell.nameTextField.delegate = self;
}
return cell;
}
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
}
How do I get the textFields
text from textFieldDidEndEditing
? 如何从
textFieldDidEndEditing
获取textFields
文本?
Depending on where you want to access this text depends on how difficult it is. 根据您要访问此文本的位置,取决于文本的难易程度。
Want to access the text in cellForRowAtIndex - (very easy) 想要访问cellForRowAtIndex中的文本-(非常简单)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; if ([indexpath row] == 2) { menuCell.nameTextField.delegate = self; } NSString * text = menuCell.nameTextField.text; return cell;
If you want to access the text anywhere in the VC and the menuCell is unique (there is only one of them) - (medium difficult) 如果要在VC中的任何位置访问文本,并且menuCell是唯一的(其中只有一个)-(中等难度)
In your header file add the custom cell as a class 在头文件中,将自定义单元格添加为类
@class menuCell;
This means you can set it a variable in the interface 这意味着您可以在界面中将其设置为变量
menuCell * _menuCell;
Next in cellForRowAtIndex you want to allocate this custom cell 接下来要在cellForRowAtIndex中分配此自定义单元格
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == bCustomCellSection) {
if (!_menuCell) {
_menuCell = [tableView_ dequeueReusableCellWithIdentifier:bProfileNameCell];
_menuCell.nameTextField.delegate = self;
}
_menuCell.nameTextField.placeholder = @"Name";
_menuCell.selectionStyle = UITableViewCellSelectionStyleNone;
return _menuCell;
}
...
}
This means that we now have access to the menu cell from anywhere in the VC and can get the text by calling 这意味着我们现在可以从VC中的任何位置访问菜单单元,并且可以通过调用获取文本
_menuCell.nameTextField.text
I have never done this but would probably do it one of two ways 我从未做过,但可能会用以下两种方法之一
a) Create an array and as we are creating the custom cells add a pointer to the textFields to the array each time. a)创建一个数组,并在创建自定义单元格时向数组添加每次指向textFields的指针。 We can then access the textField we want from that array
然后,我们可以从该数组访问所需的textField
For this method I would add the custom cells to a mutable array defined in the interface 对于这种方法,我会将自定义单元格添加到接口中定义的可变数组中
NSMutableArray * cellsArray;
remember to initialise it in viewDidLoad 记得在viewDidLoad中初始化它
cellsArray = [NSMutableArray new];
Then in cellForRowAtIndex i would add the cell each time 然后在cellForRowAtIndex中,我每次都会添加单元格
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
[cellsArray addObject: menuCell];
This obviously depends on how many sections we have. 显然,这取决于我们有多少部分。 If we have more than one section it gets more complicated again:
如果我们有多个部分,它会变得更加复杂:
Then we would need to add an array for each section to an overall array. 然后,我们需要为每个部分的整个数组添加一个数组。 This is quite complicated and could have a whole question on its own, there is a good link of how to do this here :
这是相当复杂的,可能有一个整体的问题,对自己,有一个如何做一个很好的链接在这里 :
Once you have an array of cells (or an array of arrays of cells) you can call the cell you want based on the indexPath and get the textField 一旦有了一个单元格数组(或一个单元格数组),就可以基于indexPath调用所需的单元格并获取textField
b) Call a pointer to the specific cell we want b)调用一个指向我们想要的特定单元格的指针
menuCell * menuCell = [self tableView:table cellForRowAtIndexPath:indexPath];
and then get the textField from this cell as we did previously. 然后像以前一样从该单元格获取textField。
Remember you can calculate your own indexPath if you want to create one outside of cellForRow: 请记住,如果要在cellForRow之外创建一个indexPath,则可以计算自己的indexPath:
NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
This method is pretty good if you want to access a specific cell but a bit cumbersome if you want to access it a lot and keep having to call this code all over your VC 如果您要访问特定的单元格,则此方法非常好,但如果要大量访问该单元格,并且必须在整个VC上始终调用此代码,则此方法会比较麻烦
Hope this helps 希望这可以帮助
If you are asking how to get the text from the delegate method textFieldDidEndEditing, then you simply do this: 如果您询问如何从委托方法textFieldDidEndEditing中获取文本,则只需执行以下操作:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
NSString *textFieldText = textfield.text;
}
However, if you have multiple textFields and you want to know what textfield is calling the delegate, you could tag your textField: 但是,如果您有多个textField,并且想知道哪个textfield正在调用委托,则可以标记textField:
[myTextField setTag:indexPath.row]
and then put a if statement in the delegate textFieldDidEndEditing like this: 然后将if语句放入委托textFieldDidEndEditing中,如下所示:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
if(textfield.tag == index0) do something..
else if(textfield.tag == index1) do something..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.