reloadData doesn't work

北慕城南 提交于 2020-01-14 14:12:23

问题


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];
}

回答1:


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.



来源:https://stackoverflow.com/questions/10197354/reloaddata-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!