Question

I want to get index of touched Text Box in tableview cell. The fact that I could do it before IOS 7. In the past I could get the text box index in below. This method also fails to IOS 7. I read tag should use instead of this method. But I also use tag for each different text box. Because Clicking to the text box I am doing different things, for example opening the picker or opening to the selection view. Plase help me, how can I get the row number of each text box When touched to text box.

tablevie form

-(NSInteger)getTextFieldCellRow:(id)element{
    UITableViewCell *cell = (UITableViewCell *)[[(UITextField*)element superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    return textFieldIndexPath.row;

}
Was it helpful?

Solution

Ok, I solved

-(NSIndexPath *) getButtonCellRow:(UITextField *) b {
    UITableView *tv = nil;
    UIView *superViewTemp = [b superview];
    UITableViewCell *cell=nil;

    BOOL isFoundTable=FALSE;
    BOOL isFoundCell=FALSE;

    while(superViewTemp != nil && ![superViewTemp isKindOfClass:[UIWindow class]]){

        if ([superViewTemp isKindOfClass:[UITableViewCell class]]) {
            cell=(UITableViewCell*)superViewTemp;
            isFoundCell = TRUE;

        }else if ([superViewTemp isKindOfClass:[UITableView class]]){
            tv = (UITableView*)superViewTemp;
            isFoundTable = TRUE;
        }

        superViewTemp = [superViewTemp superview];

        if(isFoundCell && isFoundTable){
            break;
        }
    }

    if(tv != nil && cell != nil){

        return [tv indexPathForCell:(UITableViewCell*)cell];
    }
    return nil;
}

OTHER TIPS

this should be a very simple solution

UIView *view = textField;

while (![view isKindOfClass:[UITableViewCell class]])

{

view = [view superview];

}

CellClass *cell = (CellClass *)view;

Try with following code:

UIView *contentView = (UIVIew *)[textfield superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
NSLog(@"%d", indexPath.row);

But in iOS 7, you can get UITableViewCell by following code:

UITableViewCell *cell = (UITableViewCell *)[[contentView superview] superview];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top