iOS UITableViewCell accessory on the left side? - ios

For my app, I want a number of cells that can have both a checkmark and a detail disclosure button. That is, I want them to appear exactly like the Wi-Fi network selection in the iOS settings: checkmark on the left side, content in the middle, and detail disclosure button on the right.
Is there a proper way to do this, or am I supposed to just use an image of a checkmark in the image part of the cell content? If you know of any sample code doing this sort of thing, please point me to it.

The standard styles for UITableViewCell do not include one that works as you want, so you'll have to do this yourself by manually adding a subview to the cell.
You could use the example here as a starting point.

You can achieve it by simply prepending a couple of unicode symbols:
Put checkmark in the left side of UITableViewCell

Why don't you add a image to the standard imageView of the cell then a tapGesture on it ?
Here is how I did in willDisplayCell:
//Reset the cell
cell.imageView.image=nil;
cell.imageView.userInteractionEnabled=NO;
for(UIGestureRecognizer *recognizer in cell.imageView.gestureRecognizers)
[cell.imageView removeGestureRecognizer:recognizer];
//Add the gesture
cell.imageView.userInteractionEnabled=YES;
cell.imageView.image=[UIImage imageNamed:#"myImage"];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(geolocationAction:)];
[cell.imageView addGestureRecognizer:tapGestureRecognizer];

Related

Show Selection Highlight on UITableView Section Header When Touched

By default on a UITableView, when you touch a cell, it goes into selected mode, and is highlighted grey.
In our app, you can touch a header and it goes on to another page.
We want users to get the same kind of visual feedback when they touch a section, as when they they touch a cell. That is, it responds by going into a "selected" mode and making the background go a bit darker to indicate selection.
This does not occur when you touch a UITableView section. The section does not go into selection mode. It does not turn grey.
We've tried putting in a transparent button that spans a custom UIView with a background image and enabling "Reverses on Highlight", but the result is not very responsive and sucks.
What's the best strategy for implementing such functionality?
I would suggest subclassing UIGestureRecognizer to get the touchDown event, as pointed out in this post: Touch Down Gesture
You can then add the gesture recognizer to your UITableViewHeaderFooterView in viewForHeaderInSection and when it fires, grab the index path of the "selected cell." It will be null for the first section for some reason, but will have the correct indexPath.section for all other sections:
CGPoint point = [touchDown locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
Then just change the background color of your header cell. You'll probably want to wipe out the color on your header when tapping another header or cell or whatever, so hold on to the cell whenever you change its background:
// declare an instance UITableViewHeaderFooterView earlier
if(header)
[[header contentView] setBackgroundColor:[UIColor lightGrayColor]]; // or whatever your default is
if(indexPath)
{
header = [self.tableView headerViewForSection:indexPath.section];
[[[self.tableView headerViewForSection:indexPath.section] contentView] setBackgroundColor:[UIColor greenColor]]; // or whatever color you want
}
else
{
header = [self.tableView headerViewForSection:0];
[[[self.tableView headerViewForSection:0] contentView] setBackgroundColor:[UIColor greenColor]];
}

UITableViewCell subviews not moving when editing enabled in ios 7

I have a tableview with my custom class for UITableViewCell, I have added some subviews to each of the UITableViewCell, now when I enable editing like this
[tableView_ setEditing:YES animated:YES];
a red delete button appears on left side of each cell and each cell is shifted towards right except the subviews which are added on the cell. This causes the button to appear on top of subviews. How to fix this? Thanks
UITableViewCell have a specific view for it's content, cell.contentView, make sure all of your addSubview calls are to this view not the cell. EX:
// good
UILabel* label = [[UILabel alloc] init];
[cell.contentView addSubView:label];
// bad
[cell addSubView:label];

disclosure indicator moves cell subviews

I have a table with some cells using a disclosure indicator and some not.
Obviously the disclosure indicator scrunches the contents so not all of the cells line up together in perfect columns.
How can I adjust my contents to stay in the same location regardless of there being a disclosure indicator or not?
One trick to solve this issue is to create an empty view and use it as the cell's accessoryView. Do this only for the cells that don't have a detail disclosure.
The trick is setting the size of the view properly. A little trial and error should do it.
In your cellForRowAtIndexPath:... method:
UITableViewCell *cell = ... // get the cell
If (/* there is no detail disclosure */) {
// Try different "width" value for this view to get the desired results
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
cell.accessoryView = view;
}
Another alternative is to use different cells. One with the disclosure indicator, and one without. You then have complete control over the layout. And by using the cell reloading mechanism rather than the table reloading mechanism this is efficient.
Better than using blank views IMHO.

IOS uitableview cell selected background

I have a UItableview in my IOS app with some information in it. I changed the Selected background color to clearcolor using the following code:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
There is text and 2 images in the cell, I've build this using CGRectMake.
But When I select and hold a table cell the the images disappear behind what looks like a white background.
As you can see I'm holding the "Dacnusa sibrica" cell, how can I fix this?
If you want to disable the blue selection of cells you can also set the cell's selection mode instead of modifying the background:
cell.selectionStyle = UITableViewCellSelectionStyleNone
This simply disables the blue selection when tapping the cell but still allows the cell to be selected (and thus handled by code).
With Auto layout it is possible to define Selection=None (default is gray) in the Interface Builder.
have you used [cell.contentView addSubview:image]?
use [tableView deselectRowAtIndexPath:indexPath animated:YES];
for deselecting the cell

How to detect which UILabel was pressed on custom UITableviewcell?

I have two labels with two seperate tags each one.
I would like to detect which one label was pressed by checking the tag.
Inside the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
i can retrieve one of them by code like this:
cell = [walltable cellForRowAtIndexPath:indexPath];
topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];
but i do not know the one that was pressed.
Is there a way to achieve to find which one label was pressed by the user?
Something important I want to point out: your reference to the label:
topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];
is not the correct, generic way to do this. I would recommend attaching the elements in your cell to an IBOutlet, and get the reference from there.
As for your question about UILabel touch events, I think a good way to achieve this is to add a UITapGestureRecognizer to your label, like so:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(firstLabelTapped)];
[firstLabel addGestureRecognizer:tgr];
[tgr release];
Do the same with the second label. If you want to pass back information to the TableView's view controller, do this with delegation. Good luck!
You can use touch events and label's tag as suggested in this question and answer.
Handling Touch Event in UILabel and hooking it up to an IBAction
One method, as suggested above, is to assign a tag to each label, then evaluate the tag of the calling UILabel in your callback.
Another approach if you're using a custom cell (i.e., a subclassed UITableViewCell, versus a standard UITableViewCell to which you've added custom content/layout) is to simply define each of the two labels as properties of your subclassed cell. If the labels are assigned as respective properties, you can evaluate those properties against the caller and determine which label was pressed.

Resources