【问题标题】:Reload UITableView data not working properly重新加载 UITableView 数据无法正常工作
【发布时间】:2012-06-27 14:34:55
【问题描述】:

我有一个从我的网络服务接收到的数组对象,用于填充 UITableView。我正在尝试实现一种从 web 服务获取新数组并重新定义填充表的数组然后重新加载 tableView 以使用该数组中的新对象的方法。 这是应该完成工作的方法:

WSCaller *caller = [[WSCaller alloc]init];

    arrayFromWS = [caller getArray];
    [self.table reloadData];

它不起作用。有什么想法吗?

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

NSString *CellIdentifier = @"productsCellIdentifier";

ProductsCell *cell = nil;

cell = (ProductsCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) 
{
    NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"ProductsCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects) 
    {
        if ([currentObject isKindOfClass:[ProductsCell class]]) 
        {
            cell = (ProductsCell *)currentObject;
            break;
        }
    }
}

if (productsMutableArray) 
{
    cell.backgroundColor = [UIColor whiteColor];
    cell.productName.text = [[self.productsMutableArray objectAtIndex:indexPath.row]objectForKey:@"name"];

}

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [productsMutableArray count];
}

【问题讨论】:

  • self.table 更改为self.tableView
  • 它不会用数组中的新对象更新tableView
  • 如果我们看不到您如何填充您的表格视图,我们怎么能说,为什么您的表格没有显示所需的信息?发布 tableViewDelegate 和 tableViewDataSource 方法。并详细说明问题所在(表根本没有重新加载或只显示空表等)
  • @gtm 它仍然不起作用。 =/
  • 是的,您可以在UITableViewController 中发布代码吗?正如@Morion 所说,我们现在没有足够的信息。

标签: ios uitableview reload


【解决方案1】:

您的数据源实现是错误的(或者至少 sn-ps 是错误的)。

首先,您没有保留 arrayFromWS:

WSCaller *caller = [[WSCaller alloc] init];
arrayFromWS = [caller getArray];
[self.table reloadData];

其次,在您的 tableView:numberOfRowsInSection: 和 tableView:cellForRowAtIndexPath: 方法中,您使用的是不同的数组 (productsMutableArray)。

要解决这个问题,我建议将上面的代码更改为(假设您的 productsMutableArray 是一个强/保留属性:

WSCaller *caller = [[WSCaller alloc] init];
self.productsMutableArray = [NSMutableArray arrayWithArray:[caller getArray]];
[self.table reloadData];

【讨论】:

  • 我认为,arrayFromWS 只是从 wev 服务接收到的数组的抽象名称。并且表格视图委托和数据源方法中的代码 sn-ps 是从真实代码中复制和粘贴的
  • 我用数组组成tableView 的方式很好。我用新对象更改数组的方式也很好。我所需要的只是一种重新加载tableView 的方法。我需要再次调用cellForRowAtIndexPath:,因为它用于填充tableView 的数组的值已经改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
相关资源
最近更新 更多