1

だから、私がUITableView作っているアプリのエントリを保持する があります。は、 .xibファイルentriesViewControllerを含む独自のクラスです。新しいアイテムを追加するボタンがあります。

これは、次のコードで行います。

-(IBAction)newItem:(id)sender {
    LEItem *newItem = [[LEItemStore sharedStore] createItem];
    NSLog(@"New Item = %@", newItem);
    [TableView reloadData];
}

これで機能し、アイテムが追加されますが、リストの一番下に配置されます。このアプリは何日もログに記録するので、この順序でアイテムを並べたくありません。最新の項目をリストの一番上に配置する必要があります。どうすればいいですか?上部のテーブル ビューに項目を追加する簡単な方法は見当たりませんでしたが、かなり基本的なものが欠けている可能性があります。

これは難しいことではないようです。おそらく何かを見落としているだけです。

アイデアは大歓迎です。

編集:

LEItemストアはこちら:

//
//  LEItemStore.m
//
//  Created by Josiah Bruner on 10/16/12.
//  Copyright (c) 2012 Infinite Software Technologies. All rights reserved.
//

#import "LEItemStore.h"
#import "LEItem.h"

@implementation LEItemStore
+ (LEItemStore *)sharedStore
{
    static LEItemStore *sharedStore = nil;
    if (!sharedStore)
        sharedStore = [[super allocWithZone:nil] init];

    return sharedStore;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedStore];
}

-(id)init
{
    self = [super init];
    if (self) {
        NSString *path = [self itemArchivePath];
        allItems = [NSKeyedUnarchiver  unarchiveObjectWithFile:path];

        if (!allItems)
        {
            allItems = [[NSMutableArray alloc] init];
        }
    }

    return self;
}

- (NSArray * )allItems
{
    return allItems;
}
-(LEItem *)createItem
{
    LEItem *p = [LEItem addNewItem];

    [allItems addObject:p];

    return p;
}

- (void)removeItem:(LEItem *)p
{
    [allItems removeObjectIdenticalTo:p];
}

-(void)moveItemAtIndex:(int)from toIndex:(int)to
{
    if (from == to) {
        return;
    }
    LEItem *p = [allItems objectAtIndex:from];

    [allItems removeObjectAtIndex:from];

    [allItems insertObject:p atIndex:to];
}

- (NSString *)itemArchivePath {
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];

    return [documentDirectory stringByAppendingPathComponent:@"item.archive"];
}

-(BOOL)saveChanges {
    NSString *path = [self itemArchivePath];

    return [NSKeyedArchiver archiveRootObject:allItems toFile:path];
}
@end
4

5 に答える 5

3

最も簡単な解決策は、これに変更-[LEItemStore createItem]することです。

-(LEItem *)createItem {
    LEItem *p = [LEItem addNewItem];
    [allItems insertObject:p atIndex:0];    
    return p;
}
于 2012-12-26T23:28:09.137 に答える
1

何か不足していない限り、新しいアイテムを作成した後、使用する代わりに

[allItems addObject:p];

あなただけが必要です:

[allItems insertObject:p atIndex:0];
于 2012-12-26T23:29:28.457 に答える
1

内部で配列を再配置しなくても実行できます。データ ソースを実装し、このメソッドを定義すると、次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

配列内で最も古いオブジェクトが最も低いインデックスにあると仮定し、テーブル ビューに M 行があると仮定すると、インデックス M-rowIndex-1 のオブジェクトの形式でセルを返します。

于 2012-12-26T23:14:10.490 に答える
0

LEItem クラスの比較メカニズムをオーバーライドして、日付を簡単に比較できます。

-(NSComparisonResult)compare:(LEItem*)otherItem {
     return [self.dateCreated compare:otherItem.dateCreated];
}

sortArrayUsingSelector:あとは、 selectorを使用するだけcompare:です。

于 2012-12-26T23:28:33.240 に答える
0

アイテムに作成日またはその他の並べ替え可能なプロパティがありますか? 保持されている項目のリスト (または NSFetchedResultsController) またはそのプロパティによってバインドされているものを並べ替えるだけです。

于 2012-12-26T23:13:14.840 に答える