0

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

user2189388
  • 202
  • 1
  • 4
  • 10
  • Have a look at my answer @user2189388 – Manu Mar 20 '13 at 06:31
  • 1
    1) You need to sort the arrays based on the remaining days 2) Create a custom class and When you are calculating the remaining days create an object of custom class and assign these values to that object and save that on an array. Sort that array using predicate and use it instead of using these three arrays – Midhun MP Mar 20 '13 at 06:34

3 Answers3

1

I would be better to use a sigle array of dictionary to store the data rather than storing it in different array. You can use dictionary to store name,days remaining etc as key value pair. And access the dictionary as

NSMutableDictionary *dic = [array objectAtIndex:indexPath.row];
NSString *s = [dic valueForKey@"key"];
Ab'initio
  • 5,368
  • 4
  • 28
  • 40
1
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
 [_daysremaining sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Manu
  • 4,730
  • 2
  • 20
  • 45
  • will order whole cell? or it will order only _dayremaining? – user2189388 Mar 20 '13 at 06:33
  • only _daysremaining. You want everything ?? – Manu Mar 20 '13 at 06:33
  • @Manohar: This won't solve the issue. It'll only sort and display the days remaining. The other details will be in the same order as it is, sorting won't affect them. – Midhun MP Mar 20 '13 at 06:36
  • yes i want cell get rearranged in that way cus days remaning are for that particular name and birthdate so we need to order cell not only days remaning – user2189388 Mar 20 '13 at 06:36
  • @user2189388: Okay. Take all your existing arrays with key in a dictionary and take a new array and add that dictionary.Then, when sorting sort with the key, it will be easy – Manu Mar 20 '13 at 06:38
1

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.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186