【问题标题】:arranging cell in particular order按特定顺序排列单元格
【发布时间】:2013-03-20 06:19:31
【问题描述】:

我在下面有这样的表格视图

姓名、出生率和天数(如 300、264)正在从不同的可变数组中显示

我使用了以下代码

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


   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

    UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 320, 50)];
    [selectionView setBackgroundColor: [UIColor clearColor]];

    UILabel *callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 150, 21)];
    callTypeLabel.text = (NSString *)[_combinedNameArray objectAtIndex:[indexPath row]];
    callTypeLabel.backgroundColor = [UIColor clearColor];
    callTypeLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [selectionView addSubview:callTypeLabel];

    UILabel *daystogo = [[UILabel alloc]initWithFrame:CGRectMake(200 , -2, 125, 30)];

    daystogo.text = @"days to go";
    daystogo.backgroundColor = [UIColor clearColor];
    daystogo.font = [UIFont boldSystemFontOfSize:14.0];
    [selectionView addSubview:daystogo];

   UILabel *days = [[UILabel alloc]initWithFrame:CGRectMake(174 , -2, 125, 30)];
   days.text = [NSString stringWithFormat:@"%@",[_daysremaining objectAtIndex:[indexPath row]]];
   days.backgroundColor = [UIColor clearColor];
   days.font = [UIFont boldSystemFontOfSize:14.0];
   [selectionView addSubview:days];

   UILabel *birthdate = [[UILabel alloc]initWithFrame:CGRectMake(5 , 22, 150, 21)];
   birthdate.text = [NSString stringWithFormat:@"%@",[_combinedBirthdates objectAtIndex:   [indexPath row]]];
   birthdate.backgroundColor = [UIColor clearColor];
   birthdate.font = [UIFont boldSystemFontOfSize:14.0];
   [selectionView addSubview:birthdate];

   [[cell viewWithTag:0] addSubview:selectionView];


return cell;

}

现在我的问题是我如何重新排序这些单元格,比如最短的天数应该是第一个,然后第二个比它大,依此类推 例子应该是这样的 25, 201, 256, 等等 姓名和出生率在数据库中,我将它们保存在数组中并对其进行处理以在另一个数组中查找剩余天数

请为此提出一些建议

【问题讨论】:

  • 看看我的回答@user2189388
  • 1) 您需要根据剩余天数对数组进行排序 2) 创建自定义类,并在计算剩余天数时创建自定义类的对象并将这些值分配给该对象并保存那在一个数组上。使用谓词对该数组进行排序并使用它而不是使用这三个数组

标签: ios tableview cell


【解决方案1】:

作为@Ab'initio 的回答,将姓名、出生日期和剩余天数添加到字典中,然后将该字典添加到NSMutableArray 并显示到UITableView,好吧,要得到你想要的顺序,你需要排序根据字典中剩余日期的键取出字典,按升序排序以获得所需的结果,使用NSSortDescriptor 可能很有用,here's 是如何排序数组的示例包含字典。

【讨论】:

    【解决方案2】:

    我最好使用单个字典数组来存储数据,而不是将其存储在不同的数组中。您可以使用字典将名称、剩余天数等存储为键值对。 并以

    的身份访问字典
    NSMutableDictionary *dic = [array objectAtIndex:indexPath.row];
    NSString *s = [dic valueForKey@"key"];
    

    【讨论】:

      【解决方案3】:
       NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
       [_daysremaining sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
      

      【讨论】:

      • 会订购整个细胞吗?还是只订购_dayremaining?
      • 仅剩 _days。什么都想要??
      • @Manohar:这不会解决问题。它只会排序并显示剩余的天数。其他细节将与它的顺序相同,排序不会影响它们。
      • 是的,我希望单元格以这种方式重新排列,因为剩余天数是针对该特定名称和出生日期的,因此我们需要订购单元格,而不仅仅是剩余天数
      • @user2189388:好的。将所有现有数组与字典中的键一起取出并取出一个新数组并添加该字典。然后,当使用键进行排序时,这将很容易
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2018-03-28
      • 2014-08-17
      相关资源
      最近更新 更多