【问题标题】:update UITableView based on array基于数组更新 UITableView
【发布时间】:2014-02-26 00:40:46
【问题描述】:

我有 JSON 文件,我把谁的信息放在我的表中。我有一个用于排序数组的按钮 我可以对数组进行排序并使用NSLog 打印它。如何根据排序后的数组更新表格?

这是我的代码:

-(void)sortArry:(UIButton *)sender
{

    NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = @[ageDescriptor];
    NSArray *sortedArray = [tableData sortedArrayUsingDescriptors:sortDescriptors];

    //here i have my data in nslog
    NSLog(@"%@ sort test",sortedArray);
} 

如何在表格中显示我的 sortedArray?

我也用过

 [self.tableView reloadData];

排序后没有显示排序后的表格

更新:

这是我的 cellForRowAtIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  
  *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    for (id currentObject in objects)
    {
        if([currentObject isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell *) currentObject;

            break;
        }
    }
 }


id keyValuePair;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
    keyValuePair = [self.filteredTableData objectAtIndex:indexPath.row];


 }
else
{
    keyValuePair = [self.tableData objectAtIndex:indexPath.row];

 }

cell.name.text = keyValuePair[@"name"];


return cell;
}

【问题讨论】:

    标签: objective-c json uitableview nsarray nssortdescriptor


    【解决方案1】:

    sortedArray 在您的 sortArry: 方法结束时超出范围时将被删除。您需要设置 UITableViewDelegate 方法检查的属性或实例变量。

    【讨论】:

    • 当然,如果你发布你的 cellForRowAtIndexPath: 方法。
    • 非常感谢,我用 cellForRowAtIndexPath: 方法更新了我的问题
    • 在您致电 reloadData 之前添加 self.tableData = sortedArray;
    • 但是 tableData 是 NSMultipleArray 但 sortedArray 是 NSArray 我可以这样做吗?
    • 使用self.tableData = [sortedArray mutableCopy];
    【解决方案2】:

    数组排序后调用[tableView reloadData];

    【讨论】:

      【解决方案3】:

      sortedArray 必须是您的 viewController 的属性,而不是 sortArry 的实例变量,因此当您重新加载所有 UITableViewdelegate 方法时,可以访问其值并进行更新。


      // The array You use to populate the table
      @property NSArray ArrayDataSource;
      

      更改方法签名以返回排序后的数组

      -(NSArray *)sortArry
      {
      
          NSSortDescriptor *ageDescriptor = 
                       [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
          NSArray *sortDescriptors = @[ageDescriptor];
          NSArray *sortedArray = 
                        [tableData sortedArrayUsingDescriptors:sortDescriptors];
      
          //here i have my data in nslog
          NSLog(@"%@ sort test",sortedArray);
      
         return sortedArray;
      }
      

      将返回值存储在数据源中:

      ArrayDataSource = [self.ArrayDataSource];
      

      终于重新加载表格

      [self.tableView reloadData];
      

      【讨论】:

      • 感谢您的回答我希望在单击按钮时将所有这些员工都放在按钮中,然后对表格进行排序
      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      相关资源
      最近更新 更多