1

这是我的第一个问题。

我的 UITableView 没有显示所有创建的单元格。有什么问题?

应用程序启动后,它会从服务器下载数据并使用 UITableView 显示。当我滚动 UITableView 时,我的应用程序会下载更多数据(如 Twitter 应用程序)。一切都很好。

但我也有一个 UISearchBar。还有一个问题。当 SearchBar 处于活动状态时,一切都以相同的方式工作(例如,它在我键入时下载数据的第一部分,在我滚动时下载另一个数据),但 UITableView 仅显示数据的第一部分。

但是 cellForRowAtIndexPath 中的 NSLog 告诉我,所有单元格都创建得很好。我不知道该怎么办。

对不起,我的英语不太好。

此外,numberOfRowsInSection 返回正确的值(例如 3),而 UITableView 中仅显示 1 个单元格。而且,当然,我的数组包含我需要显示的所有数据。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if(searching == 0){ //if we aren't in search
            NSLog(@"Total rows %d", ids.count);
            return [ids count];
        }
        if(searching == 1){ //if we are in search
            NSLog(@"Total rows %d", idsSearch.count);
            return [idsSearch count];
        }
    }

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }

        NSInteger row = [indexPath row];

        if(searching == 0){
            cell.textLabel.text = [names objectAtIndex:row];
            NSLog(@"Cell with %@ created", [names objectAtIndex:row]);
        }

        if(searching == 1){
            cell.textLabel.text = [namesSearch objectAtIndex:row];
            NSLog(@"Cell with %@ created", [namesSearch objectAtIndex:row]); //tells me that ALL cells were created. But they aren't in UITableView.
        }

        return cell;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        searchRequest = searchBar.text;
        if([searchRequest isEqualToString:@""]){
            searching = 0;
        }
        else{
            searching = 1;

            [idsSearch removeAllObjects];
            [namesSearch removeAllObjects];

            [self getArtistsStartFrom:0 withLimit:1]; //downloads new data (1 record in this example) with needed predicate (searchRequest) from server, then adds it to arrays and performs [self.tableView reloadData] at the end.
        }
    }
4

0 回答 0