简体   繁体   中英

UITableView not calling didSelectRowAtIndexPath on only iOS7 compiled with Xcode 5

I have a UITableView on an iOS app (with no IBOutlet) that gets initated and built within it's own class. The width of the tableview is based on the width of a text field and has a fixed height. There is also a thin black frame around the tableview.

The initiation statement is as follows:

-(id)initWithFrameFromField:(UITextField *)textField {
    CGRect theFrame;
    theFrame.origin.x = textField.frame.origin.x;
    theFrame.origin.y = (textField.frame.origin.y + textField.frame.size.height);

    theFrame.size.width = textField.frame.size.width;
    theFrame.size.height = 100;

    self.tableView = [self.tableView init];
    self.tableView.frame = theFrame;

    //Border
    CALayer *layer = self.tableView.layer;
    layer.borderWidth = 2;
    layer.borderColor = [[UIColor blackColor] CGColor];
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;

    self.tableView.userInteractionEnabled = YES;

    self.tableView.dataSource = self;
    return self;
}

Also, here is the cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSArray *contentForThisRow = [[self currentArray] objectAtIndex:[indexPath row]];
    NSString *uniqueIdentifier = @"CellIdentifier";
    SuggestionBoxCell *cell = nil;
    [cell.contentView setUserInteractionEnabled:NO];
    cell = (SuggestionBoxCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
    if(cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"SuggestionBoxCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (SuggestionBoxCell *)currentObject;
                break;
            }
        }

    }
    if (indexPath.row == [suggestions getNumberOfEntries]) {
        cell.textBox.text = @"Search History";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        //Display entry
    }

    return cell;
}

All of the code that displays the cell content works and there seems to be nothing wrong with the datasource functionality-wise.

The thing that blows my mind is that the app store version (the one currently posted that was compiled in Xcode 4) works fine on iOS7 and I am able to click the table cell entries, however if I take the exact same code (no modifications at all) and compile it in Xcode 5 and run it on iOS7 (simulator and device) I am not able to click the table entries. To complicate matters, I am able to do run the Xcode 5 version on iOS 6 with no problems whatsoever.

This is where I am stuck, I have converted my tableviewcell nib files back to Xcode 4.3 format and back again and made sure all "User Interaction Enabled" booleans are as they should be to no avail.

Here is a copy of my didSelectRowAtIndexPath for the specific tableview and for reference, this is called in the encapsulating UIViewController which is assigned as the delegate.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == ([tableView numberOfRowsInSection:0]-1)) {
        //If the user clicks the last row in the table, go to search history
        searchHistory = [[SearchHistoryViewController alloc]initWithNibName:@"SearchHistoryView" bundle:[NSBundle mainBundle]];
        [MSUtilities presentViewController:searchHistory withParent:self];
    } else {
        //Change text in text field
    }
}

This is where I'm stuck. I have worked many hours on this and looked at many stackoverflow posts. Any suggestions/solutions/ideas?

EDIT: Added clarifications and simplified code.

EDIT2: Added cellForRowAtIndexPath by request.

There are a lot of things that you're doing wrong here. The most obvious is that you are not calling super within your init function. Your init function should look like this:

- (id)initWithFrameFromField:(UITextField *)textField
{ 
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        // Do custom init here
        _deferredFrame = textField.frame;
    } 
    return self;
}

The next problem is that you should not create your views until you -loadView or -viewDidLoad . You would do best in this case to do most of your init code in -viewDidLoad , like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.frame = self.deferredFrame;

    CALayer *layer = self.tableView.layer;
    layer.borderWidth = 2;
    layer.borderColor = [[UIColor blackColor] CGColor];
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;
}

The last problem is that in your example, you are setting your view controller as the UITableView's dataSource, but not delegate. Calling super properly in your init and letting your -loadView method create your UITableView for you solves that problem for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM