Why are checkmarks not being displayed when UITableView.allowsMultipleSelection is enabled? - objective-c

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

Related

Implementing a search bar?

I have a project with a tableView created on the storyboard. It's pretty simple and I was following a tutorial to do it, my view controller looks like this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = restaurantDisplayNames[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES
];
}
So when you tap on a cell it puts a check mark next to it. Now I want people to be able to search for things. Problem is, Apple changed the search bar for iOS 8 and supposedly made it simpler but I can't find any tutorials on the UISearchController which replaced the depricated method.
So I dragged and dropped a Search Bar and Search Display Controller into my view controller and added the UISearchControllerDelegate, UISearchResultsUpdating> protocol declarations, but I get a crash:
'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Whenever I tap on the search bar.
I also have the method
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
}
But it is empty because I don't know what to put in there. Supposedly it is very easy and only requires a few lines of code to get it up and running, but the one tutorial I found here: http://www.stuartbreckenridge.com/blog/examining-the-new-uisearchcontroller-api now only is in swift but doesn't explain what goes in that method.
I figured it out. Man, some of these things are so ridiculously simple yet you have to dig deep to find answers. You'd think Apple could put some tips in XCode about these things, little warnings or something, but here's how I fixed it:
Apparently you need to add
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
In the cellForRowAtIndexPath: method so it looks like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = restaurantDisplayNames[indexPath.row];
return cell;
}
So it generates the cell if it doesn't exist. Not sure why, but oh well ...

Hide the circle on UITableView with multiple selection

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);
}

UITableViewCell didSelectRowAtIndexPath. Deselect the "not selected" cells when I select a new one

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

Why won't UITableViewCellAccessoryCheckMark Go Away?

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.

How to add a checkmark in a UITableViewCell

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