2013-04-23 65 views
0

我使用信息填充表視圖,但希望將其填充爲反向,以便任何新添加的單元格(新電子郵件消息)將顯示在頂部而不是底部。反向數組內容?

我在做什麼錯?

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

} 

我是如何加載陣列:

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

回答

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]; 
+0

謝謝您的回答,您可以請張貼代碼到我現有的代碼,因爲我不完全知道該從陣列中拉出什麼。如果你願意,只要告訴我該放哪裏(或替換什麼),我會將其標記爲答案。謝謝! – 2013-04-23 22:05:22

+0

您需要在cellForRowAtIndexPath方法之外執行此操作,因此每次單元加載時都不會發生這種情況,可能在您的viewWillAppear中。你會想要用你的allMessages數組來做,檢查編輯。 – JeffN 2013-04-23 22:08:21

+0

這是我如何加載allMessages,檢查編輯。 – 2013-04-23 22:29:46