开发者

will lead to "Program received signal: “SIGKILL”."

开发者 https://www.devze.com 2023-01-12 14:55 出处:网络
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.

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:d开发者_Go百科idSelectRowAtIndexPath:

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];
    }
}


   firstSelected = lastSelected;
    lastSelected = indexPath;

This leads me to believe that lastSelected is an instance variable? If this is the case, you are not properly retaining it, and there is no guarantee that it is still alive beyond the scope of this method. The indexPath passed in the didSelectRowAtIndexPath: argument needs to be retained if you are going to use it after it's execution.

Keep in mind, if you do that, you need better memory management throughout that method...i.e. releasing lastSelected before changing it's value or setting it to nil.

Assuming firstSelected and lastSelected are instance variables, you could do something like this. (all the releasing and != checking would go away if you made them retained properties and used the setter)


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

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

    [orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];

    if (firstSelected != lastSelected) {
        [firstSelected release];
        firstSelected = [lastSelected retain];
    }

    if (lastSelected != lastSelected) {
        [lastSelected release];
        lastSelected = [indexPath retain];
    }

    if (lastSelected == firstSelected && firstSelected != nil) {

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:lastSelected] withRowAnimation:CELL_ANIMATION]; //1
        [lastSelected release];
        lastSelected = nil;
        [firstSelected release];
        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
    }
}

0

精彩评论

暂无评论...
验证码 换一张
取 消