TableView throws error when trying to implement dymanic cell height - ios

I'm trying to customize the height of my tableView cells. I tried a few tutorials, but nothing seems to work. "Funny thing" is that the error message is always the same. Here is the code I'm using atm:
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.tabelle objectAtIndex:indexPath.row];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
And this is the error message I get:
2012-05-09 20:01:55.553 APP[436:f803] -[APP sizeWithFont:constrainedToSize:]: unrecognized selector sent to instance 0x687f5f0
2012-05-09 20:01:55.555 APP[436:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[APP sizeWithFont:constrainedToSize:]: unrecognized selector sent to instance 0x687f5f0'
*** First throw call stack:
(0x128e022 0x1846cd6 0x128fcbd 0x11f4ed0 0x11f4cb2 0x42c9 0x3ab688 0x3ad862 0x25b66d 0x25b167 0x2f4d 0x34e8 0x29638f 0x2965eb 0x2a6ff1 0x2a785f 0x2a79e1 0x3c55c2 0x20bd21 0x128fe42 0xea679 0xf4579 0x794f7 0x7b3f6 0x108160 0x1cbe84 0x1cc767 0x1db183 0x1dbc38 0x1cf634 0x2181ef5 0x1262195 0x11c6ff2 0x11c58da 0x11c4d84 0x11c4c9b 0x1cbc65 0x1cd626 0x24a5 0x1d35)
terminate called throwing an exception
My first thought was that it doesn't like me setting the font size of the labels in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But that doesn't seem to be the case

The reason you're getting this error is cause this:
[self.tabelle objectAtIndex:indexPath.row];
Will return the custom object you have in tabelle, not an NSString, and you're calling an NSString method on it. Maybe you mean to use
[[self.tabelle objectAtIndex:indexPath.row] Name]
or
[[self.tabelle objectAtIndex:indexPath.row] m_yolo]
Depending on which text you want to base the size on.
EDITED TO USE THE CUSTOM OBJECTS IN TABELLE

Related

Overriding didSelectRowAtIndexPath on a framework-provided view controller?

So I'm using the CoreAudioKit's CABTMIDICentralViewController to present a list of MIDI Bluetooth devices for the user to select. However, I want to be able to tell which device the user selected when they're done, but it doesn't seem Apple has added any way to do that.
So I'm trying to hack it by detecting when the user selects a row in the table:
DPBleMidiDeviceManager.h:
#import <CoreAudioKit/CoreAudioKit.h>
#interface DPBleMidiDeviceManager : CABTMIDICentralViewController
#end
DPBleMidiDeviceManager.m:
#import "DPBleMidiDeviceManager.h"
#implementation DPBleMidiDeviceManager
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"midi device selected %#", indexPath);
//either of these next lines crash, it makes no difference:
//[[tableView delegate] tableView:tableView didDeselectRowAtIndexPath:indexPath];
[super tableView:tableView didDeselectRowAtIndexPath:indexPath];
}
#end
The problem is, it's crashing on the last line saying there's no selector. Which is weird, because if I remove the super call, it doesn't crash, but it also doesn't correctly connect to the BLE device, like it would if I was not overriding that delegate call.
Is this just something Apple does so you can't access their tables? Why would they build a UI view like this and let you call it, but not give you any information about what the result was? Am I missing some standard way to do this?
EDIT: here's the details of the crash with the super call:
2015-10-29 15:14:37.039 [626:338267] midi device selected <NSIndexPath: 0x1473ae20> {length = 2, path = 0 - 3}
2015-10-29 15:14:37.039 [626:338267] -[DPBleMidiDeviceManager tableView:didDeselectRowAtIndexPath:]: unrecognized selector sent to instance 0x147f34e0
2015-10-29 15:14:37.040 [626:338267] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DPBleMidiDeviceManager tableView:didDeselectRowAtIndexPath:]: unrecognized selector sent to instance 0x147f34e0'
*** First throw call stack:
(0x2b3cdfef 0x3967dc8b 0x2b3d3409 0x2b3d11bf 0x2b300e78 0xaa165 0x2eb3956b 0x2ebe843b 0x2ea9da91 0x2ea1838f 0x2b393fed 0x2b3916ab 0x2b391ab3 0x2b2de201 0x2b2de013 0x32aab201 0x2ea82a59 0x88447 0x39c09aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
That's not how you call super. It should be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"midi device selected %#", indexPath);
[super tableView:tableView didDeselectRowAtIndexPath:indexPath];
}

iOS Populate table with mutablearray data

I have a JSON file: http://astrodeer.com.au/ingredients.php which has been been parsed in. I created two NSMutableArrays, one for the ingredients and one for the ratings for each product listed.
First of all I have populated my first table with the product name/brand:
When a user clicks on an item, it takes them to this page where I am attempting to populate the table with the ingredient names and their corresponding ratings.
This is what I have so far. I am getting an error at "numberOfRowsInSection"
"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]'"
Ideally I am going to change the UIImage depending on the rating number using an if statement. But at the moment I am struggling to work out how to get the ingredient name to display.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [_selectedProduct.ingredientsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"ingredientsCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *ingLabel = (UILabel *)[myCell viewWithTag:1];
UIImage *ingRating = (UIImage *)[myCell viewWithTag:2];
ingLabel.text = _selectedProduct.ingredientsArray[indexPath.row];
return myCell;
}

