UITableView reloadData无法正常工作

时间:2015-12-22 12:20:41

标签: ios iphone uitableview cocoa-touch reloaddata

我是一个名为tableView的UITableView。它的数据数组称为namesArray

我有一个为数组添加名称的函数,如下所示:

-(void)addName:(NSString*)name
{
    [self.namesArray addObject: name];
    [self.tableView reloadData];
}

注意:我正在调用addNames:作为NSNotificationCenter的选择器。

我在reloadData上致电tableView后,最后一个单元格(已添加的单元格)未在tableView上显示,numberOfRowsInSection会返回实际数字,因此另一个单元格的空间,但没有实际的单元格。

我正在调试cellForRowAtIndexPath,我发现当cellForRowAtIndexPath调用新单元格时它看起来很好看,它似乎已经回归了正确的细胞,所以我真的不知道问题是什么。

cellForRowAtIndexPath的代码:

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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text=[self.namesArray objectAtIndex:indexPath.row];

    return cell;
}

numberOfRows:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.namesArray.count;
}

注意:如果我尝试使用namesArray打印NSLog的最后一个对象,它看起来很好(最后一个对象是创建的新对象),所以它是' sa重新加载tableView

的数据的问题 添加新名称后的

屏幕截图(所有名称+单元格的一个位置,但它是空白的(只有标题,你甚至不能选择它)): screenshot

你能帮帮我吗?非常感谢你!

1 个答案:

答案 0 :(得分:1)

试试这个:

dispatch_async(dispatch_get_main_queue(), ^{
           [self.tableView reloadData];
        }); 

它将在主线程上调用reloadData方法。

相关问题