I am inside
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
At this point I have de indexPath and the tableView. How do I get a reference to the UICell object the indexPath represents?
thanks.
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
Related
I have created a custom cell that places a UITextField in each row of a UITableView. What I would like to do is enable the UITextField for a particular row (i.e. activate the cursor in that textfield), when the user presses on anywhere inside the cell.
I have the following method where I try to do this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField setUserInteractionEnabled:TRUE];
}
However, this is not working. Can anyone see what I'm doing wrong?
You're on the right track placing this inside didSelectRowAtIndexPath:. However, setting user interaction enabled on the textfield just means that the user would be allowed to interact with the object if they tried. You're looking for becomeFirstResponder.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Try this code:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField becomeFirstResponder];
}
What is this _cell iVar? Are you setting it somewhere?
I think what you are trying to accomplish is this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Notice that in this way you are getting the cell for the tapped indexPath.
And also, just for information, it's a pattern in objective-C to use booleans as YES or NO, instead of true or false.
How can I display the cell selected from my tableview in NSLog?
Implement didSelectRowAtIndexPath UITableview delegate and log the indexPath.row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected tableview row is %d",indexPath.row);
}
If you want to show what is being displayed in the cell, e.g. on its textLabel, use this in the didSelectRowAtIndexPath:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"selected cell textLabel = %#",cell.textLabel.text);
I want to change text of custom UITableviewCell text on didSelectRowAtIndexPath and I am using following code:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.lblName.text=#"cambridge";
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
but I am getting "request for member 'levelNo' in something not a structure or union". However I am able to set it at cellForRowAtIndexPath.
Please help
try
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.lblName.text=#"cambridge";
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//update your data, your data source must be mutable
//in case your array content are NSString, just do
[yourMutableArrayDataSource replaceObjectAtIndex:indexPath.row withObject:#"cambridge"];
//or if your data array holds NSDictionary. you can just initialize new dictionary
//as a replacement of the object in case you dont want your dictionary to be mutable
NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:#"cambridge",#"text",#"yourOtherData",#"otherData" nil];
[yourMutableArrayDataSource replaceObjectAtIndex:indexPath.row withObject:tempDict];
}
as mentioned by Maulik (thank you), the lblName text will change back to its original text when the cell scrolls. you might want to update your data source to keep the new data. **answer edited
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.lblName.text=#"New text Here";
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.lblName.text=#"cambridge";
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
By above code of janusfidel , label's text will be changed. But when you scroll the table , I guess value will be changed to original value.
You also need to update your data source(that is your array ) when you want to change original data.
I want to highlit cell when user click on the cell in the function didselectrowatindexpath.?
You have two options:
modify the cell directly in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method (but I've had some visual glitch issues with this technique, Problem modifying UITableViewCell accessoryType in tableView:didSelectRowAtIndexPath:):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// do stuff with cell
//
}
set appropriate state in your data model, and call [tableView reloadData], which in turn will result in a call to - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, where you set up the cell to display according to your data model.
I've put in this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * tableCell = [self.tableView cellForRowAtIndexPath:indexPath];
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
}
But for some reason, the checkmark doesn't appear. Could someone help?
Actaully, I figured it out, that's the right way to do it, its just it had a black bg so it was hard to see.