Say I have a small tableview(no scroll) and when a user taps an option, it changes some setting in the app. I used this function:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//some code here
}
Now I want to use UITableViewCellAccessoryCheckmark for the option that is selected, I mean it should display a checkmark in the cell the user selected (and remove checkmark from previously selected option). How do I do that?
How about
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Just use the accessoryTypeproperty of a UITableViewCell.
#property(nonatomic) UITableViewCellAccessoryType accessoryType
Like this:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//code for reusing or init a new cell goes here
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Related
I have a UITableView that hast multiple selection enabled.
Some of my cells are not to be selected. For those I implement:
in the tableView:cellForRowAtIndexPath:
I set cell.selectionStyle = UITableViewCellSelectionStyleNone;
and in tableView:didSelectRowAtIndexPath:
I call [self.tableView deselectRowAtIndexPath:indexPath animated:NO];.
This works fine. My only caveat is that the little circle left of the cell still appears. It does not get checked when a user taps the cell, but I would like for it not to be shown, when a cell is "unselectable".
How can I hide those circles for certain cells?
Thanks
You have to implement the tableView:canEditRowAtIndexPath: method in your table view datasource. It will let you prevent the cells you want to be editable and thus the circles not to be shown.
Beware that setting cell.selectionStyle = UITableViewCellSelectionStyleNone; does NOT prevent the cell to be selected. It just removes any visual clue on a selected cell.
Try this...
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Subclassing UITableViewCell and adding following method works for me:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(false, animated: true);
}
The title says it, and I think it's pretty much a no-brainer but I can't find the answer.
I think the code describes what I try to do.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected Row");
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
static NSString *CellIdentifier = #"accountCell";
UITableViewCell *allCells = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
allCells.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
So - first should all cells have no checkmarks, then I wanna add a checkmark to the selected one.
Every time the didSelectRowAtIndexPath method is called, go through a for loop that resets all the cells to their original accessory type. Then update the cell with the selected index path with a check mark like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for (UITableViewCell *cell in[self.tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
See here for a possible solution. You don't need to iterate over all cells
Okay so I am slowly figuring this out. Just one more issue I am having. I am using a string and saying that if the string is equal to the cell text to put a checkmark on it when it loads the tableView.
Here is my code for that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([cell.textLabel.text isEqualToString:transferData]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
I am then telling it to remove that checkmark and add the checkmarks accordingly when being selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
cell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cellCheck = [tableView
cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
transferData = cellCheck.textLabel.text;
NSLog(#"%#", transferData);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:indexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
Everything works fine, except when it first loads. For some reason when I select on another cell, the checkmark that is originally loaded with the tableView won't go away. Why is this?
You are making a common mistake.
When selecting the cell, you are setting the state of the check mark directly. What you should be doing is setting the state of the checkmark in the data source and let the table cell configure itself from the data source.
Edited example for an exclusive checked table view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *changedIndexPaths = nil;
NSIndexPath *currentCheckedIndexPath = [self indexPathOfCurrentCheckedObject];
if (currentCheckedIndexPath && ![currentCheckedIndexPath isEqual:indexPath]) {
// There is currently a checked index path - unselect the data source and
// add it to the changed index array.
[[self.tableData objectAtIndex:currentCheckedIndexPath.row] setChecked:NO];
changedIndexPaths = #[indexPath, currentCheckedIndexPath];
} else{
changedIndexPaths = #[indexPath];
}
[[self.tableData objectAtIndex:indexPath.row] setChecked:YES];
[self.tableView reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationNone];
}
I have a new sample app you can download to see the whole project:
You need:
if (self.selectedPath && [indexPath isEqual:self.selectedPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
Cells get reused. If you conditionally set any cell attribute, you must always have the 'else' part to reset the attribute.
Edit: With the above change in your cellForRowAtIndexPath: method, do the following in your didSelectRowAtIndexPath: method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *oldSelection = self.selectedPath;
if (self.selectedPath) {
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.selectedPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
self.selectedPath = nil;
}
if (oldSelection == nil || ![indexPath isEqual:oldSelection]) {
UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath];
}
And get rid of the didDeselectRowAtIndexPath: method.
And of course you need the selectedPath property of type NSIndexPath *.
This code lets you pick 0 or 1 row.
I have a very simple tableview in a xib file with it's delegate and datasource hooked up to TestVC. TestVC is simple:
#import "TestVC.h"
#implementation TestVC
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDEN = #"IDEN";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:IDEN];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDEN];
}
cell.textLabel.text = #"test";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (void)tableView:(UITableView *)_tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"called");
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
When I click on a cell, it doesn't get deselected. I even put a log statement there to see if didDeselectRowAtIndexPath method is being called and it is.
What am i doing wrong?
You are using the wrong method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is the one you need
I have a UITableView in an iOS5.1 app where I set
self.tableView.allowsMultipleSelection=YES;
The Apple documentation states "When the value of this property is YES, a check mark is placed next to each row that is tapped. Tapping the row again removes the check mark.".
I am able to select multiple rows as the background is set to Blue. However, no checkmarks are displayed. Does the checkmark need to be set as shown below in didSelectRowAtIndexPath because I am using custom UITableViewCells?
cell.accessoryType = UITableViewCellAccessoryCheckmark;
I do the checkmarks manually in my uitableviewcell subclasses. You are going to have to do the UITableViewCellAccessoryCheckmark manually in didSelectRowAtIndexPath and keep a track of which one is selected. I would recommend something like so:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableview cellAtIndex:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}
note: I did not test this, but should give you the basic idea. Let me know if you have any questions. Did you try using a default uitableviewcell and see if it did the checkmark? I would not think a subclass would have a problem, as long as you are not modifying in the subclass.
Another option is to use tableView:didDeselectRowAtIndexPath: in addition to tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}