title: iphone uitableview grouped by date implementation
body: <p>So currently I have a table that displays messages, but I'd like to group the messages by date, e.g.</p>
  
  <p>Tuesday 4/5/98  //header</p>
  
  <hr>
  
  <h2>message 1</h2>
  
  <h2>message 2</h2>
  
  <h2>message 3</h2>
  
  <p>Wednesday 4/6/98</p>
  
  <hr>
  
  <h2>message 1</h2>
  
  <p>etc.</p>
  
  <p>So right now, it's just one long NSMutableArray oldArr (sorted).</p>
  
  <p>What I was thinking of doing, was creating another NSMutableArray (groupArr) of unique date objects (DateGroup), in which each DateGroup would have the following ivars:</p>
  
  <pre><code>int size; //number of messages for date
  int index; //index of first message in the total array
  //so I can easily retrieve the object when the section and row is asked
  NSDate date; //need the date for the header
  </code></pre>
  
  <p>With these ivars, I can get all the sections with groupArr size, all the individual row sizes by accessing the DateGroup size, and the individual cell when given a section, row arguments by getting the index + row.</p>
  
  <p>I think this is the best way to do it. However, I am having problems populating the groupArr from the oldArr (which will dynamically increase in size). I was thinking of going one by one through the oldArr with this psuedocode:</p>
  
  <pre><code>NSDate date = nil;
  int size = 1;
  int index = 0;
  for (int i = 0; i &lt; oldArr.size; i++) {
      OldGroup* cur = [oldArr objectAt:i];
      if (date is different from cur-&gt;date){  //i know, it's pseudocode 
          DateGroup* newGroup = [[DateGroup alloc] initWithDate:cur-&gt;date index:index]; 
          [groupArr add:NewGroup];
          date = cur-&gt;date;
          index += size;
          size = 1; &lt;br/&gt;
      } else{ //the date is the same, so the object belongs in the group 
        [groupArr lastObject].size++;
      }
  }
  </code></pre>
  
  <p>Anyway, while I think this will work, it seems very unelegant to me. I was thinking about using the "indexOfObjectPassingTest" of NSMutableArry to find the next date, but can't seem to implement it conceptually. I'm trying to design a good way to do this. Any suggestions?</p>