2010-08-23 44 views
0

我有一個UITableView,我用下面的代碼擴展/縮小單元格。 我保存最後2個indexPaths來執行reloadRowsAtIndexPaths:將導致「程序接收到的信號:」SIGKILL「。」

現在我添加了UISearchBar到部分0的標題。如果我選​​中searchBar,KeyBoard顯示在UITableView的頂部 - 目前爲止非常好。

但我希望用戶能夠觸摸單元並禁用鍵盤。要做到這一點,我測試,如果搜索框是-tableView:didSelectRowAtIndexPath:

中的第一個響應者,但這樣做會導致行標1,2之一的「SIGKILL」,3

我真的不明白爲什麼

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

回答

0
 
    firstSelected = lastSelected; 
    lastSelected = indexPath; 

這使我相信lastSelected是一個實例變量?如果是這種情況,你沒有妥善保留它,並且不能保證它仍然存在超出此方法的範圍。在didSelectRowAtIndexPath:參數中傳遞的indexPath如果要在執行後使用它,則需要保留。請記住,如果你這樣做,你需要在整個方法中更好的內存管理......也就是說,在更改它的值之前釋放lastSelected或將其設置爲零。

假設firstSelected和lastSelected是實例變量,你可以這樣做。 (所有的釋放和!=檢查將消失,如果你讓他們保留屬性並使用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

感謝您的代碼,但問題仍然存在。我假設(lastSelected!= lastSelected)應該是(lastSelected!= indexPath) – vikingosegundo 2010-08-23 23:49:38

相關問題