Add Event方法是将单元格添加到顶部,而不是底部

问题描述

|| 我有以下方法将单元格添加到表视图。我希望添加的单元格在底部,但是现在它在顶部添加它。有什么建议么? addEvent方法
-(void)addEvent
{    
    Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@\"Routine\" inManagedobjectContext:managedobjectContext];

    routine.name=entered;

    NSError *error = nil;
    if (![managedobjectContext save:&error]) {
        // Handle the error.
    }
    NSLog(@\"%@\",error);

    [eventsArray insertObject:routine atIndex:0];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.routineTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [self.routineTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
     }
ViewDidLoad
- (void)viewDidLoad
{
    if (managedobjectContext == nil) 
    { 
        managedobjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedobjectContext]; 
    }

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Routine\" inManagedobjectContext:managedobjectContext];
    [request setEntity:entity];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedobjectContext executeFetchRequest:request error:&error] mutablecopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }
    [self setEventsArray:mutableFetchResults];
    [mutableFetchResults release];
    [request release];

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];

    UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@\"Edit\" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)];
    self.navigationItem.rightBarButtonItem = editButton;
    [editButton release];

    [super viewDidLoad];
}
    

解决方法

0
是第一个索引。通常,与索引有关的事情(世界上可能有一些例外)从0开始。而诸如
count
之类的事情则从1开始。 因此,如果您的数组中有1个对象,则该数组的计数为
1
,并且该对象的索引为
0
。 当您使用0时,对于ѭ6的行和节,您将告诉它将其放在表格视图的顶部。 因此,使您最后四行代码如下所示:
[eventsArray addObject:routine];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self.routineTableView reloadData];

NSInteger lastSection = [self.routineTableView numberOfSections] -1;

[self.routineTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.routineTableView numberOfRowsInSection:lastSection]-1 inSection:lastSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    ,可能使用[eventsArray addObject:]而不是insertObject:atIndex: 另外,您应该可以使用[self.routineTableView reloadData];而不是手动插入行-假设您已经以常规方式设置了表格视图控制器。     ,您问题的具体答案是,您的表具有一个数组作为数据源,并且您要在数组的开头添加一个新项。因此,在重新加载表时,新单元格位于顶部。 为了更深入地了解,我建议您至少了解以下主题: 常用数据类型用作表的数据源(主要是数组和字典) 表格视图的工作方式(例如,tableView.dataSource和tableView.delegate是什么) 当dataSource更改时重新加载表的方法(您可以做,但不一定总是想要的)     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...