Question

I am going to quickly cut to the chase here, I am using the MailCore API to connect to email and download messages into an array, then take those messages in the array and display them into a Table View.

Here is the code (retrieved from official MailCore website) that will download ALL the messages off of the server:

messageSet = [inbox messagesFromUID:1 to:0 withFetchAttributes:CTFetchAttrEnvelope];

Basically, this will download all the messages from the server and I can confirm this by reporting the contents of the Array with an NSLog, all the messages are there.

Here is now the code that will take ONLY THE FIRST MESSAGE (index 0) and add that to my messages array:

    CTCoreMessage *msg = [messageSet objectAtIndex:0];
    BOOL isHTML;
    isHTML = YES;

    messages = [[NSMutableArray alloc] init];
    sendernames = [[NSMutableArray alloc] init];

    [messages addObject:[msg bodyPreferringPlainText:&isHTML]];
    NSString *sender = [NSString stringWithFormat:@"%@", [msg sender]];

    [sendernames addObject:sender];
    [tableView reloadData];

So again, this takes the message at index 0, which is the first message and adds it to the messages array. I also have code in there that gets the senders name and adds that to the sender array, ignore that.

Here is the code in my cellForRowAtIndexPath method (just a clipping of the values I am setting for my elements on the cell):

cell.nameText.text = [sendernames objectAtIndex:0];
cell.messageText.text = [messages objectAtIndex:0];

As you can see, it displays the index 0 object, the first message, the same one we added earlier.

Lastly, here is the code that sets how many rows I have. It is based on the messagesSet array which downloads ALL of the messages.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [messageSet count];
}

My question is, how can I modify my code to do two things:

1.) Add ALL the DOWNLOADED objects to the array and not just the first one. 2.) Modify the cellForRowAtIndexPath code to display all the objects in the messages array.

Thanks so much in advance!

Was it helpful?

Solution

Well firstly you iterate through the array and fill your array as so

for (CTCoreMessage *msg in messageSet) {
    [messages addObject:[msg bodyPreferringPlainText:&isHTML]];
    NSString *sender = [NSString stringWithFormat:@"%@", [msg sender]];
    [sendernames addObject:sender];
}

Then in the cell for row just do what you were doing, but instead of passing 0 for object at index pass: indexPath.row

Hope this helps.

OTHER TIPS

The model backing your table is really messageSet. You assign that with the result of the server call. Just use that in your delegate methods:

  1. After the download, call [self.tableView reloadData];
  2. numberOfRowsInSection: can return messageSet.count;
  3. cellForRowAtIndexPath: will be called sequentially for each visible indexPath. It doesn't need to deal with all messages, just the one at a time, via [messageSet objectAtIndex:indexPath.row];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top