Sage Washabaugh
Sage Washabaugh

Reputation: 297

Strange UITableView bug

I am having a strange bug occur when I set up a UITableView. What happens is when I first click on it nothing happens (it should be changing a UIView) but then when I click on the a cell either above or below it then it changes to the UIView I wanted to be displayed. I am creating it just like every other tableview I have created, here is the code:

- (void)viewDidLoad {

    [super viewDidLoad];
    TableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320.0,120.0)]; 
    // Table View is an iVar in the header file
    TableView.dataSource = self;
    TableView.delegate = self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath     {

    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 10;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* CellIdentifier = @"CELLIDENTIFIER";
    UITableViewCell* cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        [cell setBackgroundView:[[GMCellBackground alloc] init]];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

}

Upvotes: 2

Views: 423

Answers (1)

Darren
Darren

Reputation: 10129

I think I see your problem. You wrote in your comment that you change views in

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

Note that it is didDeselect not didSelect. What you want I think is to change views in

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

This is a common error caused I think by people accepting Xcode's autocomplete suggestion without noticing that it's not the right method.

The reason that it works the second time you click is that the first cell that was selected gets deselected, so your method gets called.

Upvotes: 3

Related Questions