UITableView 重新加载数据不起作用

UITableView reloadData not work

这是我的 UITableView 代码

在 viewDidLoad 中

self.audioHistoryTableView = [[UITableView alloc] init];
self.audioHistoryTableView.delegate = self;
self.audioHistoryTableView.dataSource = self;

table 查看

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");
    NSLog(@"tableeeeeeee %lu",(unsigned long)[audioHistory count]);
    return [audioHistory count];
}


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

    NSLog(@"cellForRowAtIndexPath");
    NSString *CellIdentifier = @"historyCell";
    UITableViewCell *cell = (UITableViewCell *)[self.audioHistoryTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *audio = [audioHistory objectAtIndex:indexPath.row];
    cell.textLabel.text = audio;

    return cell;
}

当我添加数据并调用重新加载时

NSArray *newItem = [NSArray arrayWithObject:@"new audio!"];
NSLog(@"new item : %@",newItem);

[audioHistory addObjectsFromArray:newItem];
[self.audioHistoryTableView reloadData];

我知道这是行不通的,因为我添加了新数据并更改到其他页面,当返回此页面时显示添加的数据。

您可以改为通过界面生成器设置委托和数据源吗?另外,如果它是在界面生成器中制作的,为什么您必须初始化 table?删除这个:

self.audioHistoryTableView = [[UITableView alloc] init];
self.audioHistoryTableView.delegate = self;
self.audioHistoryTableView.dataSource = self;

然后在界面生成器控件中将 UITableView 拖到 UIViewController 并将委托设置为 self。再次执行此操作并将数据源设置为 self。试试看