views:

73

answers:

1

I have a UITableView, where I extend/shrink the cells with the following code. I save the last 2 indexPaths to perform a reloadRowsAtIndexPaths: on it.

Now I added a UISearchBar to the header for section 0. If I tab the searchBar, a KeyBoard is displayed on top of the UITableView — so far so good.

But I want the user to be able to touch the Cells and disable the KeyBoard. To do so, I test if the searchbox is the first responder inside the -tableView:didSelectRowAtIndexPath:

But doing so will lead to a "SIGKILL" in one of the rows marks 1, 2, 3

I really don't understand why

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Article *article = [articles objectAtIndex:indexPath.row];
    ArticleCell *cell = (ArticleCell*)[self.tableView dequeueReusableCellWithIdentifier:@"articelcell"];

    if (cell == nil) 
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ExtendibleCell" owner:self options:nil] objectAtIndex:0];
    }

    //....  
    cell.articleName.text = [NSString stringWithFormat:@"%@",article.name ];
    return cell;
}



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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([searchBar isFirstResponder]) {
        [searchBar resignFirstResponder];
    }

    [orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];
    firstSelected = lastSelected;
    lastSelected = indexPath;
    if (lastSelected == firstSelected && firstSelected != nil) {

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:lastSelected] withRowAnimation:CELL_ANIMATION]; //1
        lastSelected = nil;
        firstSelected = nil;
        return;
    }

    if (lastSelected != nil) {
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:CELL_ANIMATION];//2
    }

    if (firstSelected != nil) {
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:firstSelected] withRowAnimation:CELL_ANIMATION];//3
    }
}



-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        if (searchBar == nil) {
            searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            [searchBar setShowsBookmarkButton:YES];
            [searchBar setKeyboardType:UIKeyboardTypeAlphabet];
            [searchBar setBarStyle:UIBarStyleBlack];
            [searchBar setShowsCancelButton:YES];
            [searchBar setDelegate:self];
        }
        return searchBar;
    }
    return nil;
}


-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath isEqual:lastSelected] && lastSelected!=firstSelected) {
        return [[(Article *)[articles objectAtIndex:indexPath.row] sizesAndPrices] count]*PACKAGESIZE_PRICE_BUTTON_HEIGHT +30;
    }
    return 40.0;
}

edit I cleaned up my code for -tableView:didSelectRowAtIndexPath:, but the problem stays the same

@property(nonatomic,retain) NSIndexPath *firstSelected;
@property(nonatomic,retain) NSIndexPath *lastSelected;

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];
    self.firstSelected = nil;
    self.firstSelected = self.lastSelected;
    self.lastSelected = nil;
    self.lastSelected = [indexPath retain];

    if (self.firstSelected == self.lastSelected) {
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.firstSelected] withRowAnimation:CELL_ANIMATION];
        [self.firstSelected release];
        [self.lastSelected release];
        self.firstSelected = nil ;
        self.lastSelected = nil ;
    } else {
        if (self.firstSelected != nil) {

            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.firstSelected] withRowAnimation:CELL_ANIMATION];

        }

        if (self.lastSelected != nil) {

            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.lastSelected] withRowAnimation:CELL_ANIMATION];


        }
    }


    if ([searchBar isFirstResponder]) {
        [searchBar resignFirstResponder];
    }
}
A: 
Jerry Jones
Thanks for the code, but the problem remains. I assume (lastSelected != lastSelected) should be (lastSelected != indexPath)
vikingosegundo