Perform Single Selection of Row in UITableView with custom UITableViewCell ? - ios

I know there are lot of answers to this question . But it didn't achieved the desired result . didDEselectrow is not working . I have a UIImageView which i have set to hidden True in cellforRowAtIndex. And when someone selects any row value for hidden is set to false .
**My main problem is when i select another row , the previous row status does not change and it also shows selected . I have attached the necessary snippet of my code ,Please check and help me through this . **
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Event_Type" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell.selectionStatus)
[cell.selectEvent setHidden:NO];
else
[cell.selectEvent setHidden:YES];
[cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStatus == TRUE)
{
cell.selectionStatus = FALSE;
}
else
{
] cell.selectionStatus = TRUE;
}
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStatus == FALSE)
{
//Do your stuff
cell.selectionStatus = TRUE;
}
else
{
//Do your stuff
cell.selectionStatus = FALSE;
}
[tableView reloadData];
}

I suspect that didDeselect is not being called because you are allowing multiple selections in your tableView.
Have you set your table view's selection style to "single selection"? You can either do it in interface builder/storyboard or you can do it programmatically:
- (void) viewDidLoad {
self.tableView.allowsMultipleSelection = NO;
}
That way when you select another row it will deselect the old selection. Also you can simplify your code and make everything look better by not calling [tableView reloadData] every time one cell's selected state changes. The way I would suggest doing that is to change the hidden state of selectEvent within EventTypeTableViewCell whenever the cell's selection changes.
That leaves you with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Event_Type" forIndexPath:indexPath];
[cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]];
return cell;
}
And some method overrides within your definition of EventTypeTableViewCell:
#implementation EventTypeTableViewCell
/// Your code ///
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.selectEvent.hidden = !selected;
}
#end

Try this ::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

Related

UITableView : select row with checkmark but no highlight

Issue: I only want to show the selected cells with checkmark. I don't want the grey highlight.
I tried:
cell.selectionStyle = UITableViewCellSelectionStyleNone
but didn't work.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
cell.textLabel.text = [profile profileName];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.profileSelectionsTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
self.mobileProfileId = [profile.profileId stringValue];
[_continueButton setEnabled:YES];
}
How about this:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
The selectionStyle property should be enough to remove the highlight.
If you want to show the checkmark after the used has selected the row, you need to implement the tableView:didSelectRowAtIndexPath: method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView cellForRowAtIndexPath:visibleIndexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
In Swift, the easiest way is to deselect a cell without animation right after it has been selected:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
delete your this function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Try to use this your problem will be solved
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone
cell.textLabel.text = #"Checking Table View";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Go to Storyboard. Select your Table View Cell. Set Selection to None. Then add below codes:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
This thread looks a bit old, but I actually came across a decent solution for this recently. In case anyone stumbles across this thread, I figure I might as well post an answer. Especially since I spent hours trying to track this down.
I can't find my original sources anymore, but here's what I found out:
The highlight is made by a background view on the UITableViewCell on the selectedBackgroundView property. If we have a subclass of the table cell, we can detect the transition to the editing state with willTransitionToState: and set the background view to one that is clear/white instead of the blue/gray styles.
Here's what I ended up doing, and it works pretty well.
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
// To remove the blue/gray highlight on the table cell when selected,
// set the selection background to a clear view.
if (state & UITableViewCellStateShowingEditControlMask) {
// If we're moving to an edit state, set the custom background view.
UIView *bgView = [[UIView alloc] init];
[bgView setTintColor:[UIColor whiteColor]];
[bgView setBackgroundColor:[UIColor clearColor]];
[self setSelectedBackgroundView:bgView];
} else {
// Otherwise, just remove it completely.
// The system should handle the rest.
[self setSelectedBackgroundView:nil];
}
}
The plus side to this over the other solutions I've seen is that this one is quite minimal. No need for tracking instance variables or anything else.

Change image from custoum cell

I have a custoum cell with an image , and i want to change the image only when cell is selected.
This i was trying :
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (!tableView.tag==0) {
TableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell=[[TableViewCell2 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
// cell.cam.image = [UIImage imageNamed:#"cam_normal.png"];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected.png"];
}
}
but the image is not changing.
This is working for a normal cell.
Another try was this in cellForRowAtIndexPath
if (cell.selected==TRUE) {
cell.cam.image=[UIImage imageNamed:#"cam_selected.png"];
}
You already get the cell as a parameter but fetch another one when using dequeueReusableCellWithIdentifier!
Try something like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!tableView.tag==0)
{
TableViewCell2 *changeImageCell = (TableViewCell2*) cell;
changeImageCell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected.png"];
}
}
You have to do 2 things:
1. Change the image immediately when the cell is selected.
To do this you must first implement UITableViewCell delegate. But since you already have tableViewWillDisplayCel:forRowAtIndexPath:, I assume that you already have the delegate implemented.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected"];
}
You could change the image back when the user deselect the cell.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_unselected"];
}
2. Persist the selection when the cell is reused.
Again, when the cell is reused, you have to check whether it is selected or not. Then assign the image accordingly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView dequeReusableCellWithIdentifier:#"cell"];
BOOL isRowSelected = [indexPath isEqaul:[tableView indexPathForSelectedRow]];
if (isRowSelected) {
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected"];
} else {
cell.cam.hightlightedImage = [UIImage imageNamed:#"cam_unselected"];
}
}
Two things I notice here. First you never use true or TRUE in Objective-C. You use BOOL with YES and NO. Second, you rarely need to use dequeReusableCellWithIdentifier: outside tableView:cellForRowAtIndexPath:. Why did you do it there?

