开发者

Add Event Method Is Adding Cell To Top, Not Bottom

开发者 https://www.devze.com 2023-02-24 22:14 出处:网络
I have the following method which is adding a cell to a tableview.I want the added cell to be at the bottom, however right now it is adding it at the top.Any suggestions?

I have the following method which is adding a cell to a tableview. I want the added cell to be at the bottom, however right now it is adding it at the top. Any suggestions?

addEvent method:

-(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 is the first index. As a rule (there are likely to be a few exceptions somewhere in the world) things that are to do with indexes, start at 0. Whereas things like count start at 1.

So if you have an array with 1 object in it, the array's count will be 1, and the object will be at index 0.

When you are using 0, for you indexPath's row and section your telling it to put it at the top of the tableview.

So make you last 4 lines of code something like this:

[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];


Probably use [eventsArray addObject:] instead of insertObject:atIndex:

Also, you should be able to use [self.routineTableView reloadData]; rather than inserting rows manually -- assuming you've set up the table view controller in a normal way.


The specific answer to your question is that your table has an array as a datasource and you are adding a new item at the beginning of the array. Therefore, when the table is reloaded, the new cell is at the top.

For a deeper understanding, I recommend you understand the following topics at minimum:

  1. common data types used as data sources with tables (arrays and dictionaries mainly)

  2. how table views work (e.g. what is tableView.dataSource and tableView.delegate)

  3. methods of reloading the table when the dataSource changes (what you did is ok but not always what you'll want)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号