2011-07-11 48 views
5

我有一个UITextField,我将其添加到UITableViewCell以用作一长串帐户的搜索字段。我有如下补充它:UITextField - 返回BOOL的代理时崩溃

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ((indexPath.section < accountsection) && (hasSearch) && (indexPath.row == 0)) 
    { 
     // Search 
     if (!searchField) 
     { 
      searchField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, cell.frame.size.width - 40, 25)]; 
      [searchField setEnabled:YES]; 
      searchField.placeholder = NSLocalizedString(@"Search", @"search"); 
      searchField.keyboardType = UIKeyboardTypeDefault; 
      searchField.returnKeyType = UIReturnKeySearch; 
      searchField.autocorrectionType = UITextAutocorrectionTypeNo; 
      searchField.clearButtonMode = UITextFieldViewModeAlways; 
      searchField.delegate = self; 
      [cell addSubview:searchField]; 
      [searchField release]; 
     } 

     // Clean up an account label if needed 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.textLabel.text = @""; 
     cell.detailTextLabel.text = @""; 

     // Show the search field if it was hidden by a text label 
     searchField.hidden = NO; 
     [cell bringSubviewToFront:searchField]; 
    } 
} 

为了检测编辑文本字段,我已经设置了页眉和陷阱以下委托调用UITextFieldDelegate

@interface AccountViewController : UITableViewController <UITextFieldDelegate> { 
    BOOL hasSearch;  
    UITextField *searchField; 
... 
} 

在实现中,我然后处理这些委托方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    NSLog(@"Done editing"); 
    [self filterAccountsBy:textField.text]; 
    [textField resignFirstResponder]; 
    return NO; 
} 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSLog(@"Searching for %@", string); 
    [self filterAccountsBy:string];  
    return YES; 
} 

但是在第二个中,除非我返回YES,否则文本不会更改;在第一个中,返回YES似乎不影响我。但是当我在任何一个返回YES时,我都会得到一个讨厌的EXC_BAD_ACCESS

我必须在我的手册中添加这个UITextField到我的手机丢失的东西,但我无法弄清楚它是什么...任何人都可以协助?

非常感谢。


编辑:如下建议,我注释掉filterAccounts打电话和我的应用程序现在已经不再崩溃。这里是这种方法的完整代码:

- (void)filterAccountsBy:(NSString *)filterstring 
{ 
    [accounts removeAllObjects]; 
    if (([filterstring length] == 0) && (!isChooser) && (![vpmsConn isDomainLogon])) { 
     [accounts addObject:[[vpmsConn accounts] objectAtIndex:0]]; 
    } 

    if ([filterstring length] == 0) { 
     [accounts addObjectsFromArray:[cache accounts]]; 
    } else { 
     for (AccountItem *ac in [cache accounts]) 
     {   
      BOOL found = NO; 

      // Name search 
      if ([[ac.clientName uppercaseString] rangeOfString:[filterstring uppercaseString]].location != NSNotFound) { 
       found = YES; 
      } 

      //more similar searches 

      if (found) { 
       [accounts addObject:ac]; 
      } 
     } 
    } 

    [self.tableView reloadData]; 
} 

虽然我有点困惑。当我使用textFieldShouldReturn过滤此列表然后返回NO时,它会正确过滤并且不会崩溃。有些关于从这些方法中的任何一个返回YES都会导致经过滤后的崩溃。如果我根本没有过滤,返回YES是没有问题的。

让我知道我是否应该发布任何其他代码。

+0

什么是'filterAccountsBy:'?如果你评论它,它会崩溃吗? – albertamg

+0

该方法仅通过提供的文本过滤帐户;它基本上对NSMutableArray执行以下操作:删除所有对象,然后添加新对象,然后在使用该数组作为其数据源的tableview上调用reloadData。 – hocker

+0

然而,正如你猜测的那样,当方法启用时它确实崩溃,而当它被注释掉时并不会崩溃。这让我想知道现在这种方法做错了什么。我将发布上面的完整方法代码。 – hocker

回答

0

我已经解决了这个问题,但是我不能(由于NDA)在这里发布答案。那些对付费iOS开发者计划感兴趣的人应该去NDA开发者论坛并且搜索UITextField EXC_BAD_ACCESS,你会发现答案......

0

我认为你的UITextFieldsearchfield开始释放。如果你使用self.searchField来设置你的searchfield(假设它是一个保留的属性),那将解决它。

我不确定您的.h的其余部分是什么样的。你可以这样做:

@property (nonatomic, retain) UITextField *searchField; 

然后在.M

@synthesize searchField; 

这就是我说的做搜索领域的留置物。这样你的班级将保持搜索领域。

+0

我试过了,它没有效果。从之前的评论者的编辑,我的过滤器逻辑发生了一些事情。我将编辑原始问题以添加此代码... – hocker

+0

如果打开僵尸,您应该能够找到正在引用的对象。这应该可以帮助你缩小问题范围(http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4) –

+0

其实,我已经完全按照链接描述的方式启用僵尸 - Xcode 4只是在main()函数中无用地转储了我。我会在上面发布回溯... – hocker

0

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

哪里是

return cell; 

0

- (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPathreturn cell;

在事件

DONOT释放serachfield;

在的dealloc推出

加载@属性(非原子,保留)的UITextField * searchField;