Multiple checkMark when row selected in UITableView IOS

I have a UITableView that displays checkmarks when a row is selected. The problem is that When i select a row in didSelectRowAtIndexPath and add a checkmark on the selected row it adds an additional checkmark. Here's my code
Any help would be very much appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName];
cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage];
//cell.detailTextLabel.text =#"Breve DescripciĆ³n de la categoria";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone;
[self.cellSelected removeObject:indexPath];
}else {
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[self.cellSelected addObject:indexPath];
}
[self checkMark];
[tableView reloadData];
}
- (void)checkMark{
for (NSIndexPath * indexPath in self.cellSelected) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
}
}
[self.tableView cellForRowAtIndexPath:indexPath] call in the didSelectRowAtIndexPath will not return the exact cell. It can be same cell, new cell or reused cell. If it is a reused cell at its accessory view has a checkmark, you will end up having two cells with checkmark.
Its better to store in the array and use it accordingly. If you are planning to have multiple selections, Use the code example below.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.cellSelected = [NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Cell Initialisation here
if ([self.cellSelected containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
//self.selectedIndexPath = indexPath;
//the below code will allow multiple selection
if ([self.cellSelected containsObject:indexPath])
{
[self.cellSelected removeObject:indexPath];
}
else
{
[self.cellSelected addObject:indexPath];
}
[tableView reloadData];
}
Try this:
Declare in your .m file:
#interface MyViewController () {
NSIndexPath *__selectedPath;
}
In tableView:cellForRowAtIndexPath: check if given indexPath is the same as stored in ivar:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//configure cell
if ([__selectedPath isEqual:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
In tableView:didSelectRowAtIndexPath store pointer to selected NSIndexPath or nil if cell has been deselect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
__selectedPath = indexPath;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
__selectedPath = nil;
}
}
I wrote it without checking in Xcode, so there could be some typos, but I show main idea.
In my opinion you shouldn't call [self checkMark]; in your tableView:cellForRowAtIndexPath: method.
Moreover if you want to have only one selected cell at a time, you should't create NSMutableArray to store NSIndexPath. It seems yours cellSelected stores 2 NSIndexPaths at a time, that why you have this strange behaviour.

imageview.hidden = false // for selected ^ true for not selected in UITableView

given below is my code to make image visible or not on multiple selection in uitableview but there is only 1 problem it doesnot work in realtime it works after scrolling.
i think i have mistake in picking cell of myTableView
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selected)
{
cell.imageView.hidden = false;
}
else{
cell.imageView.hidden = true;
}
writing this code in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
,
this is the exect solution of your problem
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[TbView deselectRowAtIndexPath:indexPath animated:YES];
if (!([selectedIndexPath containsObject:indexPath]))
{
[selectedIndexPath addObject:indexPath];
[[TbView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[selectedIndexPath removeObject:indexPath];
[[TbView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
}
Better to use
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden=!cell.imageView.hidden;
Try creating a subclass of UITableViewCell and overriding the setSelected method:
- (void)setSelected:(BOOL)selected
{
if (self.selected == selected)
return;
[super setSelected:selected];
if (selected)
{
self.imageView.hidden = NO;
}
else
{
self.imageView.hidden = YES;
}
}
P.S. don't forget to call setSelected on super as well.
Another option is use following two delegate method of UITableView.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method use for previous selected row/cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden = false;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method use for selcted row/cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden = true;
}
There is a much easier, simpler action than all this. Refresh table after selection has been made.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
//then reload table
[tableView reloadData];
}

Being able to tap on ONLY one item in a UITableView

So I have a subclassed UITableView that lists data. I need to make only one cell selectable.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// rows in section 0 should not be selectable
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == 3) {
cell.userInteractionEnabled = YES;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSLog(#"Hey whats up, address");
return indexPath;
}
else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
My code that does this so far works, but only after the cell is clicked at least once. Placing it in didSelectCell allows the cell to be selected if held down for 1-2 seconds.
You should do that in the cellForRowAtIndexPath. Like this:
-(UITableViewCell*)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// your code
if(indexPath.row != 3)
{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
{
}
And then on didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 3)
{
//do your stuff
{
}

Resources