【问题标题】:UITableView reloadData not workUITableView reloadData 不起作用
【发布时间】:2015-02-16 17:00:44
【问题描述】:

这是我的 UITableView 代码

在 viewDidLoad 中

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

表格视图

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

当我添加数据并调用 reload 时

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

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

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

【问题讨论】:

  • 你在使用故事板吗?
  • 能否将整个方法添加到您更新数据并调用重新加载的问题中?这个方法是在 audioHistoryTableView 的视图控制器中还是在另一个视图控制器中?
  • 在您致电reloadData 后,您的日志消息会说什么?他们在打印吗?他们打印的东西是否正确?
  • @lightice11 是的,使用故事板
  • @LyndseyScott 它在另一个视图控制器中。我只是添加 UITableView,而不是 UITableViewController

标签: ios uitableview cell reloaddata


【解决方案1】:

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

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

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

【讨论】:

  • 很高兴我能帮上忙。是的,只要尝试保持一致,如果您在界面构建器中构建,然后在界面构建器中设置数据源和委托。 :)
  • @PhatsaweePuttarukkul 是的,您重新初始化了在情节提要中创建的表这一事实是问题所在。至于设置数据源和委托,您实际上可以像您所做的那样以编程方式或在界面构建器中执行此操作......应该没关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 2010-12-13
  • 1970-01-01
相关资源
最近更新 更多