单元格已创建但未在UITableView中显示

时间:2012-11-20 15:48:39

标签: objective-c xcode uitableview uisearchbar

这是我的第一个问题。

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

启动App后,它会从服务器下载数据并使用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.
        }
    }

0 个答案:

没有答案
相关问题