I'm trying to add custom cells to a static UITableViewController. I use SWTableViewCell to get the swipable cell and I'm using a custom -xib for the cell layout.
I seems to work fine when I only need to use one cell, but when there are more then one I get this: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
This happens when I use dequeueReusableCellWithIdentifier in the cellForRowAtIndexPath when the second cell should appear (when I begin to scroll).
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.images = selectedReport.image;
imagesArray = [self.images.allObjects mutableCopy];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self.tableView registerNib:[UINib nibWithNibName:#"PictureCell" bundle:nil] forCellReuseIdentifier:#"PictureCell"];
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
{
if(section == 0){
return 10;
}else if(section == 1){
return imagesArray.count; //It is the second section which is dynamic
}
return 1;
}
- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString* cellIdentifier = #"PictureCell";
if (indexPath.section == 0 || indexPath.section == 2)
{ //Takes the rows from the storyboard
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
else
{
NSLog(#"%i",indexPath.row);
NSLog(#"%i",indexPath.section);
PictureTableViewCell *cell = (PictureTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; //Crash here when indexPath.row == 1
[cell setCellHeight:cell.frame.size.height];
cell.containingTableView = tableView;
indexPath.section, indexPath.row];
Image *tempImage = imagesArray[indexPath.row];
cell.image.image = [UIImage imageWithData:tempImage.image];
cell.lblDescription.text = tempImage.image_description;
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
return cell;
}
}
I have checked that numberOfRowsInSection returns 2 when section == 1.
Thank you for any assistance!
Use this instead
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
or in viewDidLoad, register the class like
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"PictureCell"];
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PictureCell" forIndexPath:indexPath];
}
Related
How To Create Expanding And Collapsing Of Row in UITableView ,
I followed this video ,"https://www.youtube.com/watch?v=OYaI5ASpsOE" after clicked on cell I got following error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to compare <NSIndexPath: 0x600000029560> {length = 2, path = 0 - 18446744073709551615} with object of different class: NSArray'
Please use This library. Just add it into your project using POD.
pod 'ExpandableCell'
Import it into your viewController
import ExpandableCell
Make ExpandableTableView in Storyboard or in code
#IBOutlet var tableView: ExpandableTableView!
Inherit ExpandableDelegate
class ViewController: UIViewController, ExpandableDelegate
Set delegate
tableView.expandableDelegate = self
Reference: https://github.com/younatics/ExpandableCell
Below tableview delegate and datasource code i used crash is not reproducible to me
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrowtitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ExpandingCell *cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"Expanding Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblrow.text = self.arrow[indexPath.row];
cell.lblrowtitle.text = self.arrowtitle[indexPath.row];
cell.lblfruit.text = self.arrFruit[indexPath.row];
NSInteger cal = (indexPath.row) * 25;
cell.lblcalcal.text = [NSString stringWithFormat:#"%ld",(long) cal];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return (self.selectedIndex == indexPath.row) ? 120 : 45 ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedIndex == indexPath.row) {
self.selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if (self.selectedIndex == -1) {
NSIndexPath *prev = [NSIndexPath indexPathForRow:self.selectedIndex inSection:0];
self.selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:#[prev] withRowAnimation:UITableViewRowAnimationFade];
}
self.selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
i am developing an app with the help of this example in my app and everything is working as required but when i expand the expended cell and scroll down app crashes when table reaches at the end. let me post all my code here.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = nil;
static NSString *cellIdentifier = nil;
// init expanded cell
if ([indexPath isEqual:self.expandedIndexPath]) {
cellIdentifier = #"ExpandedCellIdentifier";
}
// init expanding cell
else {
cellIdentifier = #"Cell";
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.cellImageView.image = [UIImage imageNamed:#"placeholder.png"];
}
if ([[cell reuseIdentifier] isEqualToString:#"ExpandedCellIdentifier"]) {
cell.playIconBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cell.playIconBig.tag= indexPath.row-1;
[cell.playIconBig addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchDown];
cell.playIconBig.frame = CGRectMake(25, 5, 25, 25);
[cell.playIconBig setBackgroundImage:[UIImage imageNamed:#"play_expand.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:cell.playIconBig];
}
// set text in expanding cell
if ([[cell reuseIdentifier] isEqualToString:#"Cell"]) {
[cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:#"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.clipsToBounds = YES;
//[cell.contentView addSubview:arrowImg];
cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
cell.artist.text = [NSString stringWithFormat:#"By %#",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
cell.share.text = [[rssOutputData objectAtIndex:indexPath.row]xmllikes];
cell.download.text = [[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
}
// set text in expanding cell
if ([[cell reuseIdentifier] isEqualToString:#"Cell"]) {
[cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:#"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.clipsToBounds = YES;
//[cell.contentView addSubview:arrowImg];
cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
cell.artist.text = [NSString stringWithFormat:#"By %#",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// take expanded cell into account when returning number of rows
if (self.expandedIndexPath) {
return [rssOutputData count] + 1;
}
return [rssOutputData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath isEqual:self.expandedIndexPath])
{
return 40;//height for expanded
} else {
return 140;//height for normal
}
}
and here didselectrowatindexpath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view addSubview:_internetConnectionIndicator];
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
if ([[cell reuseIdentifier] isEqualToString:#"ExpandedCellIdentifier"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[theTableView deleteRowsAtIndexPaths:#[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[theTableView insertRowsAtIndexPaths:#[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
- (NSIndexPath *)actualIndexPathForTappedIndexPath:(NSIndexPath *)indexPath
{
if (self.expandedIndexPath && [indexPath row] > [self.expandedIndexPath row]) {
return [NSIndexPath indexPathForRow:[indexPath row] - 1
inSection:[indexPath section]];
}
return indexPath;
}
#pragma mark - UIScrollViewdelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
for (FSParallaxTableViewCell *cell in self.theTableView.visibleCells) {
[self updateImageViewCellOffset:cell];
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self updateImageViewCellOffset:(FSParallaxTableViewCell *)cell];
}
Please help me debugging my code. Thanks here is error msg
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = nil;
static NSString *cellIdentifier = nil;
// init expanded cell
if (indexPath.row >= [rssOutputData count])
cellIdentifier = #"ExpandedCellIdentifier";
}
// init expanding cell
else {
cellIdentifier = #"Cell";
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.cellImageView.image = [UIImage imageNamed:#"placeholder.png"];
}
if (indexPath.row >= [rssOutputData count]){
cell.playIconBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cell.playIconBig.tag= indexPath.row-1;
[cell.playIconBig addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchDown];
cell.playIconBig.frame = CGRectMake(25, 5, 25, 25);
[cell.playIconBig setBackgroundImage:[UIImage imageNamed:#"play_expand.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:cell.playIconBig];
}else {
[cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:#"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.clipsToBounds = YES;
//[cell.contentView addSubview:arrowImg];
cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
cell.artist.text = [NSString stringWithFormat:#"By %#",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
cell.share.text = [[rssOutputData objectAtIndex:indexPath.row]xmllikes];
cell.download.text = [[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// take expanded cell into account when returning number of rows
if (self.expandedIndexPath) {
return [rssOutputData count] + 1;
}
return [rssOutputData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath isEqual:self.expandedIndexPath])
{
return 40;//height for expanded
} else {
return 140;//height for normal
}
}
I'm quite sure that the method tableView:numberOfRowsInSection: returns the wrong number, it returns the number that bigger than the data size. Please check carefully these lines of code:
if (self.expandedIndexPath) {
return [rssOutputData count] + 1;
}
Assure that the condition of the if statement is correct. There is a case that you have modified the data else where but you don't know about that.
If you are returning number of rows after adding +1 to the data source than
if (self.expandedIndexPath) {
return [rssOutputData count] + 1;
}
you should ensure that for the additional row you should not access the modal / rssOutputData should be accessed till it bounds. If in case some how the code accesses the rssOutputData for the index for which the data doesn't exist than you should be ready to see error as below
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
Please re manipulate your code it's somehow trying to access the array out of bounds.
I am using tableview apple's lazy loading code in my project,but having exception in project. And error is - *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],here is my code please help.But it is working in other project.I have no delegate method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LazyTableCell";
static NSString *PlaceholderCellIdentifier = #"PlaceholderCell";
NSUInteger nodeCount = [self.entries count];
if (nodeCount == 0 && indexPath.row == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
cell.detailTextLabel.text = #"Loading…";
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nodeCount > 0)
{
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.name;
if (!appRecord.appIcon)
{
if (self.mytable_view.dragging == NO && self.mytable_view.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
Because cellForRowAtIndexPath is returning a nil value and then configureCellForDisplay is expecting a UITableViewCell. The storyboard or XIB does not have a cell defined with the cell identifier you specified. Better check the spelling of identifier.
Please check for Delegate.
Have you added both delegate or not?
UITableViewDelegate, UITableViewDataSource
import UIKit
class UserFriendVC: UIViewController, UITableViewDelegate, UITableViewDataSource
{
override func viewDidLoad() {
super.viewDidLoad()
}
}
you should add cellIdentifier from "Show the attributes inspector". Please fallow just like below snippet.
Firstly added below code to your-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delagate method.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ResultCustomTableViewCell *cell = (ResultCustomTableViewCell *)[resultTableView dequeueReusableCellWithIdentifier:#"ResultTableViewCellIdentifier"];
...
//set the cell property
...
return cell;
}
and then jump the "Show the attributes inspector", While selected cell root view.
And then paste the same identifier name to identifier section in "Show the attributes inspector". in this case i use this ResultTableViewCellIdentifier.
And one more think. if you use custom cell just like this scenario, you must add below delegate metod. Because your custom cell height can be higher or smaller original tableview height. And Also you should register this nib in viewDidLoad.
- (void)viewDidLoad {
[resultTableView registerNib:[UINib nibWithNibName:#"ResultCustomTableViewCell" bundle:nil] forCellReuseIdentifier:[ResultCustomTableViewCell reuseIdentifier]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat height = 0.0;
if (tableView == resultTableView){
height = 44.0f;
}
return height;
}
i hope, it'll fix your problem
CellIdentifier I bet your cellForRowAtIndexPath is returning nil.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"LazyTableCell";
static NSString *PlaceholderCellIdentifier = #"PlaceholderCell";
NSUInteger nodeCount = [self.entries count];
if (nodeCount == 0 && indexPath.row == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlaceholderCellIdentifier] autorelease];
}
cell.detailTextLabel.text = #"Loading…";
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (nodeCount > 0) {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.name;
if (!appRecord.appIcon)
{
if (self.mytable_view.dragging == NO && self.mytable_view.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
That's probably why [UITableView _configureCellForDisplay:forIndexPath:] is failing... because cellForRowAtIndexPath is returning a null value and then configureCellForDisplay is expecting a UITableViewCell.
Register your cell in the - (void)viewDidLoad method using
[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:#"Your Reuse Identifier"];
if your are using storyboards and use this if you are using a custom cell.
[self.tableView registerNib:UINib nibWithNibName:#"Your File Name.xib" bundle:nil forCellReuseIdentifier:#"Your Reuse Identifier"]
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.
Hi am creating a table with two prototype cells. The button "Cell One" is connected to the method btnCellOneTouched and button "Cell Two" is connected to btnCellTwoTouched. The button in each prototype cell has tag 0 and the textfield has tag 1.
Each prototype cell has its own identifier - cellOne and cellTwo respectively. This is the code in my .m file. The variables are all correct and give no error.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
intNumRows = 0;
numSections = 1;
}
- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
{
return numSections;
}
- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return intNumRows;
}
- (void)displayRowNum:(id)sender
{
UIButton *btn = (UIButton *)sender;
NSLog(#"%d", btn.tag);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellType];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellType];
[(UIButton *)[cell viewWithTag:0] addTarget:self action:#selector(displayRowNum:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)btnCellOneTouched:(id)sender
{
strCellType = #"cellOne";
intLastRow = [tblMain numberOfRowsInSection:0];
indexPath = [NSIndexPath indexPathForRow:intLastRow inSection:0];
intNumRows++;
[tblMain insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
- (IBAction)btnCellTwoTouched:(id)sender
{
strCellType = #"cellTwo";
intLastRow = [tblMain numberOfRowsInSection:0];
indexPath = [NSIndexPath indexPathForRow:intLastRow inSection:0];
intNumRows++;
[tblMain insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
I am getting an error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x90638e0' when i run the program.
What is wrong with the way I am assigning a method to the button in the cell created at runtime?
If I comment out the line where I add the target, it works fine.
This one has me flummoxed for a while. Please help.
Thanks,
Anjaan.