Reputation: 1200
In my controller class, I'm implementing the table view data source delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"In cell for row at index path %i ", indexPath.row);
// a bunch of other typical stuff
}
On a button push, I want to modify a certain cell. To get a reference, I do this:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
And I do get a reference to the cell, but my log method doesn't execute. It as is if the UITableViewController.tableView has a different delegate? When I do this:
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
It returns a different row as a result (it looks like a header?). Here are some NSLogs to show what's happening
NSLog(@"About to send message to tableView:cellForRowAtIndexPath:indexPathForRow:inSection");
MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *) [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
MCSwipeTableViewCell *theOtherCell = (MCSwipeTableViewCell *) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSLog(@"Here is the call on self %@", cell);
NSLog(@"Here is the delegate cell %@", theOtherCell);
NSLog(@"Here is my reference %@", self);
NSLog(@"Here is the table view delegate %@", self.tableView.delegate);
and the output:
2014-06-13 15:20:41.601 krow[28823:70b] Here is the call on self <MCSwipeTableViewCell: 0xa3eda00; baseClass = UITableViewCell; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x8d82dc0>; layer = <CALayer: 0x8d82de0>>
2014-06-13 15:20:41.601 krow[28823:70b] Here is the delegate cell <MCSwipeTableViewCell: 0xc43fa00; baseClass = UITableViewCell; frame = (0 0; 320 220); autoresize = W; gestureRecognizers = <NSArray: 0xa8be640>; animations = { position=<CABasicAnimation: 0x8d79040>; }; layer = <CALayer: 0xa87d070>>
2014-06-13 15:20:41.601 krow[28823:70b] Here is my reference <KrowInboxTableViewController: 0x8c779c0>
2014-06-13 15:20:41.601 krow[28823:70b] Here is the table view delegate <KrowInboxTableViewController: 0x8c779c0>
Why am I getting different rows for cellForRowAtIndexPath??
Upvotes: 1
Views: 968
Reputation: 23634
For starters, the cellForRowAtIndexPath:
method you are calling purposefully does not call the delegate to create a new cell. Unlike the delegate method tableView:cellForRowAtIndexPath:
which actually creates or dequeues a cell and returns it to the tableview, the cellForRowAtIndexPath:
method is simply there to give you a reference to an existing cell. The documentation backs this up in the description of the return value:
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
.
So again, the delegate method that the tableview calls on its own is there to create a cell, while the method you are calling is just there to grab an existing visible cell without creating or dequeueing anything.
As for the next bit, the problem is that you are calling a method that the tableview is supposed to call, which will create or dequeue a new cell rather than returning an already visible one. You should never call this method manually. The first method you were using cellForRowAtIndexPath:
is the right way to get an existing visible cell. Beyond that, you need to provide more info about what it is you're actually trying to accomplish if that doesn't solve your problem.
Upvotes: 5