I have tableview with searchDisplayController. When I search for some text I successfully display the results on screen. My problem arises when I want to add a cell to existing results. Adding a cell must be done when searchDisplayController is active.
That is:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary * dict ;
BOOL isSearchView = tableView == self.searchDisplayController.searchResultsTableView;
if (isSearchView) {
dict = [searchTasks objectAtIndex:indexPath.row];
[self.searchDisplayController.searchResultsTableView deselectRowAtIndexPath:indexPath
animated:YES];
}
else{
dict = [self.tasks objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Task * task = [self getsomeTask];
NSIndexPath * ip = [NSIndexPath indexPathForRow:indexPath.row+1
inSection:indexPath.section];
if (isSearchView) {
[searchTasks insertObject:task atIndex:ip.row];
[self.searchDisplayController.searchResultsTableView
insertRowsAtIndexPaths:#[ip]
withRowAnimation:UITableViewRowAnimationRight];
}
after this is executed, and after the last line insertRowsAtIndexPaths:#[ip],
the code executes:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchDisplayController.isActive) {
return [searchTasks count];
} else {
return [tasks count];
}
}
Which is fine and here it selects the first option which is true according to program logic. But the it crashed and after this execution it never calls
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function of UITableview.
And gives me error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
I cannot understand the reason why it does not call the cellForRowAtIndexPath function after rowcount function.
Any suggestions?
Related
I want to display data in uitableview when the particular uitextfield is tapped. I am not getting the data in taleview. Please check the below and help me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setFont:[UIFont fontWithName:#"Bold" size:20]];
if (ethnicityField.tag == 1)
{
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}
if (languageField.tag == 2)
{
cell.textLabel.text = [langData objectAtIndex:indexPath.row];
}
Here, when I click on the first textfield it shows the empty table and crashes.
It's not taking the exact tag value. Please help.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
int row = 0;
if (ethnicityField.tag == 1)
{
row = [data count];
}
if (languageField.tag == 2)
{
row = [langData count];
}
return row;
}
Crash report:
Actually I have 2 arrays, one with 5 values and another with 11 values, the problem is both textfield takes first array and if I select the 5th object in second textfield it shows the following error.
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 9 beyond bounds [0 .. 4]'
I believe ethnicityField and languageField always have the same tag so numberOfRowsInSection always returns [data count];
What you could do is checking whether a textfield is active when you display your table.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
int row = 0;
if ([ethnicityField isFirstResponder])
{
row = [data count];
}
else if ([languageField isFirstResponder])
{
row = [langData count];
}
return row;
}
I have a one View controller managing 2 tableviews. I use a flag to track which table is selected. In each of the delegate functions I just check the flag and use the right table.
Everything works great except that when i load the second table which has lesser items than the first one, crashes when I scroll the table , for the following error.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'
* First throw call stack:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Drawing Row = %d Total num Of items = %d", indexPath.row, [[self.fetchedResultsControllerComments fetchedObjects] count]);
Prints this:
Drawing Row = 2 Total num Of items = 0
If the number of items in this table is correct, then why is this function getting called in the first place?
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentSelectionTableType1)
{
// Draw first kind of cell.
PlainImageCell *cell1 = [tableView dequeueReusableCellWithIdentifier:#"ImageCell"];
if(cell1 == nil)
cell1 =[[PlainImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ImageCell"];
[self configureCell1:cell1 atIndexPath:indexPath];
return cell1;
}
// else Draw the second kind of cell
PlainTextCell *cell2 = [tableView dequeueReusableCellWithIdentifier:#"TextCell"];
if(cell2 == nil)
cell2 =[[PlainTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TextCell"];
[self configureCell2:cell2 atIndexPath:indexPath];
return cell2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(currentSelectionTableType1)
return [[self.fetchedResultsControllerDataSource1 sections] count];
return [[self.fetchedResultsControllerDataSource2 sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo;
if(currentSelectionTableType1)
{
sectionInfo = [self.fetchedResultsControllerDataSource1 sections][section];
}
else
{
sectionInfo = [self.fetchedResultsControllerDatasource2 sections][section];
}
return [sectionInfo numberOfObjects];
}
Thx
EDIT - based on the code you added:
You need to define one cell before your conditional and then configure that cell based on the conditional and then return the cell after the conditional. If you need both an ImageView and a TextCell, you can configure those objects in the conditional code.
Why not just use one TableView with two datasources and switch out the datasources as needed?
Something like this:
#property(nonatomic, strong) NSArray *tableViewDataSource1;
#property(nonatomic, strong) NSArray * tableViewDataSource2;
#property(nonatomic) BOOL usingDataSource2;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.usingDataSource2) {
return [self.tableViewDataSource2 count];
}
return [self. tableViewDataSource1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create the cell before conditional
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"reuseIdentifier" forIndexPath:indexPath];
// Conditionally configure the cell
if (self.usingDataSource2) {
// Configure Cell using self.tableViewDataSource2 data
} else {
// Configure Cell using self.tableViewDataSource1 data
}
// Return the configured cell after the conditional
return cell;
}
I have added two prototype cells to my table view named FeedItem and ButtonItem. And my cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(indexPath.row != [feeds count] - 1)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"FeedItem"];
FeedItem *item = [feeds objectAtIndex:indexPath.row];
[self configureTextForCell:cell withFeedItem:item];
[self configureCheckmarkForCell:cell withFeedItem:item];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ButtonItem"];
}
return cell;
}
But my app crashes with this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Can someone point what I am doing wrong ?
My app crashes when I try to insert a new entry. I received this error previously, and I was not able to fix it. Can anyone help?
Here is my error displayed in the console:
2013-09-14 15:41:00.370 Probation App[9919:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0xd7b81 0x33eb228d 0x33f34f81 0x328f6277 0x31fbd5df 0x31fbd291 0x31fbbf01 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0x542fd 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception
and here is my code:
#pragma mark UITableViewDataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if( nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
if (indexPath.row <self.probationers.count)
{
Probationer *thisProbationer = [self.probationers objectAtIndex:indexPath.row];
cell.textLabel.text = thisProbationer.probationerName;
cell.detailTextLabel.text = thisProbationer.probationerID;
}
else
{
cell.textLabel.text = #"Add Probationer";
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(NSInteger)tableView: (UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
NSInteger count = self.probationers.count;
if(self.editing)
{
count = count +1;
}
return count;
//return [self.probationers count];
}
//Deleting an Entry
-(void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle) editing forRowAtIndexPath: (NSIndexPath *) indexPath
{
if (editing == UITableViewCellEditingStyleDelete)
{
[self.probationers removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark UITableViewDelegate Methods
-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Probationer *chosenProbationer = [self.probationers objectAtIndex:indexPath.row];
ProbationerController *detailedViewController = [[ProbationerController alloc]init];
detailedViewController.delegate = self;
detailedViewController.currentProbationer = chosenProbationer;
if (indexPath.row <self.probationers.count && !self.editing)
{
[self.navigationController pushViewController:detailedViewController animated:YES];
}
if (indexPath.row == self.probationers.count && self.editing)
{
AddProbationerController *addProbationer = [[AddProbationerController alloc] init];
[self.navigationController pushViewController:addProbationer animated:YES];
}
[tv deselectRowAtIndexPath:indexPath animated:YES];
//selectedIndexPath = indexPath;
//[self.navigationController pushViewController:detailedViewController animated:YES];
}
The number of rows should always come from the data source (the bit you have commented out //return [self.probationers count];). Don't try to just add to the number. Add to the data source and then refresh the table view.
The stack trace say it all. Its definitely because of you self.probationers array.
Why don't you NSLog the number of items in the array and the number of rows/sections in your tableview.
When you are trying to associate indexPath.row and self.probationers you have to make sure they match with number of elements.
Also follow the basics while accessing elements of array(as they are much prone to get exceptions)
Nil check for the accessing array.
Know the exact count of the array, by logging.
Make sure you are trying to access any objects more than the available array indexes.
I have a editable UITableView (with adding and deleting elements) in my application.
It working strange.
While I have only one or two lines in it, everything is working perfect.
But if I add more items, I have an exception '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
I was unable to put breakpoints and handle it.
Maybe, somebody can help me?
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
return [typeList count] + plusRow;
}
- (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];
}
if ([indexPath row] > ([typeList count]-1)) {
NSString * appendCellDescription = #"";
if ([[self tableView] isEditing]) appendCellDescription = #"Add";
[[cell textLabel] setText:appendCellDescription];
} else {
NSLog(#"Accessing array: %d", [indexPath row]);
[[cell textLabel] setText:[[typeList getObject:[indexPath row]] description]];
}
return cell;
}
#pragma mark - Editing table view
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"Row: %d, count: %d", [indexPath row], [typeList count]);
if (indexPath.row > [typeList count]-1) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
- (IBAction)btnModifyClick:(id)sender{
if ([[self tableView] isEditing]) {
[[self tableView] setEditing:FALSE animated:YES];
[[self tableView] reloadData];
} else {
[[self tableView] setEditing:TRUE animated:YES];
[[self tableView] reloadData];
}
}
Generally This Error describe, When mistake in Array Index such like you array have 3 item and you tring to get/abstract 4th item from Array, at that time this type of Error generate.
Best way to find error use BreakPint. and Debug your project line by line. and find where your app. crush ?? i am sure that it cruse Around any array.
EDIT:
I thinks mistake in array name typeList check it in cellForRowAtIndexPath method with BreakPoint.
numberOfRowsInSectionis returning higher number of items count than the one fetched in cellForRowAtIndexPath datasource method, try debugging the returned value:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
NSLog(#"%i",[typeList count] + plusRow);
return [typeList count] + plusRow;
}
I have found a way, to solve my problem.
I just changed "Sections" to 0 in XCode properties in storyboard TableView.
And now working ok.
Thanks to everyone to response!