0

我有一个 UITableView,我在其中使用以下代码扩展/收缩单元格。我保存最后 2 个 indexPaths 以对其执行 a reloadRowsAtIndexPaths:

现在,我UISearchBar在第 0 部分的标题中添加了一个。如果我选​​择 searchBar,则在 UITableView 的顶部会显示一个键盘——到目前为止一切都很好。

但我希望用户能够触摸单元格并禁用键盘。为此,我测试搜索框是否是-tableView:didSelectRowAtIndexPath:

但这样做会导致在标记 1、2、3 的行之一中出现“SIGKILL”

我真的不明白为什么

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

编辑 我清理了我的代码-tableView:didSelectRowAtIndexPath:,但问题仍然存在

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

1 回答 1

0
   firstSelected = lastSelected;
    lastSelected = indexPath;

这让我相信 lastSelected 是一个实例变量?如果是这种情况,则说明您没有正确保留它,并且无法保证它在此方法的范围之外仍然存在。在 didSelectRowAtIndexPath: 参数中传递的 indexPath 如果要在执行后使用它,则需要保留它。

请记住,如果您这样做,则需要在整个方法中进行更好的内存管理......即在更改它的值或将其设置为 nil 之前释放 lastSelected。

假设 firstSelected 和 lastSelected 是实例变量,你可以这样做。(如果你让它们保留属性并使用设置器,所有的释放和 != 检查都会消失)


- (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
    }
}

于 2010-08-23T21:26:47.440 回答