0

我有下面这样的表格视图

在此处输入图像描述

从不同的可变数组显示名称、出生率和天数(如 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,等等名称和出生率在数据库中我将它们保存在数组中并对其进行处理以在另一个数组中查找剩余天数

请为此提出一些建议

4

3 回答 3

1

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

于 2013-03-20T06:38:48.820 回答
1
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
 [_daysremaining sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
于 2013-03-20T06:30:46.243 回答
1

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

NSMutableDictionary *dic = [array objectAtIndex:indexPath.row];
NSString *s = [dic valueForKey@"key"];
于 2013-03-20T06:26:58.070 回答