UISearchBar - 删除行

发布于 2024-12-25 04:48:40 字数 2198 浏览 3 评论 0原文

我使用 tableview 并使用 UISearchBar 来搜索它。当我必须删除一行时,我使用以下代码:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

//Get the object to delete from the array.

    Coffee *coffeeObj = nil;

    if (tv == self.searchDisplayController.searchResultsTableView)
    {
        coffeeObj = [appDelegate.coffeeArrayFiltered objectAtIndex:indexPath.row];
    }
    else
    {
        coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
    }


    appDelegate.countRowNumber -= 1;
    NSString *txtmessage2an = [NSString stringWithFormat:@"%@", coffeeObj.price];
    float priceFn = [txtmessage2an floatValue];
    NSString *txtmessage3a = [NSString stringWithFormat:@"%0.2f", priceFn];
    float stringDouble1 = [txtmessage3a floatValue];
    float stringDouble2 = appDelegate.countPriceNumber;
    float stringDoubleSum = stringDouble2 - stringDouble1;
    appDelegate.countPriceNumber = stringDoubleSum;
    [appDelegate removeCoffee:coffeeObj];

    //Delete the object from the table.

            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadData];

    if([appDelegate.searchTerm isEqualToString:@"!"]){
        NSLog(@"Not in search...");
    }
    else
    {
        NSLog(appDelegate.searchTerm);
        [self.searchDisplayController.searchBar setText:appDelegate.searchTerm];
    }
}
}

基本上它可以工作,但是当 [self.searchDisplayController.searchBar setText:appDelegate.searchTerm];被调用,然后我收到以下错误:

2012-01-05 15:36:05.769 iTaxFriend[9261:707] -[NSIndexPath getCharacters:range:]: 无法识别的选择器发送到实例 0x3ab490 2012-01-05 15:36:05.771 iTaxFriend[9261:707] *** 终止应用程序到期 未捕获异常“NSInvalidArgumentException”,原因: '-[NSIndexPath getCharacters:range:]: 发送到的无法识别的选择器 实例0x3ab490'

我调用 [self.searchDisplayController.searchBar setText:appDelegate.searchTerm]; 来刷新 UISearch 结果。当我注释这一行并手动执行此操作,在搜索字段中重新键入时,它正在工作而不会崩溃!

有什么想法如何刷新结果吗?

I use a tableview and I use a UISearchBar in order to search it. I use the following code when I have to delete a row:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

//Get the object to delete from the array.

    Coffee *coffeeObj = nil;

    if (tv == self.searchDisplayController.searchResultsTableView)
    {
        coffeeObj = [appDelegate.coffeeArrayFiltered objectAtIndex:indexPath.row];
    }
    else
    {
        coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
    }


    appDelegate.countRowNumber -= 1;
    NSString *txtmessage2an = [NSString stringWithFormat:@"%@", coffeeObj.price];
    float priceFn = [txtmessage2an floatValue];
    NSString *txtmessage3a = [NSString stringWithFormat:@"%0.2f", priceFn];
    float stringDouble1 = [txtmessage3a floatValue];
    float stringDouble2 = appDelegate.countPriceNumber;
    float stringDoubleSum = stringDouble2 - stringDouble1;
    appDelegate.countPriceNumber = stringDoubleSum;
    [appDelegate removeCoffee:coffeeObj];

    //Delete the object from the table.

            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadData];

    if([appDelegate.searchTerm isEqualToString:@"!"]){
        NSLog(@"Not in search...");
    }
    else
    {
        NSLog(appDelegate.searchTerm);
        [self.searchDisplayController.searchBar setText:appDelegate.searchTerm];
    }
}
}

Basically it works, but when the [self.searchDisplayController.searchBar setText:appDelegate.searchTerm]; is called, then I get the following error:

2012-01-05 15:36:05.769 iTaxFriend[9261:707] -[NSIndexPath
getCharacters:range:]: unrecognized selector sent to instance 0x3ab490
2012-01-05 15:36:05.771 iTaxFriend[9261:707] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[NSIndexPath getCharacters:range:]: unrecognized selector sent to
instance 0x3ab490'

I call [self.searchDisplayController.searchBar setText:appDelegate.searchTerm]; in order to refresh the UISearch results. When I remark this line and do it by hand, retyping on the search field, it is working without crashing!

Any ideas how to refresh the results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文