Objective C- numberOfRowsInSection method is not called properly in UITableView - objective-c

In my table view, I have 7 section. Each section count is 3 initially.
When I launch my app, numberOfRowsInSection method is calling from last section count(6) but data are displayed in all sections properly.
When I reload my table after insert event numberOfRowsInSection is calling again from 6 not from 0 so how to resolve it?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *arrayOfItems = [workScheduleArray objectAtIndex:section];
return arrayOfItems.count;
}
-(void)insertNewDutyTimeRow:(UIButton *)addNewRowBtn atIndexPath:(NSIndexPath *)addNewDutyIndex
{
insertNewRow = [[NSMutableArray alloc]init];
[insertNewRow addObject:#"Start Time"];
[insertNewRow addObject:#"End time"];
[insertNewRow addObject:#"Service type"];
newArray = [workScheduleArray objectAtIndex:addNewDutyIndex.section];
[newArray insertObject:insertNewRow atIndex:addNewDutyIndex.row+1];
NSLog(#"WorkSchedule array:%#",workScheduleArray[addNewDutyIndex.section]);
[self.personalTable reloadData];
}

Your numberOfRowsInSection is generic, it returns the same number for all your sections. it begins by section 0 and ends by your last section (in your case its 6). So this is why you must specify all the cases. For example you can do this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
NSMutableArray *arrayOfItems = [workScheduleArray objectAtIndex:section];
return arrayOfItems.count;
{...}
}
}

Related

UITableView with dynamic sections and custom cells

My data structor looks like this:
Inside an NSArray I have a few objects from different classes. How the array looks like if something like this:
Let's say myArray has 3 objectAs. Each objectA and an NSArry that has objectBs
Now at runtime I do not know the array counts.
My UITableView displays one objectA per section. Each section has 1 row. Unless my objectA has an NSArray of objectBs with a count of greater than 0.
So the UITableView would look like this if all my objectA's has no arrays of objectB's in them.
Section 1: Row 1: `objectA instance
Section 2: Row 1: `objectA instance
Section 3: Row 1: `objectA instance
Lets say the 3rd objectA has an array of objectB's with a count of 1. My UITableView would look like this:
Section 1: Row 1: `objectA instance
Section 2: Row 1: `objectA instance
Section 3: Row 1: `objectA instance
Section 3: Row 2: `objectB instance
Now I am using two different UITableViewCells for each object so objectA would use CustomCell1 and objectB would use CustomCell2
Here is where I am getting stuck - I need to return the correct cell to the correct section / row.
So I made sure my UITableView knows what to expect.
I wrote the methods like so:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ObjectA *objectA = self.myArray[section];
return [objectA.objectBArray count] +1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.myArra count];
}
So numberOfRowsInSection takes a look at the objectBs array and returns its count + 1 so that even if its 0 there is always one row. (I need at least one row for objectA in the current section.
numberOfSectionsInTableView is straight forward - one section for each objectA in self.myArray
This seems to do exactly as I need.
I then got stuck on cellForRowAtIndexPath method.
This is how far I got:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:#"cell1"];
CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:#"cell2"];
ObjectA *objectA = self.myArray[indexPath.section]; //Each section has one ObjectA.
//Make sure we have some objectB's to display first
if ([objectA. objectBArray count] > 0){
ObjectB *objectb = (ObjectB*) objectA.objectB[indexPath.row];
//if the current section (section 3) has a indexPath 0 already
// index path 1 would have the first objectB
}
cell1.objectA = objectA;
cell2.objectB = objectB;
//Now return the correct cell for the current section / index path
}
So I need to check if the current section already has valid cell1 object if it does I then need to return cell2 to the second or third row. for objectB As row one must always have a cell1.
I know I am almost there. I just can't figure out how to return the correct cell.
Update
So the cellForRowAtIndexPath method is only being called three times. Here is what I did in numberOfRowsInSection method to make sure it returns the right number of rows:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ObjectA *objectA = self.myArray[section];
NSLog (#"Section:%ld", (long)section);
NSLog(#"Returning %ld", (long)[objectA.objectBArray count]+1);
return [objectA.objectBArray count] +1;
}
My console shows this:
2014-07-16 19:36:17.488 App[15473:60b] Section:2
2014-07-16 19:36:17.488 App[15473:60b] Returning 2
2014-07-16 19:36:18.128 App[15473:60b] Section:0
2014-07-16 19:36:18.128 App[15473:60b] Returning 1
2014-07-16 19:36:19.063 App[15473:60b] Section:1
2014-07-16 19:36:19.063 App[15473:60b] Returning 1
Any ideas?
If there will always only be one objectA in each section then you can test if your indexPath.row is anything larger then zero (not the first row).
See if this does the trick:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:#"cell1"];
CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:#"cell2"];
ObjectA *objectA = self.myArray[indexPath.section]; //Each section has one ObjectA.
cell1.objectA = objectA;
//Check if not first row and objectB's exist
if (indexPath.row > 0 && [objectA. objectBArray count] > 0){
ObjectB *objectb = (ObjectB*) objectA.objectB[indexPath.row];
cell2.objectB = objectB;
return cell2;
}
return cell1;
}
This is an answer to the second issue I was having. After setting breakpoints in my view controller - I noticed my tableView was already loading cells before my call to [self.tableView reloadData];
Not sure why that was happening so a quick nil-check in my rows for items method solved the issue:
if the main array had no objects - return 0. If it did - return objectBArray count + 1; Now the cellForRowAtIndexPath is called the correct number of times.

Objective-C Sectioned UITableView

I want a sectioned UITableView but I don't know how, my situation:
I have 2 arrays
array Date ("12/08/13", "13/08/13", "17/08/13")
array Count("2", "1", "5")
The count means how many rows are there in the section.
I have
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [dateArray objectAtIndex:section];
}
But how can make in the first section, 2 rows in the second section 1 row and in the third section 5 rows.
I hope someone can help me. If you have questions just ask.
Thanks.
EDIT: I get my information from my website so the array changes everyday.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// length of your section count array:
return [sectionCountArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// the section count from your array, converted to an integer:
return [sectionCountArray[section] integerValue];
}
UPDATE: From your comment is seems that you have a "flat" data source array.
Since the row numbers start with zero in each section, you have to compute the
index into the array from the row number and the section count of all previous sections
in cellForRowAtIndexPath:
NSInteger flatIndex = indexPath.row;
for (NSInteger sec = 0; sec < indexPath.section; sec++)
flatIndex += [tableView numberOfRowsInSection:sec];
cell.textLabel.text = yourFlatDataArray[flatIndex]; // (Just an example!)
You return the number of sections in the numberOfSectionsInTableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.countArray count];
}
Then get the number from the array and return that in the numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSNumber *count = [self.countArray objectAtIndex:section];
return [count integerValue];
}
You need to override the following UITableView datasource method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 2
else if (section == 1)
return 1
else (section == 2)
return 5
}

NSInternalInconsistencyException with UITableViewController and a Storyboard-Pop

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 :)

NSTableView crashs when changing data with selected row

In my cocoa app i have 2 NSTableViews. The AppDelegate is the datasource for both of them. I want to change the data of the second TableView depending on the selected row of the first TableView. For that i use an NSDictionary. I add an array for every entry in the data array of Table1.
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
if([table1 selectedRow] != -1 && [[aTableView identifier] isEqualToString:#"table2"]){
return [[dict objectForKey:[arrForTable1 objectAtIndex:[table1 selectedRow]]] count];
}
if([[aTableView identifier] isEqualToString:#"table1"]) return [arrForTable1 count];
return 0;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
if([table1 selectedRow] != -1 && [[aTableView identifier] isEqualToString:#"table2"]){
return [[dict objectForKey:[arrForTable1 objectAtIndex:[table1 selectedRow]]] objectAtIndex:rowIndex];
}
if([[aTableView identifier] isEqualToString:#"table1"]){
return [arrForTable1 objectAtIndex:rowIndex];
}
return nil;
}
This code crashes when in table2 a row is selected and after that a switch in the table1 to a row with an empty array.
How can i fix it? I tried to [table2 deselectAll:self] in the - selectionShouldChangeInTableView: method but it doesn't work. It works to trigger the deselectAll: method with an button and that was ok.
Sounds like you might need to call [table2 reloadData] when your selection in table1 changes.

TitleForHeaderInSection throwing EXC_BAD_ACCESS

I am trying to create a generic UITableView in my iPhone app.
I have a UITableView which populates the data using an array via a SELECT query loop.
I add the data into my array and populate the array in cellForRowAtIndexPath:.
I get the section header using that array and by using a sort method, I put the section headers in Array1.
I would like to have titleForHeaderInSection: work by having section 0 be a static header name and sections 1 and later become generic, meaning the header name will come from Array1.
I am not sure how can I create that logic since the app always throws EXC_BAD_ACCESS with the code below.
My logic: I keep the count of the array in an int and see if the value is greater than 0. If it is, I add the section header for and objectAtIndex:0, otherwise I use the static one. But when the count gets to 2, for section 2 and objectAtIndex:1, it breaks and throws EXC_BAD_ACCESS.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int value = [[self Array1] count];
if(section == 0)
return #"Countries";
if (value > 0) {
if (section == value){
return [[self Array1] objectAtIndex:section - 1];
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int count = [[self Array1] count];
return count + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int value = [[self Array1] count];
if (section == 0) {
return [self.Array count];
}
if (value > 0) {
if (section == [[self Array1] count]) {
NSString *initialLetter = [[self Array1] objectAtIndex:section - 1];
// get the array of elements that begin with that letter
NSArray *elementsWithInitialLetter = [self elementsWithInitialLetter:initialLetter];
// return the count
return [elementsWithInitialLetter count];
}
}
}
Looks like you're just missing a retain on the iVar backing the Array1 method. declare the array as a property:
#property (nonatomic, retain) NSArray* datastore;
Then cache the value you're referring to in the Array1 method in this method (probably in viewDidLoad).
self.datastore = [self Array1];
Then replace all remaining references to [self Array1] with self.datastore. Build run and see if it still crashes. (Don't forget to set self.datastore = nil in your dealloc!