How Can I access the textfields in static TableViewcells without creating referencing outlet?

て烟熏妆下的殇ゞ 提交于 2019-12-13 09:39:57

问题


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:

  1. Set tag to your textField:

    textField.tag = kTextFieldTag;
    
  2. Retrieve subView of cell with that tag

    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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!