質問

IMは、一般者によってすべてのオブジェクトを見つけることができるようにUISearchバーを使用しようとしています。PFUserまたはユーザーがpointsのメインオブジェクト内にネームオブジェクトを持つように設定しました。ユーザーを見つけようと試みると、オブジェクトは何も表示されませんが、NSLOGSでは、すべての情報が表示されます。私の問題はcellForRowAtIndexPathにあると思いますが、理由はわかりません!

最初の照会:

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"points"];


    // The key of the PFObject to display in the label of the default cell style
    //self.textKey = @"opponent";

    // Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
    // self.imageKey = @"image";

    // Whether the built-in pull-to-refresh is enabled
    self.pullToRefreshEnabled = YES;

    // Whether the built-in pagination is enabled
    self.paginationEnabled = NO;

    // The number of objects to show per page
    self.objectsPerPage = 50;
  //where you set the username to the specific user to get only this user's post.
    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.

    if (self.objects.count == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByDescending:@"createdAt"];

    return query;
}
.

私は私のバーを置きます:

-(void)viewDidAppear:(BOOL)animated{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

self.tableView.tableHeaderView = self.searchBar;

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;


//CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
//self.tableView.contentOffset = offset;

self.searchResults = [NSMutableArray array];
}
.

結果のフィルタリング:

    -(void)filterResults:(NSString *)searchTerm {

        [self.searchResults removeAllObjects];

        PFQuery *query = [PFQuery queryWithClassName: @"points"];
        [query whereKeyExists:@"name"];  //this is based on whatever query you are trying to accomplish
        [query whereKeyExists:@"opponent"]; //this is based on whatever query you are trying to accomplish
        [query whereKey:@"name" containsString:searchTerm];

        NSArray *results  = [query findObjects];

        NSLog(@"%@", results);
        NSLog(@"%u", results.count);

        [self.searchResults addObjectsFromArray:results];
    }

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}
Counting The Objects:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {
        //if (tableView == self.searchDisplayController.searchResultsTableView) {

        return self.objects.count;

    } else {

        return self.searchResults.count;

    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
.

それをすべて表示しようとしています!:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    // If you want a custom cell, create a new subclass of PFTableViewCell, set the cell identifier in IB, then change this string to match
    // You can access any IBOutlets declared in the .h file from here and set the values accordingly
    // "Cell" is the default cell identifier in a new Master-Detail Project
    static NSString *CellIdentifier = @"celltag";

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

    if (tableView != self.searchDisplayController.searchResultsTableView) {
        UILabel *game = (UILabel*) [cell viewWithTag:200];
        game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];

        UILabel *points = (UILabel*) [cell viewWithTag:201];
        points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];

        UILabel *date = (UILabel*) [cell viewWithTag:250];
        date.text = [object objectForKey:@"date"];

        UILabel *name = (UILabel*) [cell viewWithTag:203];
        name.text = [object objectForKey:@"name"];
    }
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {


        UILabel *game = (UILabel*) [cell viewWithTag:200];
        game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];

        UILabel *points = (UILabel*) [cell viewWithTag:201];
        points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];

        UILabel *date = (UILabel*) [cell viewWithTag:250];
        date.text = [object objectForKey:@"date"];

        UILabel *name = (UILabel*) [cell viewWithTag:203];
        name.text = [object objectForKey:@"name"];



    }

    UILabel *game = (UILabel*) [cell viewWithTag:200];
    game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];

    UILabel *points = (UILabel*) [cell viewWithTag:201];
    points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];

    UILabel *date = (UILabel*) [cell viewWithTag:250];
    date.text = [object objectForKey:@"date"];

    UILabel *name = (UILabel*) [cell viewWithTag:203];
    name.text = [object objectForKey:@"name"];


    return cell;
}
.

役に立ちましたか?

解決

cellForRowAtIndexPathメソッドには多くのエラーがあります。

(1)奇妙な条件付きを含んでいます。

a。最初にあなたはPONTERSを!=演算子と比較しています:

if (tableView != self.searchDisplayController.searchResultsTableView)
.

しかし、あなたはisEqual

を使用してオブジェクトを比較しています
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
.

どちらの方法も十分であり、この条件が技術的に機能するはずですが、一貫性があるほうがいいです。また、cellForRowAtIndexPathのTableViewは、==または!=演算子を使用して、ポインタへの参照を保持しているので、演算子は十分ですが、速いです。

b。そのように2つのIFSを使用することは、2つのオプションの両方のためにifステートメントを持っているため、フォームが悪いです。代わりに、if-elseステートメントを使用することをお勧めします。

if (tableView != self.searchDisplayController.searchResultsTableView) {

   ...

} else {

    ...
}

return cell;
.

c。あなたの条件付きは、それが立っているので完全に不要です。条件付きブロックの両方に同じコードがあり、もう一度条件付きブロックの後も再び!そのため、略語として、IFステートメントの外側の最後のコードから両方のIFステートメントを削除できます。

(2)しかし、あなたの現在のものが冗長であっても条件付きを必要とする do を必要としています。

指定したコードの使用中にテーブルが変更されない理由に関して最も大きな問題:

PARSEのcellForRowAtIndexPathメソッドを使用しているため、行データは- (PFQuery *)queryForTableからの情報を入力しているため、コードが現在書き込まれているため、オブジェクトにこのクエリの結果が入力されます。

PFQuery *query = [PFQuery queryWithClassName:@"points"];
.

検索していないときは、このクエリから返されたparse pfobjectパラメータを使用できますが、Self.SearchResults配列の現在のインデックス、つまり[self.searchResults objectAtIndex:indexPath.row]のオブジェクトを使用することができます。 >

if (tableView != self.searchDisplayController.searchResultsTableView) {

    UILabel *game = (UILabel*) [cell viewWithTag:200];
    game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];

    UILabel *points = (UILabel*) [cell viewWithTag:201];
    points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"], [object objectForKey:@"ppg"]];

    UILabel *date = (UILabel*) [cell viewWithTag:250];
    date.text = [object objectForKey:@"date"];

    UILabel *name = (UILabel*) [cell viewWithTag:203];
    name.text = [object objectForKey:@"name"];

} else {

    PFObject *searchResult = [self.searchResults objectAtIndex:indexPath.row];

    UILabel *game = (UILabel*) [cell viewWithTag:200];
    game.text = [NSString stringWithFormat:@"V.s %@", [searchResult objectForKey:@"opponent"]];

    UILabel *points = (UILabel*) [cell viewWithTag:201];
    points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[searchResult objectForKey:@"goals"], [searchResult objectForKey:@"assists"], [searchResult objectForKey:@"totalpoints"], [searchResult objectForKey:@"plusminus"], [searchResult objectForKey:@"hits"], [return cell; objectForKey:@"ppg"]];

    UILabel *date = (UILabel*) [cell viewWithTag:250];
    date.text = [searchResult objectForKey:@"date"];

    UILabel *name = (UILabel*) [cell viewWithTag:203];
    name.text = [searchResult objectForKey:@"name"];
}

return cell;
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top