【问题标题】:add a new row to the top of UITableView while using core data使用核心数据时在 UITableView 顶部添加新行
【发布时间】:2012-04-30 09:34:28
【问题描述】:

我有一个文本字段,我在其中输入一个值并将其保存并在 tableView 中检索它们。 最后保存的项目显示在表格视图的底部。 但我希望它显示在表格视图的顶部。

我尝试了以下代码但失败了。

- (void)viewDidLoad
{

   CoreDataOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
   NSManagedObjectContext *context = [appDelegate managedObjectContext];
   NSEntityDescription *entityDesc = 
   [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
   NSFetchRequest *request = [[NSFetchRequest alloc] init];
   [request setEntity:entityDesc];
    NSError *error;
    objects = [[context executeFetchRequest:request error:&error]retain];
    [request release];
    [super viewDidLoad];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    labelOne = [[UILabel alloc]initWithFrame:CGRectMake(5, 11, 110, 21)];
    labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(230, 11, 70, 21)];

    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier]autorelease];
    }

    [cell.contentView addSubview:labelTwo];
    [cell.contentView addSubview:labelOne];

    NSManagedObject *matches = nil;
    matches = [objects objectAtIndex:indexPath.row];

    labelOne.text = [matches valueForKey:@"name"];
    labelTwo.text = [matches valueForKey:@"amount"];

    NSArray *index = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
    [tableView insertRowsAtIndexPaths:index withRowAnimation:YES];

    return cell;
}

//保存

-(IBAction)save:(id)sender
{
CoreDataOneAppDelegate  *appDelegate = 
[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = 
[appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription
              insertNewObjectForEntityForName:@"Employee"
              inManagedObjectContext:context];
[newContact setValue:name.text forKey:@"name"];
[newContact setValue:amount.text forKey:@"amount"];
name.text = @"";
amount.text = @"";
//label
status.text = @"saved";
NSError *error;
[context save:&error];
}

【问题讨论】:

    标签: ios cocoa-touch uitableview core-data


    【解决方案1】:

    我假设你在某个地方做[objects addObject:newEntry]。将其更改为 [objects insertObject:newEntry atIndex:0]

    【讨论】:

    • 我对 xcode 非常陌生。所以请不要介意我在问你愚蠢的问题。我没有使用 [objects addObject:newEntry]。在我的代码中的任何地方。我也发布了剩余的代码。请检查并帮助!
    • 您发布的代码都是关于表格视图的。但是,每当用户在文本字段中输入内容时执行的代码呢?
    • 代码已更新!但我编写此代码是为了保存在不同的视图控制器中。
    • 好的,我知道它是 CoreData。我现在不太关心这个以及获取对象的顺序。我认为您可以通过在 NSFetchRequest 中设置 sortDescriptors 来更改结果的顺序。我不确定如何告诉请求首先获取最新的。也许按ID排序?对不起。
    猜你喜欢
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多