问题
I have lot of text fields statically placed in static TableViewcells. How can access the text in it without creating referencing outlet for each textfield? I have tried taking subviews of cell. It's not working. Any alternatives?
I tried this. It's not working.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView
cellForRowAtIndexPath:indexPath];
for (UIView *subview in [cell subviews]){
if([subview isKindOfClass:[UITextField class]]){
UITextField *inputField = (UITextField *)subview;
NSLog(@"%@",inputField.text);
}
}
return cell;
}
回答1:
You could use an IBOutletCollection. That lets you connect them all to one array, where you can access them by index, or use setValue:forKey: to address them all at once.
回答2:
You can always iterate through every subviews of your cell to find a UITextField
. Something like:
NSArray *subviews = [cell subviews];
for (view in subviews) {
if([view isKindOfClass:[UITextField class]]) {
//Do what you want with your UITextField
}
}
回答3:
Follow these steps:
Set
tag
to yourtextField
:textField.tag = kTextFieldTag;
Retrieve
subView
ofcell
with thattag
UITextField *refTextField = [cell viewWithTag:kTextFieldTag];
Here, kTextFieldTag
is constant.
来源:https://stackoverflow.com/questions/14824608/how-can-i-access-the-textfields-in-static-tableviewcells-without-creating-refere