How do I add a checkmark to a table view? - iphone

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.

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.

Highlight table view cell on click, not on release

I have a table view with a list of items. When I click on one of those items I want the background to be highlighted. I have that code working but the colour changes on release, not on the click itself. How can I highlight when the user taps on the cell and not when he/she releases it?
Here is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.backgroundView=v;
cell.title.textColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor1"]];
}
I want it to happen like a button. Is there an onClick method for a UITableView or a UITableViewCell besides didSelectRowAtIndexPath?
EDIT
Here is my code from cellForRowAtIndexPath
-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView=v;
}
At the time of creating cell, in cellForRowAtIndexPath, write below code:
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView = v;
No need to write anything in didSelectRow method.
You can consider this as option
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView selectRowAtIndexPath:indexPath.row animated:YES scrollPosition:UITableViewScrollPositionTop];
}

How to highlight a row in a UITableView

The code below seems to have no effect. I want it to be highlighed in the same way it highlights when you tap on a row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.textLabel setHighlighted:YES];
return cell;
}
This line will handle repainting the cell, label and accessory for you:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
As others have noted, you can programmatically select a cell using this method:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
However, you should be careful not to call this method too soon.
The first time that the view controller containing said tableView is initialized, the earliest that you should call this method is within viewDidAppear:.
So, you should do something like this:
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // set to whatever you want to be selected first
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
If you try putting this call into viewDidLoad, viewWillAppear:, or any other early view lifecycle calls, it will likely not work or have odd results (such as scrolling to the correct position but not selecting the cell).
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection: 0];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
usually works
If you are using interface builder to build your interface, one thing to check is that you specified the view of the table view controller. If you don't actually specify the view, this selection message may go nowhere.
(you specify the view by selecting the table view controller in the interface builder inspector window and dragging the outlet for "view" to the table view you want to do the selection in).
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// do something here
}
Try this :)
You can useĀ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.textLabel setHighlighted:YES];
return cell;
}
You have two choices. Either use the selection of the table view:
[tableView selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionNone];
Or change the background color of the table cell in the table view delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = ...
}

AccessoryDetailDisclosureButton not deselecting after I call deselectRowAtIndexPath

In my tableview I have number of rows that have disclosure detail button accessory type, and when pressing the row or the accessory, a new view is pushed to a navigation controller. I call the deselectRowAtIndexPath at the end of the didSelectRowAtIndexPath function.
If I press the accessory button, I go to a new view, but it remains highlighted when I come back to this view (pop). The row is deselected as expected but not this. Any thoughts on how to 'unhighlight' it?
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}
For those who are still bothered by this and want a solution that appears to work right now, try this:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
You must reload the data FIRST and THEN select the row so that the highlight appears beneath your action sheet options.
Simply remove from your
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
method the statement
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
Since your table already allows selection, and you are correctly deselecting it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
you are done, because the row will be highlighted when selected pressing the disclosure button, and deselected when tableView:didSelectRowAtIndexPath: is executed.