I have a table view form which user is supposed to enter some information (question and choices) in. Basically, when user clicks the last section, text fields and other elements are being collected.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
AskCellQuestion *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"askQuestionCell"];
if (cell == nil) {
cell = [[AskCellQuestion alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"askQuestionCell"];
}
...
cell.questionText.delegate = cell;
return cell;
} else if (indexPath.section==1) {
if (indexPath.row < numberOfChoices) {
AskCellChoice *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"askChoiceCell"];
if (cell == nil) {
cell = [[AskCellChoice alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"askChoiceCell"];
}
...
cell.choiceText.delegate = cell;
...
return cell;
} else {
AskCellChoiceAdd *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"askChoiceAddCell"];
if (cell == nil) {
cell = [[AskCellChoiceAdd alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"askChoiceAddCell"];
}
return cell;
}
} else if (indexPath.section==2) {
...
}
// Configure the cell...
return 0;
}
In didSelectRowAtIndexPathfunction, I'm trying to access these text fields and take their values:
UITableView * questionFormView = [self tableView];
AskCellQuestion * questionCell = (AskCellQuestion *)[questionFormView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * questionText = questionCell.questionText.text;
NSLog(#"TXt: %#",questionText);
UIImage * questionImage = questionCell.questionImage;
NSLog(#"IMG: %#",questionImage);
NSString * questionVideo = questionCell.youtubeVideoID;
NSLog(#"VID: %#",questionVideo);
for (int i = 0; i < numberOfChoices; i++) {
AskCellChoice * choiceCell = (AskCellChoice *)[questionFormView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
NSLog(#"C TXt: %#",choiceCell.choiceText.text);
NSLog(#"C IMG: %#",choiceCell.choiceImage);
NSLog(#"C VID: %#",choiceCell.youtubeVideoID);
}
It is giving this output in console:
2012-03-17 01:04:46.700 x[2346:207] TXt: (null)
2012-03-17 01:04:46.701 x[2346:207] IMG: (null)
2012-03-17 01:04:46.701 x[2346:207] VID: (null)
2012-03-17 01:04:46.701 x[2346:207] C TXt: TEST CHOICE 1
2012-03-17 01:04:46.701 x[2346:207] C IMG: <UIImage: 0x6ec75f0>
2012-03-17 01:04:46.702 x[2346:207] C VID: FMij4sZioBM
2012-03-17 01:04:46.702 x[2346:207] C TXt: TEST CHOICE 2
2012-03-17 01:04:46.702 x[2346:207] C IMG: <UIImage: 0x6e7ce30>
2012-03-17 01:04:46.702 x[2346:207] C VID: GfIcEzFzqKE
In short, I can not access the text field in section 0. I tried adding a second row and it returned null too. I tried swapping question and choice cells' sections, that time it returned null for section 0 which corresponded to choices. Question cell worked well. I also tried increasing section numbers, so question cells were in section 1, choices were section 2 etc... then it returned null for section 1. I couldn't figure out what is going on, it's a really weird problem.
By the way, I can access the cell in didSelectRowAtIndexPath when indexPath is equal to the index path of the row in section 0. But when it's not, it's returning null when I do [NSIndexPath indexPathForRow:0 inSection:0].
For example in didSelectRowAtIndexPath:
if (indexPath.section==4) {
NSLog(#"TEST 1 - %#", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).questionText.text);
//[self askTheQuestion];
} else if (indexPath.section==0) {
NSLog(#"TEST 2 - %#", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).questionText.text);
NSLog(#"TEST 3 - %#", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:indexPath]).questionText.text);
}
Gives this output when I write 'fooBar' to the question input in section 0 and click the cell at section 4 ('Ask' button):
2012-03-17 01:38:47.125 x2516:207] TEST 1 - (null)
and gives this when I click the cell at section 0, which is the cell with question input:
2012-03-17 01:38:44.782 x[2516:207] TEST 2 - fooBar
2012-03-17 01:38:44.793 x[2516:207] TEST 3 - fooBar
Thanks in advance...
I am thinking that to press the Add cell (indexPath section == 1, row == numberOfChoices), the cell at section 0, row 0 is outside the screen. As I understand UITableViews, UITableViewCells are being reused. Generally, if the cell is outside of the screen, it goes into the reusable pool. (If I'm wrong, please correct me.) Calling a cell outside the screen may or may not give the correct values. I would suggest storing the values in the cells somewhere else (like an #property) so that you can retrieve them later.
Related
So I am trying to select objects from my array, to be able to delete them when I do an IBAction. I tried:
checking if the item is Selected:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.editEnabled) {
RDNote *selectedNote = [self.notes objectAtIndex:indexPath.row];
if (selectedNote.isSelected) {
selectedNote.selected = NO;
for (NSIndexPath *indexPathFromArray in self.indexPathsOfSelectedCells) {
if (indexPathFromArray.row == indexPath.row) {
[self.mutableCopy removeObject:indexPathFromArray];
}
}
} else {
selectedNote.selected = YES;
[self.indexPathsOfSelectedCells addObject:indexPath];
}
[self.collectionView reloadData];
IBAction:
- (IBAction)didTapTrashBarButton:(id)sender {
NSMutableArray *mutableNotes = [NSMutableArray arrayWithArray:self.notes];
for (NSIndexPath *indexPath in self.indexPathsOfSelectedCells) {
[mutableNotes removeObjectAtIndex:indexPath.row];
}
self.notes = [NSArray arrayWithArray:mutableNotes];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithArray:self.indexPathsOfSelectedCells]];
} completion:^(BOOL finished) {
self.indexPathsOfSelectedCells = nil;
[self activateEditMode:NO];
[self saveDataToFile:self.notes];
}];
}
But I am having issues with indexes e.g.:(some times shows me the Error that Object index 2 is not between [0..1]), and there are bugs while selecting multiple Object and Deleting them.
Please help me with an advice for some other method that I can use, a Code will be perfect! Thanks!
This problem arrises because:
Let say you have five objects in array 1,2,3,4,5
you are running a loop for removing object based on indexpath which are selected rows. Now your indexpath contains 1st row and 3rd row.
while executing it for the first time you will remove object 1. Now 2,3,4,5 will left in the array. Now second time your indexpath.row is 3rd. It will remove third object which is 4 but in actual array it was 3.
Your code is crashing sometimes because if you have selected first row and last row. In this case i have selected 1 and 5. Now my indexpaths array will say I have to delect objectsAtIndexes 1 and 5.
While executing the loop I'll remove object at Index 1. Now i will be left with 2,3,4,5. On second iteration it will say remove objectAtIndex 5 where as Index 5 doesn't exist because now we have 4 elements in array.
In such cases best way of doing this is try removing elements in array from the end like remove 5th element first and then go for other one. Run your loop in reverse order.
NSInteger i = [self.indexPathsOfSelectedCells count]-1;
while (i > 0){
NSIndexPath *indexPath = [self.indexPathsOfSelectedCells objectAtIndex:i];
[mutableNotes removeObjectAtIndex:indexPath.row];
i--;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = #"MessageCellIdentifier";
MessageTableViewCell* cell = (MessageTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Message* message = [self.dataModel.messages objectAtIndex:indexPath.row];
[cell setMessage:message];
return cell;
}
I am developing an chat application in which i am getting exception when sending and receiving messages happening at the same time.The following is the exception message
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070 2013-04-30
16:55:14.314 [2689:907] Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Invalid update: invalid
number of rows in section 0*.
The number of rows contained in an existing section after the update (19) must be equal to the number of rows contained in that section before the update (17), 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).'
- (int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataModel.messages count];
}
- (void)didSaveMsg:(NSMutableArray *)array1
{
self.dataModel.messages = array1;
[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:array1.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[self scrollToNewestMessage];
}
- (void)scrollToNewestMessage
{
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(self.dataModel.messages.count - 1) inSection:0];
[tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
[tableview reloadData];
[[DBModel database]updateMessageCount1:mobileNo cnt:#"0"];
}
The error message is fairly self explanatory - you are telling the table view you want to insert a single cell, but in the table's datasource are then saying it has two extra cells to display (19 instead of 17).
The problem almost certainly isn't in the code you've posted, it will be either in the numberOfRows method of your datasource, or in the code at the point at which you insert the extra cell.
I integrated GrabKit in my iPhone-App, which is a Library to Grab Photos from social Networks. Now I have the following situation/problem:
I have a UITableViewController - AlbumListViewController.
Then there's a UITableViewCell - AlbumCellViewController.
When you tap on one of these cells - albums with photos
There's a UITableViewController - PhotoListViewController
and a UITableViewCell - PhotoCellViewController.
Everything here works just finde, I can browse albums, choose one, browse the photos in it and then when I choose one of the single photos, there is a PushViewController to my SetPhotoViewController, which displays the selected image in Fullscreen.
Now when I want to use the back-Button of my navigation bar in the SetPhotoViewController, the crashes with the following message:
*** Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:], /SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:400
2012-10-22 22:49:32.814 Amis[1820:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to allocate data stores for 1073741821 rows in section 1. Consider using fewer rows'
*** First throw call stack:
(0x23de012 0x1b63e7e 0x23dde78 0x15f9f35 0xc8f83b 0xc923c4 0xb56fa2 0xb5692c 0x1690f 0x1cd653f 0x1ce8014 0x1cd87d5 0x2384af5 0x2383f44 0x2383e1b 0x2a9a7e3 0x2a9a668 0xaab65c 0x42fd 0x2b75)
libc++abi.dylib: terminate called throwing an exception
DataSource and Delegate of my two UITableViewControllers are both set to File's Owner.
When I debug trough the code, I can't find any special things, all photos can be loaded, all the arrays are filled with data, there's nothing strange.
Here are the two functions of the PhotoListViewController which get called, as soon as I press the back button on my NavigationController:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSLog(#"%i", indexPath.row);
NSLog(#"%ii", indexPath.section);
// Extra Cell
if ( indexPath.section > _lastLoadedPageIndex ){
static NSString *extraCellIdentifier = #"ExtraCell";
cell = [tableView dequeueReusableCellWithIdentifier:extraCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:extraCellIdentifier];
}
cell.textLabel.text = #"load more";
cell.textLabel.font = [UIFont fontWithName:#"System" size:8];
} else {
static NSString *photoCellIdentifier = #"photoCell";
cell = [tableView dequeueReusableCellWithIdentifier:photoCellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"PhotoRowViewController" owner:self options:nil] objectAtIndex:0];
}
}
// setting of the cell is done in method [tableView:willDisplayCell:forRowAtIndexPath:]
return cell;
}
and..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// extra cell
if ( [cell.reuseIdentifier isEqualToString:#"ExtraCell"] ){
if ( state == GRKDemoPhotosListStateAllPhotosGrabbed ) {
cell.textLabel.text = [NSString stringWithFormat:#" %d photos", [[_album photos] count] ];
}else {
cell.textLabel.text = [NSString stringWithFormat:#"Loading page %d", _lastLoadedPageIndex+1];
[self fillAlbumWithMorePhotos];
}
} else // Photo cell
{
NSArray * photosAtIndexPath = [self photosForCellAtIndexPath:indexPath];
[(PhotoRowViewController*)cell setPhotos:photosAtIndexPath];
}
}
What am I missing?
Edit: The code of the numberOfRowsInSection-Method:
If I debug it, the first time res is equal 0, the second time the method gets called, res is equal 322121535.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSUInteger res = 0;
if ( state == GRKDemoPhotosListStateAllPhotosGrabbed && section == _lastLoadedPageIndex ) {
NSUInteger photosCount = [_album count];
// Number of cells with kNumberOfPhotosPerCell photos
NSUInteger numberOfCompleteCell = (photosCount - section*kNumberOfRowsPerSection*kNumberOfPhotosPerCell) / kNumberOfPhotosPerCell;
// The last cell can contain less than kNumberOfPhotosPerCell photos
NSUInteger thereIsALastCellWithLessThenFourPhotos = (photosCount % kNumberOfPhotosPerCell)?1:0;
// always add an extra cell
res = numberOfCompleteCell + thereIsALastCellWithLessThenFourPhotos +1 ;
} else if ( section > _lastLoadedPageIndex) {
// extra cell
res = 1;
} else res = kNumberOfRowsPerSection;
return res;
}
I'm the developer of GrabKit. Thanks for using it :)
Actually, the controllers in GrabKit are there for demonstration purpose only, they are not designed to be used "as is" in a project, and more specifically : The GRKDemoPhotosList controller was not made to have another controller pushed in the controllers hierarchy.
So what you need is just a little fix to make grabKit demo's controllers fit to your project :)
I guess the problem is the following :
_ in [GRKDemoPhotosList viewWillAppear], the method fillAlbumWithMorePhotos is called.
_ when you pop back from your controller to GRKDemoPhotosList, this method is called one more time, and it generates this bug.
Try to add a flag in the viewWillAppear method, to avoid calling fillAlbumWithMorePhotos twice, I guess it'll work fine.
Feel free to contact me by mail ( pierre.olivier.simonard at gmail.com ) or on twitter ( #pierrotsmnrd ) if you need more informations or help :)
I know a question like this has been posted before, but I was trying to get a direct answer specific to my code. I have seen the other post and have followed it to get to this stage.
I have the (null) libc++abi.dylib: terminate called throwing an exception line in my log. I have traced it to this line of code:
normalCell.textLabel.text = [[displayArray objectAtIndex:indexPath.row] objectForKey:#"name"];
this is in my cellForRowAtIndexPath method which has this code:
static NSString *normalCellIdentifier = #"normalCell";
static NSString *topRowCellIdentifier = #"topRowCell";
UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
if (normalCell == nil)
{
normalCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
}
UITableViewCell *topRowCell = [tableView dequeueReusableCellWithIdentifier:topRowCellIdentifier];
if (topRowCell == nil)
{
topRowCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topRowCellIdentifier];
}
// Configure the cell...
if (searchBar.selectedScopeButtonIndex != 0)
{
normalCell.textLabel.text = [[displayArray objectAtIndex:indexPath.row] objectForKey:#"name"];
}
if (segmentedControl.selectedSegmentIndex == 0)
{
if (indexPath.section == 0)
{
[self makeButtonsForTableView:tableView atIndexPath:indexPath withTableViewCell:topRowCell];
[topRowCell addSubview:button1];
[topRowCell addSubview:button2];
topRowCell.selectionStyle = UITableViewCellSelectionStyleNone;
return topRowCell;
}
else
{
return normalCell;
}
}
else
{
return normalCell;
}
The makeButtonsForTableView has all my code for the button, but that isn't the problem because I have removed that from the code and the problem still persists so I don't need to include that.
The displayArray varies its contents depending on the selectedSegmentIndex on a UISegmentedControl and on a UISearchBar's selected scope.
Each of the Arrays it gets its values from import from my website domain, the arrays are:
modsArray
itemsArray
serversArray
pluginsArray
newItemsArray
newBlocksArray
newMobsArray
Thanks for any help you can give.
-- EDIT 22:40GMT - 27/09/2012 --
So I logged the contents of displayArray when the app loads, and this is the log
2012-09-27 22:39:56.539 AppName[22949:11303] (
)
2012-09-27 22:39:56.675 AppName[22949:11303] (
)
-- EDIT 23:13GMT - 27/09/2012 --
Interestingly, when I change tabs and go back the log changes from this:
2012-09-27 23:13:14.074 MinePedia[23853:11303] DisplayArray Count: 0
2012-09-27 23:13:14.074 MinePedia[23853:11303] IndexPath Row: 0
2012-09-27 23:13:14.355 MinePedia[23853:11303] DisplayArray Count: 0
2012-09-27 23:13:14.355 MinePedia[23853:11303] IndexPath Row: 1
to this
2012-09-27 23:13:14.074 MinePedia[23853:11303] DisplayArray Count: 1
2012-09-27 23:13:14.074 MinePedia[23853:11303] IndexPath Row: 1
2012-09-27 23:13:14.355 MinePedia[23853:11303] DisplayArray Count: 1
2012-09-27 23:13:14.355 MinePedia[23853:11303] IndexPath Row: 0
Anyone know what this means?
-- EDIT 23:42 - 27/09/2012 --
Ok so sort of a different problem than before, it now doesn't crash so thats been solved, thanks Mayur, but now on startup and until the tab is changed at the top, the scope and table view remains with the indexpath being larger than the count. How do I fix this?
Keep it as following :
normalCell.textLabel.text = [NSString stringWithFormat:#"%#",[[displayArray objectAtIndex:indexPath.row] objectForKey:#"name"]];
This way, even if null value comes up then it won't crash.
Also, always check if array is filled up, only then access the list, like this.
if (searchBar.selectedScopeButtonIndex != 0)
{
if(indexPath.row < [displayArray count])
{
normalCell.textLabel.text = [NSString stringWithFormat:#"%#",[[displayArray objectAtIndex:indexPath.row] objectForKey:#"name"]];
}
}
Per your update in the comments, your displayArray has no elements when you call objectAtIndex: on it. This causes an exception. You need to ensure that your tableView:numberOfRowsInSection: returns a number of rows for this section equal to displayArray's count.
I'm working on populating the table view with an array of dictionaries. The contents of the arrays and the dictionaries are parsed without problems. However, the table is populated with [array count] number of cells with contents with index 0 of the array. It seems to me that indexPath.row returns 0 for all cells. How do I populate each cell with the corresponding index?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *term = [self.terms objectAtIndex:indexPath.row];
cell.textLabel.text = [term objectForKey:#"content"];
cell.detailTextLabel.text = [term objectForKey:#"created"];
return cell;
}
Thanks a lot!
Edit: Here's what I got for logging indexPath
2011-11-21 03:07:58.025 Demo[21269:f803] 2
indexes [0, 0]
2011-11-21 03:07:58.027 Demo[21269:f803] 2
indexes [1, 0]
Seems like the first index is updating while the second is not.
When I logged indexPath.row, however
2011-11-21 03:19:40.306 Demo[21546:f803] 0
2011-11-21 03:19:40.308 Demo[21546:f803] 0
So what should I use to get the value that is incrementing?
Thanks for the help again.
That block of code looks fine in itself. Maybe your tableview has multiple sections with one row each? What are you returning for the datasource methods tableView:numberOfRowsInSection: (should be [self.terms count]
) and numberOfSectionsInTableView: (should be 1).
If those are OK, put NSLog(#"%#",indexPath); somewhere in the method and post the output into your question.
I have just had a similar issue (indexPath.row was always 0) and noticed how much of an idiot I was. In my numberOfSectionsInTableView I was returning the [dataSet count] and in my tableView:numberOfRowsInSection I was returning 1. Should be the other way around. Oops!