Tableview cell change cell backgroung image(png) on click - iphone

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.

Related

Need to enable UITextField in row of UITableView when cell is selected in iOS

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.

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 iPhone tableview cell label text on didSelectRowAtIndexPath

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.

update title of selected row in Uitableview

i'm using the following code to get the selected cell in the UiTableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Mofify the cell here
}
when i change the cell text or color it does not make any change in the actual UitableView
I would suggest that you reload the cell. Use:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
You should keep track of which cell was selected, and update the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I don't know why the text isn't updating for you when you change it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but even if you get it to work, it will revert if you scroll that cell off the screen.
UITableView is a subclass of UIView and UIView has this method to force it's refresh
- (void)setNeedsDisplay
But if you don't take that change into account in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you may never see the change.
UITableViewCell has the -titleLabel method which returns a UILabel. Then you might use the setText: and -setColor: methods.
Try doing:
Set the backgroundColor property of the UITableViewCell.
Set the backgroundColor property of the UILabel (may not do what you need).
Set the backgroundView property if the UITableViewCell to a custom class you make by subclassing UIView, where you create a label and color.
Also, you can't just change the cell color, you need to have a data source from which cellForRowAtIndexPath: reads, which sets the cell color there. This way, the color change will persist.

iphone - a reference to a cell

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];