beginUpdates endUpdates crash with nothing between

I am entirely confused. I am modifying my table and it's crashing while up via adding/deleting rows and reloading sections. So I stopped adding and deleting rows, and it still crashes. So I stopped reloading sections and it still crashes. So I just put the code:
[self.tableView beginUpdates];
[self.tableView endUpdates];
with this code it crashes, without it's fine.
Why?
2014-09-12 21:52:41.257 weatherApp[12616:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(0x2ea84f83 0x392ffccf 0x2e9bb7cb 0x3149eaef 0x3149cd8f 0x3149c843 0x31474e5b 0xeb82d 0xed059 0x313d48f7 0x3137bc27 0x3137b47d 0x312a1d59 0x30f1f62b 0x30f1ae3b 0x30f1accd 0x30f1a6df 0x30f1a4ef 0x30f6dc49 0x33d3a75d 0x2f770451 0x2ea44ea9 0x2ea4fa67 0x2ea4fa03 0x2ea4e1d7 0x2e9b8ebf 0x2e9b8ca3 0x33912663 0x3130514d 0x107599 0x3980cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Edit: Be advised I am calling this update in cellForRowAtIndexPath because I need to check for an update as new cells are displayed. The code that causes this update is the [self updateSection code, which right now only runs begin updates, end updates.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WFHNotamTableViewCell *cell = (WFHNotamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
if (!cell)
{
NSLog(#"Dequeue blank cell");
}
NSLog(#"Loading %d - %d", indexPath.section, indexPath.row);
NSString *group;
NSString *icao = [metarManager getIdentifierAtTotalIndex:indexPath.section andReturnGroup:&group];
NSMutableDictionary *notamDict = [notamManager getNotamFor:icao atVisibleIndex:indexPath.row];
NSDate *lastUpdate = [notamDict objectForKey:#"timeDownloaded"];
[self updateSection:indexPath.section andLastUpdate:lastUpdate];
return cell;
Thank you!

App crashing when it executes the table delegate method

When I run the search function in aepub reader my app crashes. It enters the cellfor row at index method and when it executes NSLOg(#"%#",hit.neighbourText) it shows exception.
(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.adjustsFontSizeToFitWidth = YES;
NSLog(#"indexpath%d",indexPath.row);
NSLog(#"%#",[results objectAtIndex:[indexPath row]]);
hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
if([results count]>0) {
NSLog(#"%#",hit.neighboringText);
cell.textLabel.text = [NSString stringWithFormat:#"...%#...", hit.neighboringText];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Chapter %d - page %d", hit.chapterIndex, hit.pageIndex+1];
return cell;
}
}
I am getting some value for hit.neighboringText but after that, I reload my tableview then the following exception will be raised, why?
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString
neighboringText]: unrecognized selector sent to instance 0x1481c4'
*** First throw call stack:
It's because hit is actually an NSString object and not a SearchResult object as you expect:
hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
The clue is in the exception text:
-[__NSCFConstantString neighboringText]: unrecognized selector sent to instance ...
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
No amount of casting to SearchResult is going to change that.
EDIT: Actually anywhere you see a cast you should be suspicious of the actual object you are dealing with. If you aren't sure then check it with isKindOfClass:.
it means that hit = (SearchResult*)[results objectAtIndex:[indexPath row]]; returns a ConstantString and not SearchResult object
Best would be checking if hit is the same class type as SearchResult before getting the value from neighboringText
you can try like something like this:
if([hit isKindOfClass:[SearchResult Class]]){
// do something with hit
}
else{
// different class
}
The answer to your problem lies in the error message:
unrecognised selector sent to instance 0x1481c4.
What you need to do next is print the value of that address via po 0x1481c4. It would appear that it is not actually a string, but you are not showing that code.
I guess there are two possibilities:
hit is not a SearchResult object but a String object
hit or the results array is not owned anymore / released but not set to nil and point garbage, which I believe is the case because I have experienced it before
I think you need to make sure that the array is not autoreleased/released at that point (for example if you are creating it with [NSArray arrayWith...] it is autoreleased, you might not own it inside cellForRowAtIndexPath) and the hit object is properly initialized before giving it to the results array.

Custom UITableViewCells Alloc Problrm

So I have custom UITableViewCells (made with storyboard), and when I do not literally alloc, init the cells, the app crashes. However, since they are custom cells, I don't know how to alloc/init them.
This code crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
return cell;
}
with this printed to the console:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I understand this means its crashing because there is no cell allocated yet, however when I try to allocate it, I have to specify a cell style.
So, when I build the cell as follows, it doesn't crash:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
if (cell == nil)
cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TableViewCellsID"];
return cell;
}
The problem is the code that works (the second set), requires me to set a UITableViewStyle. And as far as I know, there is no UITableViewStyleCustom.
Any helps would be appreciated.
It crashes in first case because there is no cell to reuse, and you are calling dequeueReusableCellWithIdentifier.
EDIT -
For custom cell initialisation you can check following threads -
How to make custom TableViewCell with initWithStyle after 3.0
initWithFrame vs initWithStyle

Resources