开发者

Adding Insert Row in tableView

开发者 https://www.devze.com 2022-12-29 20:16 出处:网络
I have a tableView that loads its data directly from a Core Data table with a NSFetchedResultsController. I\'m not using an intermediate NSMutableArray to store the objects from the fetch results;

I have a tableView that loads its data directly from a Core Data table with a NSFetchedResultsController. I'm not using an intermediate NSMutableArray to store the objects from the fetch results;

I basically implemented inside my UITableViewController the protocol method numberOfRowsInSection and it returns the numberOfObjects inside a NSFetchedResultsSectionInfo.

id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];

and then I configure the cell content by implementing configureCell:atIndexPath and retrieving object info from the fetchedResultController but right now I have a generic configuration for any object (to avoid complications)

cell.textLabel.text = @"categoria";

I also have a NavigationBar with a custom edit button at the right that loads my own selector called customSetEditing. What I'm trying to accomplish is to load an "Insert Cell" at the beginning of the tableView so when I tap it, it creates a new record. This last part is easy to implement the problem is that I dont's seem to be able to load the insert row or any row when I tap on the navigation bar edit button.

this is the code for my customSetEditing:

- (void) customSetEditing {
            [super setEditing:YES animated:YES];
            [self.tableView setEditing:YES animated:YES];
            [[self tableView] beginUpdates];
            //[[self tableView] beginUpdates];
            UIBarButtonItem *customDoneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(customDone)];
            [self.navigationItem.rightBarButtonItem release];
            self.navigationItem.rightBarButtonItem = customDoneButtonItem;
            //[categoriasArray insertObject:[NSNull null] atIndex:0];
            NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:0],nil ];
            [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
            //[indexPaths release];
            [self.tableView reloadData];}

Before adding the:[self.tableView reloadData]; I was getting an out of bounds error plus a program crash and although the program is not crashing it is not loading anything.

I have seen many examples of similar situations in stackoverflow (by the way is an excellent forum with very helpful and nice people) none of the examples seems to work for 开发者_Go百科me.

Any ideas?


You know I just found out that the application was crashing at commented line in this method:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    //NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = @"categoria";
}

It works if I comment the line. It might be because there is no data at all in the fetched results controller at the beginning, the thing is I need that line so I tried with:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
            if (![fetchedResultsController objectAtIndexPath:indexPath]) {
                cell.textLabel.text = @"categoria";
            }
            else{
                NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
                cell.textLabel.text = @"categoria";
            }

}

But still crashes.


Your -configureCell:atIndexPath: should not be called if your table is empty. What does your -tableView:rowsInSection: method look like?


this is the code (sorry about the mess I don't seem to figured out how to fix it for stackoverflow ):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; if (self.tableView.editing) { return ([sectionInfo numberOfObjects]+1); } return [sectionInfo numberOfObjects]; }

0

精彩评论

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

关注公众号