how to get a handle on the UITableViewCell given NSIndexPath* iOS - ios

I have the indexPath of type NSIndexPath*,a reference to the tableView, I want to use these to get a handle on the corresponding cell.How dp I do this?

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
But note that that method returns nil if corresponding row is not visible at the moment (or indexPath is out of the table's range)

Related

How to change specific element in row without reloading row in Objective-C?

I want to update a UiSwitch in a row in UITableView. I want to make it on and off with animation programmatically.
NSIndexPath *indexPath = [[NSIndexPath indexPathForRow:0 inSection:lastSelectedCardSectionNumber] retain];
newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:#"newCell" forIndexPath:indexPath];
[cell.switch1 setOn:YES animated:YES];
but the above line not changes the state and I have to reload row which doesn't make UiSwitch state change with animation.
There is a view on top of the switch which will user taps on it to change the state of switch. but it should be change just in some situations. Therefore I don't allow user to change it I want to change it programmatically.
The problem is with how you are retrieving the cell. It should be:
newCell *cell = (newCell *)[self.tableView cellForRowAtIndexPath:indexPath];
not this:
newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:#"newCell" forIndexPath:indexPath];
The former gets the already existing instance of cell but the later one creates the new instance of the cell.

get the invisible UITableViewCell

I used the code below to get an invisible UITableViewCell
NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [listTableview cellForRowAtIndexPath:path];
path returns something, but cell returns nil.
It looks like the code above only can get the visible UITableViewCell.
Your comment welcome
The docs clearly say it needs to be visible.
An object representing a cell of the table, or nil if the cell is not
visible or indexPath is out of range.
Also, you are initializing the index just like how you would intialize any other variable, which has nothing to do with it having to be a valid indexPath.

Getting previous UITableViewCell in loading of next cell

So, this is weird.
I have a UITableView that loads cells in from cellForRowAtIndexPath. I'd like to know the state of the previous cell before properly loading the current cell, so I load in the previously created cell with CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]]; but I am always getting nil.
I'm not sure what's going on as this process seems pretty simple - does the table view not save the cell on each run of cellForRowAtIndexPath?

How to create object in Objective-C?

I am new in iOS development. Currently I am reading this tutorial http://www.appcoda.com/how-to-handle-row-selection-in-uitableview/ . I am facing problem when I am reading this line
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
I know object in objective-c is created by following way
classname *objecname = [[classname alloc]init];
My confusion point is here UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
How cell object is created here? Please tell details.
Based on the tutorial you linked to, I'm assuming you are dealing with selecting a row. When you select a row, you have access to the NSIndexPath of that row, which contains two parts:
The section of the cell
The row of the cell
With that information, let's break down the confusing code: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell
This part declares your variable. You've chosen to name it cell. You could just as easily have named it theCell, like so:
UITableViewCell *theCell
The * means that you're declaring a pointer, which is just a reference to an actual object, or the table view cell.
[tableView cellForRowAtIndexPath:indexPath]
The tableView refers to the UITableView that was just selected. UITableView has a method called cellForRowAtIndexPath, and what that method does is retrieve the cell at the specified section and row of the UITableView. In your case, it retrieves the row that you've just selected and stores the reference to it in your cell variable.
When declaring a new object, yes, it would take on the following syntax:
classname *objecname = [[classname alloc] init];
The key word here is new. When dealing with selecting rows in a UITableView, you don't want to create a new cell because you can't select a cell that doesn't exist. You want to get the cell that the user has just selected.
I assume you have this code in your didSelectRowAtIndexPath: method, is that right?
If so, you're tapping on a cell that is on the screen, so the cell object already exists. Where and how was it created? Inside cellForRowAtIndexPath:, which you have already written.
So when you make this call
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
it just gives you the cell object to work with, but it doesn't create a new one.
The cell is not created with that line. That line gets a reference to a cell that is already in the table view at that indexPath.

How to get UITableViewCell's index from its UITextField?

I have a UITextField in a custom cell inside table. I created new class DataCell which is subclass of UITableViewCell. Inside DataCell I created outlets for textfields and I also have method inside implementation file which uses 'editing did end' and I manipulate textField values there.
I am now wondering how to get rowIndex or number of the cell, as each time I click + button new custom cell is loaded on the table. If I get tag I always get same tag number regardless of the cell I selected.
The text field passed to your delegate is a subview of the cell's contentView.
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
You can use this logic when you are not sure of hierarchy between textfield and cell.
UITableViewCell *cell = nil;
UIView *parentView = textField.superview;
while(parentView) {
if([parentView isKindOfClass:[UITableViewCell class]]) {
cell = parentView;
break;
}
parentView = parentView.superview;
}
if(cell)
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Add tags to the text field in the tableView:cellForRowAtIndexPath: method. In this example, I have a custom cell with a label and a text field:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.label1.text = self.theData[indexPath.row];
cell.textField.tag = indexPath.row;
return cell;
}
It sounds like you are maybe handling the end of editing in your custom cell class, but you might want to consider doing it in the table view controller instead, since that gives you easy access to the model, which I presume you are modifying with what the user types in the text field. If you do that, then you should connect the text field's delegate property up to the table view controller in IB.
If we're accepting fragile answers then for the sake of contributing something new to the conversation:
CGRect rectInTableView =
[tableView convertRect:textField.bounds fromView:textField];
NSUInteger indexOfCellContainingTextField =
(NSUInteger)(rectInTableView.y / tableView.rowHeight);
Assumptions that make it fragile: (i) all rows are the same height; (ii) the height is set on the table view. If you haven't implemented UITableViewDelegate -tableView:heightForRowAtIndexPath: then both of those assumptions will hold true. You're also taking advantage of the fact that casting a positive float to an integer rounds down.
I would argue that although still not completely clear of assumptions, this is less fragile than Mundi's solution because it makes assumptions only about things you do directly control (ie, cell sizing) and not about things you don't (ie, the view hierarchy UIKit uses internally to present table views).

Resources