Gents,
I need some help here. It's almost like I know what I'm trying to do but I've been unable to get it work when coding it out. The scenario is I have a view controller with a table view in it. My table view has three different dynamic prototypes, since I have three different kinds of cells. Now, all I want to do, is to specify which prototype cell to generate in which row. I have also given each of the prototype cells unique identifiers in storyboard.
The way I understand things is that my cellforRowatIndexpath methods needs to understand which kind of cell to display in that row, and then select the identifier as such, dequeue and set the content as required.
Correspondingly, This is my code what I'm trying to do:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
{
if(indexPath.row==0)
{
static NSString *CellIdentifier = @"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// configure your cell here...
return cell;
}
}
Unfortunately, things don't work as planned and my app crashed giving me a 2013-02-07 16:47:39.859 ProjectsABC30068:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSIndexPath setTableViewStyle:]: unrecognized selector sent to instance 0x83812e0' error.
There are no issues with the table view as such since I've set the datasource and delegate properly. And also made an outlet for the table view.
I've also tried to change the if statement in cellforRowAtIndexPath to if(myTable.indexPathForSelectedRow==0). This produces the dynamic prototype in all the cells but at least the app doesn't cease.
What do you guys think is the problem?
I don't know how to correctly use index path, if someone could help me with that, I would appreciate it.
You said it's reporting:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSIndexPath setTableViewStyle:]: unrecognized selector sent to instance 0x83812e0
You get this when cellForRowAtIndexPath
is not returning a valid UITableViewCell
.
What value did you return from tableView:numberOfRowsInSection:
? The reason I ask is that your cellForRowAtIndexPath
will fail for any row except the first one. If your tableView:numberOfRowsInSection:
indicated a value greater than 1, then your app will crash.
To confirm this, you might want to examine what indexPath
is at the the start of your cellForRowAtIndexPath
, by adding this line at the start of the method:
NSLog(@"%s: section = %d; row = %d", __FUNCTION__, indexPath.section, indexPath.row);
That way you can make sure what rows you're trying to generate a cell for and that you're correctly handling it.
Later you say:
I've also tried to change the if statement in cellforRowAtIndexPath to if(myTable.indexPathForSelectedRow==0). This produces the dynamic prototype in all the cells but at least the app doesn't cease.
That definitely isn't going to work and it implies a confusion of how cellForRowAtIndexPath
works. It has nothing to do with currently selected cells. You respond to numberOfSectionsInTableView
and tableView:numberOfRowsInSection:
by telling it which cells your are able to create, and iOS will then call cellForRowAtIndexPath
repeatedly for each of the cells it determines might be visible given the number of sections and number of rows.
Make sure that your numberOfSectionsInTableView
and tableView:numberOfRowsInSection:
methods only return values that you correctly handle in your cellForRowAtIndexPath
.
For further reading, I'd suggest you go through the Table View Programming Guide for iOS or google "UITableView tutorial".