简体   繁体   中英

UISearchBars. Copy Array always equals to Nil

 - (void)searchTableList {
        NSString *searchString = searchBar.text;
        NSString *str=[[stories valueForKeyPath:@"name"] componentsJoinedByString:@"@"];
        NSLog(@"desired string:%@",str);
        NSMutableArray *array = [[NSMutableArray alloc]init];
        array = [str componentsSeparatedByString:@"@"];

//Never attempt to use compare with an array of dictionaries have to extract strings first first

        for (NSString *tempStr in stories) {
            NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
            if (result == NSOrderedSame) {
                [filteredContentList addObject:tempStr];
            }
        }
    }

First of all, filteredContentList is never allocated in your code above, so it will always point to nil. Add smth like filteredContentList = [NSMutableArray array] in viewDidLoad . Secondary, you rely on isSearching boolean flag to detect wether your are dealing with search results table view or general table view of you controller. This is, IMHO, bad practice. You should rely on tableView parameter, which is being passed to every method of table views delegates (in your case -- your UITableViewController ). Set tags or compare tableView parameter to self.tableView . Last thing -- you do not need to call reloadData at viewDidLoad .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM