change iPhone tableview cell label text on didSelectRowAtIndexPath - iphone

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.

Related

Change a label in a prototype cell upon button press

I have set up a tableview using custom cells as such:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{SWHNearYouCell *cell = (SWHNearYouCell *)[tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
SWHNearYou *aPointer = [self.thingsNearYou objectAtIndex:indexPath.row];
cell.customNearYouLabel.text = aPointer.restrauntsNearYou;
return cell;
}
I want to change the text of customNearYouLabel upon a button press but work out how to get a pointer to the cell in my -IBAction method.
Thanks
You can just handle that in your tableView:didSelectRowAtIndexPath: method by grabbing the cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.customNearYouLabel.text = #"New Text";
}
I presume the button is not on a table cell?
If so, you just need to update the relevant value in the self.thingsNearYou array.
If you then call [tableView reloadData] then the table will reload it's data and the text of customNearYouLabel will change.
Worked it out - needed to add in self before tableview
-(IBAction)cancel:(id)sender{
SWHNearYouCell *cell = (SWHNearYouCell *)[self.tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
cell.customNearYouLabel.text = #"New Text";
NSLog(#"This is it: %#",cell.customNearYouLabel.text);
}
I'll need to spend some more time on it as [self.tableView reloadData]; will not update the table but I reckon that should be easier to solve.

Change UITableViewCell.detailTextLabel Outside Of TableView Protocols

i have a tableView with 3 cells.
i want to change .detailTextLabel of that cells after tableView loaded.
how i can do that?
i triad this:
UITableViewCell *firstCell = [tableView cellForRowAtIndexPath:0];
firstCell.detailTextLabel.text = #"ABC";
[tableView reloadData];
but this is not doing nothing. what the problem?
instead of
UITableViewCell *firstCell = [tableView cellForRowAtIndexPath:0];
use
UITableViewCell *firstCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
Try this:
UITableViewCell *firstCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
firstCell.detailTextLabel.text = #"ABC";
no need to call reloadData.
when you say
[tableView reloadData];
that time your : -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
method will be called for three times as you have 3 cell in your row and this will again override your firstCell.detailTextLabel.text with the original text that you must be providing through your array...
so if you are using array.. then you need to change the object in that array for the required index.
just implement this:
[array replaceObjectAtIndex:0 withObject:#"Change"];
[tableViewCategory reloadData];
hope so this will work for you...

how to set accessoryType when cell has been selected

I have been trying to figure out how to set the accessoryType to UITableViewCellAccessoryCheckmark when the cell is selected but am having trouble finding a decent example of this.
If you know how to do this or a good tutorial could you please let me know that would be great.
To restrict the user to just one selection, meaning to create an exclusive list of one choice only, you could follow these steps;
Firstly, have a global index path declared in your .h file to keep track of the already selected cell ->
NSIndexPath *oldIndexPath;
When you create the cells, be sure to set the accessory type to none, so that no cell is selected by default when the table is seen;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CallIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Finally, in the didSelectRowAtIndexPath delegate method, add the following code which will remove the checkmark from the already selected cell, and add a checkmark to the newly selected one.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (oldIndexPath==nil) { // No selection made yet
oldIndexPath=indexPath;
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell
[formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell
oldIndexPath=indexPath;
}
}
Hope this works out! :)
Something like this may work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
To answer the comment below, just push a viewController in the same method like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
// Then push a new view
iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:#"iPhoneCustomViewController" bundle:nil];
[self.navigationController pushViewController:myVC animated:YES];
[myVC release];
// And deselect the row (if desired)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you know that:
1.) UITableView keeps track of the index paths for the rows that have been selected? It's in an array called indexPathsForSelectedRows
2.) UITableView has a flag you can set to make it either single or multiple selection. You can change it by calling the setter setAllowsMultipleSelection:(BOOL).
So, assuming that the table has been set to single selection, we can do the following in the tableView:CellForRowAtIndexPath method ...
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setText:#"Some Text"];
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if ([selectedIndexPaths containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;}
This implementation of CellForRowAtIndexPath will give you a clean checkmark with no gray background when a cell is selected. You will need to set the checkmark in the didSelectRowAtIndexPath delegate method to make sure a cell gets the checkmark the moment it gets selected.
No need to create separate ivars or anything else to keep track of what was or wasn't selected. It's all neatly contained in the UITableView as Apple intended.
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType=UITableViewCellAccessoryCheckmark;
Implement this in didSelectRowAtIndexPath delegate method
From the docs:
The delegate handles selections in this method. One of the things it
can do is exclusively assign the check-mark image
(UITableViewCellAccessoryCheckmark) to one row in a section
(radio-list style). This method isn’t called when the editing property
of the table is set to YES (that is, the table view is in editing
mode). See "Managing Selections" in Table View Programming Guide for
iOS for further information (and code examples) related to this
method.
Here is an example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark;
}

Tableview cell change cell backgroung image(png) on click

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.

How do you change the textLabel when UITableViewCell is selected?

I want to change the textLabel and detailTextLabel of a cell when it has been selected.
I've tried the following, but no change occurs:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyAppDelegate *appDelegate = (MyPhoneAppDelegate*)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = #"xxxxx";
cell.textLabel.text = #"zzzzz";
[tableView reloadData];
}
I agree, reloading the table view will actually dump and reload/display all the cells using tableView:cellForRowAtIndexPath: and use the original data, not the updated #"xxxxx" and #"yyyyy" in your tableView:didSelectRowAtIndexPath: method.
In a little test project I was able to change the labels upon selection with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = #"it was tapped";
}
You should not be trying to reload the table while a cell is selected. Instead, try
[cell setNeedsLayout]
after you make the above changes to the labels.
Also, is there a reason you're making a reference to the app delegate in the method?
Try to reload the cell you selected (described by indexPath) :
[yourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
Create a New iPad Project (Split View) and Now go through the Classes->Files. The easiest way's been given there. The XCode's Generated Codes.
Sample Code Lines :-
cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
You can use them in cellForRowAtIndexPath ||&& didSelectRowAtIndexPath ..
Not sure what you're trying to do with the delegate but you should try calling the tableView already instantiated; i.e. call
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
Maybe I'm not clear
What I'm saying is that you are instantiating a new empty table view
UITableViewCell *cell = [**tableView** cellForRowAtIndexPath: indexPath]; //cell has nothing it is new.
consider replacing to call the old
UITableViewCell *cell = [**self.tableView** cellForRowAtIndexPath: indexPath]; //now you have one that has a textField already in it
Did you try to refresh only the selected cell instead of reloading the whole table ?
[cell setNeedsDisplay];
instead of
[tableView reloadData];
This will have better performance and I'm not but I suppose that selection is lost if you refresh the whole table (this may be the reason why you don't see any change in the end)....