your log says that it wants tableView:heightForRowAtIndexPath method
just implement this method as below
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
Question
I can't seem to figure out why my app crashed when adding objects to my table. I used this (http://www.theappcodeblog.com/2011/05/09/parsing-xml-in-an-iphone-app-tutorial/) tutorial to parse XML, which it does fine, but does not add the data from my parsed XML to a UITableView.
Here is the code I'm using:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"starting table");
static NSString *cellid = @"Cell";
row *curname = [[xpar worker] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid]autorelease];
}
picframe = CGRectMake(0, 0, 100, 100);
UIImageView *imgview = [[UIImageView alloc]initWithFrame:picframe];
imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", curname.pic]];
[cell.contentView addSubview:imgview];
nameframe = CGRectMake(105, 50, 50, 20);
namecon = [[UILabel alloc] initWithFrame:nameframe];
namecon.tag = 0011;
namecon.numberOfLines = 1;
namecon.font = [UIFont boldSystemFontOfSize:12.0];
[cell.contentView addSubview:namecon];
jobframe = CGRectMake(165, 75, 50, 20);
jobcon = [[UILabel alloc] initWithFrame:jobframe];
jobcon.tag = 0012;
jobcon.numberOfLines = 1;
jobcon.font = [UIFont systemFontOfSize:12.0];
[cell.contentView addSubview:jobcon];
majframe = CGRectMake(165, 25, 50, 20);
majcon = [[UILabel alloc] initWithFrame:majframe];
majcon.tag = 0013;
majcon.numberOfLines = 1;
majcon.font = [UIFont systemFontOfSize:12.0];
[cell.contentView addSubview:majcon];
namecon = (UILabel*)[cell.contentView viewWithTag:0011];
namecon.text = [curname name];
NSLog(@"%@", namecon.text);
jobcon = (UILabel*)[cell.contentView viewWithTag:0012];
jobcon.text = [curname job];
majcon = (UILabel*)[cell.contentView viewWithTag:0013];
majcon.text = [curname major];
return cell;
}
I'm using a storyboard with a UITableView and a TableViewCell with the Identifier "Cell" like in the code. The UIViewController's class is the above code's .m file, the UITableView is using Dynamic Prototypes, the UITableView's dataSource and Delegate Outlets are the above code's .m file, and the .m file's Outlet is linked to the UITableView.
I'm also very new to Objective-C and iOS dev, so i'm sorry if this is a very obvious answer. I've tried a ton of different things to no avail. If there are any other parts of my code I should post I will do it ASAP.
Thanks!
EDIT: Log after crash:
2014-04-07 13:18:17.383 LabWorker[80097:60b] -[workersViewController
tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x10c3d4a60
2014-04-07 13:18:17.386 LabWorker[80097:60b] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[workersViewController
tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x10c3d4a60'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101acd495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010182c99e objc_exception_throw + 43
2 CoreFoundation 0x0000000101b5e65d -[NSObject(NSObject)
doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101abeb85 ___forwarding___ + 453
4 CoreFoundation 0x0000000101abe938 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001005eba30 -[UISectionRowData
heightForRow:inSection:canGuess:] + 234
6 UIKit 0x00000001005f2334 -[UITableViewRowData
rectForRow:inSection:heightCanBeGuessed:] + 582
7 UIKit 0x00000001005f2432 -[UITableViewRowData
rectForGlobalRow:heightCanBeGuessed:] + 150
8 UIKit 0x00000001005f2dd5 -[UITableViewRowData
globalRowsInRect:canGuess:] + 359
9 UIKit 0x0000000100494531 -[UITableView
_visibleGlobalRowsInRect:] + 210
10 UIKit 0x00000001004937ab -[UITableView
_updateVisibleCellsNow:] + 881
11 UIKit 0x00000001004a5721 -[UITableView layoutSubviews] +
207
12 UIKit 0x0000000100439993 -[UIView(CALayerDelegate)
layoutSublayersOfLayer:] + 354
13 QuartzCore 0x000000010242a802 -[CALayer layoutSublayers] + 151
14 QuartzCore 0x000000010241f369
_ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
15 QuartzCore 0x000000010241f1ea
_ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x0000000102392fb8
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
17 QuartzCore 0x0000000102394030 _ZN2CA11Transaction6commitEv +
394
18 QuartzCore 0x000000010239469d
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
19 CoreFoundation 0x0000000101a98dc7
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x0000000101a98d37 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x0000000101a78522 __CFRunLoopRun + 946
22 CoreFoundation 0x0000000101a77d83 CFRunLoopRunSpecific + 467
23 GraphicsServices 0x0000000102f79f04 GSEventRunModal + 161
24 UIKit 0x00000001003d9e33 UIApplicationMain + 1010
25 LabWorker 0x0000000100002fa7 main + 103
26 libdyld.dylib 0x00000001068985fd start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
EDIT: More code/explanation:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"sections");
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"rows, %lu", (unsigned long)[[xpar worker] count]);
return [[xpar worker] count];
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"CGFloat");
return 100;
}
It runs and gets 11 rows for the [[xpar worker] count] (correct number), then runs the "CGFloat" method 11 times, and then it shows me exactly what the log I posted above shows.
EDIT: Where it crashes:
#import <UIKit/UIKit.h>
#import "labAppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
---> return UIApplicationMain(argc, argv, nil, NSStringFromClass([labAppDelegate class]));
}
}
it says it crashes where the arrow above it in the main.m file.
argc = 1
**argv = (char) '/'
Solution
your log says that it wants tableView:heightForRowAtIndexPath method
just implement this method as below
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
OTHER TIPS
The reason for your crash can be an invalid index at line
row *curname = [[xpar worker] objectAtIndex:indexPath.row];
I have not tried running the code.But you can log the count of [xpar worker] and check if it is more than indexPath.row or not.You can also use a defensive code i.e
if([[xpar worker] count] > indexPath.row)
{
row *curname = [[xpar worker] objectAtIndex:indexPath.row];
}
But to be sure you find out where the crash is by using the exception breakpoint by adding it as follows:
The application will stop at the point the app is crashing and you can check the values of your object if they are nil or for any other reason.