Search code examples
iostableviewcell

arranging cell in particular order


i have table view like this below

enter image description here

names , birthrates and days like 300, 264, is getting displayed from different Mutable array

i have used following code for it

     - (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;

}

Now my question is how can i reorder this cells in way like minimum days to go should be frist and then second bigger than it and so on example it should be like 25, 201, 256, and so on names and birthrates are in database i save them in array and make process on it to find remaning days in another array

plz suggest something for this


Solution

  • As @Ab'initio answer, add Name, DOB and days remaining count into a dictionary and then add that dictionary into NSMutableArray and display into UITableView, okay, to get the order you want, you need to sort out the dictionary base on the remaining day's key from your dictionary, sort it in ascending order to get the result in order you want, use NSSortDescriptor can be useful, and here's an example of how to sort array contains dictionaries.