Reputation: 401
I have a subclass of UITableViewCell
called ReminderCell
.
ReminderCell
has a method called backToPlaceAnimation
that I wrote.
The problem is for example if I want to call backToPlaceAnimation
on the first ReminderCell
from RemindersVC
using tableView: cellForRowAtIndexPath:
it's not returning the correct object (it's UITableViewCell
object and not ReminderCell
at all).
How can I get the correct object?
Code:
NSIndexPath *cellIndexPath=[NSIndexPath indexPathWithIndex:0];
ReminderCell *cell=(ReminderCell*)[self tableView:self.AroundersTableView cellForRowAtIndexPath:cellIndexPath];
[cell cancelDeletion];
Crash log:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.
cellForRowAtIndexPath: override:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating a default cell using "cellIdentifier"(NSString)
ReminderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell==nil) {
cell=[[ReminderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
//Changing "cell"'s "index" to indexPath.row
cell.index=(int)indexPath.row; //Crashes here
cell.textLabel.text=@"Hello";
//Returning cell
return cell;
}
Thank you!
Upvotes: 1
Views: 111
Reputation: 668
You should replace [NSIndexPath indexPathWithIndex:0];
with [NSIndexPath indexPathWithIndex:0 inSection:0];
Upvotes: 1
Reputation: 941
Based on the error you showed above you're not getting the cell back from the cell for row method because the index path you're creating is incorrect. Try using [NSIndexPath indexPathForRow:0 inSection:0]
(Note: you will need to change the 0's to reflect the cell you want.
Upvotes: 0
Reputation: 2766
Your CellForRowAtIndexPath should return ReminderCell instead of UITableViewCell
So it should start with this:
-(ReminderCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Upvotes: 0
Reputation: 3013
define your cell as ReminderCell
ReminderCell *cell = [self.AroundersTableView cellForRowAtIndexPath:cellIndexPath];
Upvotes: 0
Reputation: 668
You are declaring cell
as UITableViewCell
, declare it as ReminderCell
if you want it to be that type
Upvotes: 0