0

I am populating a table view with information, but would like it to be populated in reverse, so that any newly added cells (new email messages) would appear on the top, not the bottom.

What am I doing wrong?

    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"MailCell";

    MailCell *cell = (MailCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MailCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        // SLICK
        // Anything that should be the same on EACH cell should be here.

        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1];
        cell.selectedBackgroundView = myBackView;

        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.messageText.textAlignment = NSTextAlignmentLeft;
        cell.messageText.lineBreakMode = NSLineBreakByTruncatingTail;
    }

    NSUInteger row = [indexPath row];

    // Extract Data

    // Use the message object instead of the multiple arrays.

    CTCoreMessage *message = [[self allMessages] objectAtIndex:row];

    // Sender

    CTCoreAddress *sender = [message sender];
    NSString *senderName = [sender name];

    // Subject

    NSString *subject = [message subject];
    if ([subject length] == 0)
    {
        subject = @"(No Subject)";
    }

    // Body

    BOOL isPlain = YES;
    NSString *body = [message bodyPreferringPlainText:&isPlain];
    body = [body stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
    body = [body stringByReplacingOccurrencesOfString:@"\r" withString:@" "];

    // Populate Cell

    [[cell nameText] setText:senderName];
    [[cell subjectField] setText:subject];
    [[cell messageText] setText:body];

    if ([message isUnread])
    {
        UIColor *myColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1.0];
        cell.nameText.textColor = myColor;
    }
    else
    {
        cell.nameText.textColor = [UIColor blackColor];
    }

    return cell;

}

How I am loading the array:

- (NSMutableArray *)allMessages
{
    if (_allMessages == nil)
    {
        _allMessages = [[NSMutableArray alloc] init];
    }
    return _allMessages;
}
4

1 回答 1

1

您正在从 NSArray 索引中拉取,[indexPath row]这意味着您从索引 0 开始并到 n。这意味着您的顺序不是相反的。你需要先反转你的数组。一个简单的方法是:

- (void)viewWillAppear:(BOOL)animated
{
    NSArray *allMessages = [self allMessages];
    NSArray* reversedMessages = [[allMessages reverseObjectEnumerator] allObjects];
}

然后在您的cellForRowAtIndexPath方法中,您可以执行以下操作:

CTCoreMessage *message = [reversedMessages objectAtIndex:row];
于 2013-04-23T22:02:04.897 回答