3

I had a custom UItableViewCell class named EditableTableViewCell that (besides other things) has a UITextField inside added to its contentView. In the Textfield delegate method I'm trying to detect to which section it belongs to. Like this:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    //traverse up in the view hierarchy until we find the cell's content view.
    while (![contentView isKindOfClass:[EditableTableViewCell class]] || contentView.superview == nil) {
        contentView = [contentView superview];
    }
    EditableTableViewCell *thiscell = (EditableTableViewCell*)contentView;
    NSIndexPath *indexpath =[[self tableView]indexPathForRowAtPoint:thiscell.center];

This gives me the correct result and I saw a similar solution here that also navigates the view hierarchy. I'm wondering if this is the only solution or if there's something more appropriate that doesn't involve looping for all the available cells.

Community
  • 1
  • 1
Andres Bucci
  • 947
  • 1
  • 9
  • 37

2 Answers2

5

Try this, no need to loop through the view

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    CGPoint center = [self.tableView convertPoint:textField.center fromView:contentView];
    NSIndexPath *indexpath =[[self tableView] indexPathForRowAtPoint:center];
    NSLog(@"indexPath:%@", indexpath);
}
lanbo
  • 1,678
  • 12
  • 12
  • you should put the @ before the NSLog. I tried editing it but It doesn't let me. This works, but i don't know if it's more efficient, since my implementation also works. Do you know how to test this? – Andres Bucci Sep 26 '13 at 09:10
  • yes, Thanks, just edit the codes. "c string" is a C string , but not a NSString, I am sorry for that – lanbo Sep 26 '13 at 09:25
  • my codes should be more generic, and your codes rely on EditableTableViewCell, – lanbo Sep 26 '13 at 09:27
0

Why not

NSIndexPath *indexpath = [(UITableView*)self.superview indexPathForCell:self]

?

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • well, because before iOS7 the cell was found like textfield.superview.superview; otherwise the indexpath always returned 0 – Andres Bucci Sep 26 '13 at 09:04
  • 2
    .superview is a code smell to me. .superview.superview is a real stink. A project I'm working on had lots of this and it's broken now in iOS 7. It smelt like a problem, and it ended up being a problem. There is always a better way than doing .superview, which is lazy. – bandejapaisa Sep 26 '13 at 09:23
  • Which object is the delegate of the text view? The cell itself or the view controller? – Léo Natan Sep 26 '13 at 09:24
  • @bandejapaisa I agree. There is bad design here in the question. – Léo Natan Sep 26 '13 at 09:24
  • @bandejapaisa I also think is bad, but the other alternative was tags, which I also don't like very much, specially with tableviewcells that get created and deleted. By the way, nice nickname ;) la bandeja paisa rules! – Andres Bucci Sep 26 '13 at 09:55
  • @LeoNatan the view controller. – Andres Bucci Sep 26 '13 at 09:56