简体   繁体   中英

reloadData doesn't work

i'm trying to update my UITableview but when i call [tableView reloadData]; the table doens't update, after i scroll the tableview above the name of the cell while be changed. But it's not adding a new row or something like that.

-(void)update {
        [tableView reloadData];

    NSLog(@" %i", [tableData count]);
}

The nslog it's returing 1 when i add a row it's returning 2 but the table is not updating.

- (void) refresh:(id)sender {  


    NSString *jsonString = [NSString 
                            stringWithContentsOfURL:[NSURL URLWithString:xmlDataUrl] 
                            encoding:NSUTF8StringEncoding
                            error:nil];


    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    parser = nil;

    [self setTableData:[results objectForKey:@"items"]];
        [self performSelector:@selector(update) withObject:nil afterDelay:.2];


}

.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

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


    NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[item objectForKey:@"title"]];
    [[cell detailTextLabel] setText:[item objectForKey:@"descript"]];
    [[cell detailTextLabel] setText:[item objectForKey:@"detail"]];

.

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


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

        return [tableData count];
}

Every-time I have seen this problem it is a result of the UITableView outlet not being connected. As a result the reloadData call is sent to nil . This is almost certainly what is wrong in this case. This can be easily tested by a simple log statement.

-(void)update {
    NSLog(@"tableView is '%@'",tableView);
    [tableView reloadData];
    NSLog(@" %i", [tableData count]);
}

More likely than not you will see tableView is (null) in the console.

Side note : The fact that the tableview populates at all is a sign that the dataSource and delegate outlets are connected.

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