inserting UITableViewCell not working - objective-c

Could somebody explain what I'm doing wrong in attempting to insert a new UITableViewCell? I'm trying to insert a custom UITableViewCell, but it throws the following error: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0)
return 1;
else if (section == 1)
return numberOfRows;
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EditableCell";
EditableCell *editableCell = (EditableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (editableCell == nil) {
editableCell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_myTextField = [editableCell textFieldCell];
if (indexPath.section == 0){
[_myTextField setPlaceholder:#"Menu Name"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else if (indexPath.section == 1){
[_myTextField setPlaceholder:#"Category"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else {
[_myTextField setPlaceholder:#"Category"];
[_myTextField setReturnKeyType:UIReturnKeyDone];
}
_myTextField.keyboardType = UIKeyboardTypeDefault;
_myTextField.delegate = self;
return editableCell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.returnKeyType == UIReturnKeyNext) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
NSArray *array = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
numberOfRows = numberOfRows + 1;
[self.tableView reloadData];
}
}

You need to make sure that the number of rows returned in the method below now returns the correct number of rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
I am sure what is happening is that you are returning the same value here after your insert, whereas the runtime is expecting it to be one more than it was before you made the call to insert a row.
In a bit more detail, what is happening in MVC terms (Model/View/Controller) is this:
Your call to insertRowsAtIndexPaths is requesting that the Controller update the View with an additional row.
The Controller then does a sanity check to see if you have your sums right, so it calls:
tableView:numberOfRowsInSection
Now the controller knows what value this method returned last time (in your case, 1), and it also knows that you have requested to insert a row. There have been no delete calls, so it expects the next time it calls this method, the value should be (last time's value + rows inserted - rows deleted) = 2
If the Controller deems that you have things in order, it will then call cellForRowAtIndexPath (along with other methods) to get the actual cells for each of the rows in each section.
So for your problem, you need to keep track of the rows in your model - maybe in an array or an ivar with the count of rows available. Update your model by updaing this value as you add/delete rows before you make the call to insertRowsAtIndexPath, and then return this value when tableView:numberOfRowsInSection is called.
Additional tip:
If you wish to have your cell inserts/deletes to be animated, change your code to the below. Notice there is no call to reloadData anymore, instead the insert/reload is wrapped in begin/end update calls - The animated updates will occur after endUpdates due to the reloadSections call.
NSMutableIndexSet *sectionsToReload = [[NSMutableIndexSet alloc] init];
[sectionsToReload addIndex:1]; // Add sections as necessary
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
[self.tableView reloadSections:sectionsToReload withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Check out the WWDC 2010 video from Apple, "Mastering Table Views" - explains everything really nicely, regarding using insertRowsAtIndexPaths and associated methods.
HTH,
Stretch

After you attempt to insert a new cell, your datasource must contain the number of original cells (1) plus the "extra" cell you are inserting (1) for a total of 2. So your tableView:numberOfRowsInSection: must return 2.
The error message indicated that tableView:numberOfRowsInSection: returned 1.
Update your datasource so that it returns this "extra" cell.

Related

Get row from indexPath in RoboVM

I am trying to write the following objective-c code in robovm:
(NSIndexPath *)indexPath = ...;
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
However, when I look thru RoboVM's calls for NSIndexPath here:
https://github.com/robovm/robovm/blob/e60579613140f4a6d48f108042633fbf17b9c289/cocoatouch/src/main/java/org/robovm/apple/foundation/NSIndexPath.java
or by scrolling thru Eclipse's options, I don't see anything corresponding to row. What can I do?
I have no experience with RoboVM. However, indexPath.section and indexPath.row are just convenience methods/properties for
[indexPath indexAtPosition:0] // section
[indexPath indexAtPosition:1] // row
and the indexAtPosition: method is defined in the Java class.

Adding data to a tableView using a NSMutableArray

I'm having a problem adding an item to my tableView.
I used to initialize an empty tableView at the start of my App and get it filled with scanned items every time the tableView reappears and there is an item in my variable.
Initialization of the tableView:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
self.listArray = array;
TableView Data Source:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.listArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
return #"Eingescannte Artikel:";
else
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"testCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [listArray objectAtIndex:row];//[NSString stringWithFormat:#"Das ist Zeile %i", indexPath.row];
return cell;
}
(Not the whole thing but the ones I changed)
As you may have seen I use an NSMutableArray to add items to my tableView.
So if an item ist scanned I'm adding it to my array like this:
[listArray insertObject:sharedGS.strEAN atIndex:0]; //using a shared Instance where I implemented my variable.
I also tried to use an variable to extend my Index every time a new Item is added, but it won't work both ways.
I'm quite new to programming so an not-too-hard-to-understand-answer would be quite nice ;)
If there's any information missing, feel free to ask.
/edit: Trying to specify my question: The data from the variable is written in a TableViewCell, but if I scan another one the other one is just being replaced. Not sure if it's a problem with my array or my tableView...
/edit No.2: Found out(thanks to fzwo) that my array isn't working correctly. It just doesn't grow by an addObject: or insertObject:atIndex: command. But I just don't get why... :(
All I'm doing: [listArray addObject:sharedGS.strEAN]; not that much space for errors in one simple line. Maybe I'm just too stupid to recognize what I'm doing wrong:D
You state that your problem is "adding an item to my tableView" , since you are adding the object to your array i am guessing the problem is that you are not reloading the table or that it is missing the dataSource binding.
You have not actually asked any question (even if you added info to "specify your question") so a wild guess, after
[listArray insertObject:sharedGS.strEAN atIndex:0];
put
[yourTableView reloadData];
Are you intentionally adding new items to the top of the table ? otherwise you could do
[listArray addObject:sharedGS.strEAN]; to add new items to the bottom
Otherwise it's worth noting that you are misusing dequeueReusableCellWithIdentifier, look at the example below for proper usage:
// Try to retrieve from the table view a now-unused cell with the given identifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

Delete object from Array and UITableView

I have a NSMutableArray with a list of prizes.
With those, I filled a UITableView.
Now, I´m trying to delete one of those prizes using TableView CommitEditingStyle. And giving me the "Invalid update: invalid number of sections...." error.
I readed a lot of post over internet about the "Invalid update: invalid number of sections...." but I can´t make it work.
First I remove the object from the array and then update the TableView (like all the posts teach me). But still give me the error
I try setting the array to nill and is the same.
Is necesary to refresh the array or something like this?
This is my Code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self myPrizesList] removeObjectAtIndex: [indexPath row]];
[self.tableView beginUpdates];
// Either delete some rows within a section (leaving at least one) or the entire section.
if ([[self myPrizesList] count] > 0)
{
// Section is not yet empty, so delete only the current row.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// Section is now completely empty, so delete the entire section.
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
}
I always said that, for a 5 element array, "the number of sections contained in the table view after the update (5) must be equal to the number of esctions contained in the table view before the update (5), plus or minus the number of sections inserted or deleted (0 insert, 1 deleted)"
If I set the array to nil, say: "the number of sections contained in the table view after the update (1) must be equal to the number of esctions contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 insert, 1 deleted)"
Thanks and sorry for my poor english
It is the sections deletion that is the issue, you are deleting the final prize AND section, but when the table reloads it still has the original number of sections... 5. How are you determining the number of sections to return? This needs to reduce if you have deleted one.

NSFetchedResultsControllerDelegate and deleteRowsAtIndexPaths: Section issue

In response of a delete operation on a CELL (not a section) of a tableView connected to a NSFetchedResultsController i obtain this error :
'Invalid update: invalid number of
sections. The number of sections
contained in the table view after the
update (1) must be equal to the number
of sections contained in the table
view before the update (2), plus or
minus the number of sections inserted
or deleted (0 inserted, 0 deleted).'
I understand that the problem is related to Section numbers after and before update. It says that i didn't delete sections, but the after-value is different from the before-value.
Ok this's true! but my section depend on row cells, so if i remove the last row cell of a section, the section disappear.
Here how i define Section and Row numbers:
Sections are created to group row by an attribute "date". Thus, if a row has the attribute "date" 10 April 2010 and a second row has 11 April 2010 i have 2 sections containing 1 row.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.controller sections]count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
And here my definition of commitEditingStyle, where i remove cell from table and delete data from DB (object type "Transactions" is a subclass of NSManagedObjectContext, that define my model).
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Transactions *trans = (Transactions *)[self.controller objectAtIndexPath:indexPath];
//Delete transaction
[self.context deleteObject:trans];
NSError *error = nil;
[self.context save:&error];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
I find where's the problem.
The delegate is defined in a controller called from a UITabBar. Every time its view appears, a function is called to Fetch data on NSFEtchedResultController(with performFetch) and setup the delegate for the NSFetchedResultControllerDelegate. This way (i don't know exactly why) every time i try to delete or insert cell/section on table, the delegate seems to be duplicated as many time as i opened the controller via UITabBar, thus Delegate function are repeated more than one time creating problem and errors that i described in the question.
To avoid this problem, now i setup Request and Delegate in init function of ViewController and in no other places. This ensure that it set only one reference to delegate and only one Fetch is performed over NSFetchResultController.

Obj-C, have a number of tableviews, need to quickly add code to say not data found?

I have a number of views with UITableViews and I'd like to quickly add code to each so that I can add a row with a simple label saying that there was no data to be shown.
Whats making things a little bit more complicated is that in some cases I'm using custom cells.
I don't want to have to change my populate data array functions, but I can't think of a way to add this feature to a lot of tables quickly.
Can anyone show me what to do ?
In the tableView:numberOfRowsInSection: data source method, get the number of rows as normal. Then, if the number is 0, return 1 instead. In the tableView:cellForRowAtIndexPath: method, perform the same test. If there is no data, use a different cell and set it up for your message.
The following example assumes that you have 1 section and that your data is an NSArray instance variable named theArray which contains strings.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger num = theArray.count;
return (num ? num : 1);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(theArray.count == 0) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
cell.textLabel.text = #"No Data Found";
return cell;
}
// Normal processing here
}