How to simple change checkmark in TableView? - ios

In my application, in storyboard, I have a screen with a lot of Table View Cells.
For example, in first two of them, I need to move checkmarks, when user touches one of them (make a select).
Is there are an easy way to do it, for example, like outlet connection?
Or how it is possible to do?
Thanks.

Check out Apple's documentation on managing selections in UITableView. Basically, you'll coordinate the accessory views of the previously selected cell (remove the checkmark accessory) and currently selected cell (add the checkmark accessory) in your table view's didSelectRowAtIndexPath method.
Here's a rough implementation, assuming you have an array of available taskTypes, and a property for the currentTaskType selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSInteger taskTypeIndex = [taskTypes indexOfObject:[self currentTaskType]];
if ( taskTypeIndex == [indexPath row] )
{
return;
}
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:taskTypeIndex inSection:0];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if ( [newCell accessoryType] == UITableViewCellAccessoryNone )
{
[newCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[self setCurrentTaskType:[taskTypes objectAtIndex:[indexPath row]]];
}
if ( [oldCell accessoryType] == UITableViewCellAccessoryCheckmark )
{
[oldCell setAccessoryType:UITableViewCellAccessoryNone];
}
}

Related

Selecting one row is checking the mark of another row

I'm getting a weird problem with table view in iOS. In the table, when I click on a row, I want a checkmark to be displayed, it is working very fine with the code below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Right now, my table has 11 rows. When I click on the 1st row, the checkmark appears for the 1st row (this is good) but it also appears for the 10th row. When I click on the 2nd row, the 2nd(this is good) and the 11th appears.
Does anyone has an idea about this issue?
Do this...
self.tableView.allowsMultipleSelection = YES;
Then when you need to get all the selected rows...
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
This array will contain all the rows with the checkmarks on them.
No need to do anything in code yourself.
For the same thing what you are trying to do, I implemented it in following way -
Declare a NSIndexPath Variable in AppDelegate or in local file.
if (Appdelegate.selectedIndexPath == (id)[NSNull null]) {
NSLog(#"null");
Appdelegate.selectedWebsiteIndex=indexPath;
UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
UITableViewCell *cell1 = [aTableView cellForRowAtIndexPath: Appdelegate.selectedIndexPath];
cell1.accessoryType = UITableViewCellAccessoryNone;
Appdelegate.selectedIndexPath =indexPath;
UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Hope This helps you..

How to call a specific cell selection based on user interaction in UITableView

I have a tableview where the user taps on a couple of cells and depending on the tap, the image in that cell gets replaced by a highlighted image, to let the user know it has been selected.
One such cell is the USE CURRENT LOCATION cell. Where the second cell just below that is an alternative SELECT YOUR CITY cell which calls a UIPicker.
What I want is that if the user taps on the USE CURRENT LOCATION cell (the image is replaced with a highlighted image) but if the user then taps on the SELECT YOUR CITY cell to bring up the picker, I need that the first cell image be set back to normal state. This way Im telling the user, "USE CURRENT LOCATION" has been automatically disabled because you are selecting a city manually.
So I tried adding this line:
//Deselect Row 1
[tableView deselectRowAtIndexPath:1 animated:YES];
inside didSelectRowAtIndexPath for case 2 (because case 0 is a cell with an image)
In -tableView:cellForRowAtIndexPath: set the selectionStyle to UITableViewCellSelectionStyleNone Or you do it in interface builder (The goal is to avoid blue/ or gray selection)
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SomeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure your cell
return cell;
}
Then in -tableView:didSelectRowAtIndexPath: customize the behavior :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *useCurrentLocationCellIndexPath = [NSIndexPath indexPathForRow:1
inSection:0];
NSIndexPath *pickCityCellIndexPath = [NSIndexPath indexPathForRow:2
inSection:0];
if ([indexPath compare:useCurrentLocationCellIndexPath] == NSOrderedSame) {
// The "use current location" cell was selected. Change the image to the highlighted image
[tableView cellForRowAtIndexPath:indexPath].imageView.image = highlightedImage;
} else if ([indexPath compare:pickCityCellIndexPath] == NSOrderedSame) {
// The "pick city" cell was selected. Change the image to normal one. And show the picker using your code.
[tableView cellForRowAtIndexPath:useCurrentLocationCellIndexPath].imageView.image = normalImage;
//[self showCityPicker];
}
}
I ended up using:
//Deselect Row 1 - only highlights it
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:selectedCellIndexPath];

UITableViewCell using checkmark as a toggle switch

I want to use the Checkmark as an toggle switch to let user selects a bundle of items, but this is not working as expected - I have to tap the cell 2 times for the cell to toggle checkmark on/off. And if I do [tableView reloadData] instead of reloadRowsAtIndexPaths it does not work at all.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
Your code is changing the view property and then reloading the table view from your model data. You should be changing the model data instead of the view property before you do the reload.

How can I add multiple cell.textLabel.text in Array?

I have make multiple selection like below in UITableViewController using. This is delegate method of UIAlertView, in which there is UIAlertView contains Table.
Now How can I add particular selected indexPath.row to my array and remove if some one uncheck it.
- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"MY INDEX PATH IS %#", indexPath);
if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
[tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
I would be tempted to create a dictionary of your items used for the tableView. Then have a 'selected' bool property for each object in the dictionary. You can then simply turn this true or false and use [tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone]; when your alert view is tapped. You could then also enumerate through if you needed to know which objects have their rows selected.

iOS UITableViewCellAccessoryCheckmark Visible ob every scroll

I have a list which I have using as a check boxes. I have enable or disable Check mark on row on select. But when I scroll the list its make mark row after every 10 rows.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:indexPath];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
oldCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
UItableView reuses the cell in every scroll so using condition as per accessory type is not a good practice. You can Create an NSMutableArray with selected items and Check as per the Condition below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if ([selected containsIndex:indexPath.row]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Do the rest of your code
return cell;
}
in didSelectrowAtindexpath method you can Add and remove the Selected items.
Its because UITableView reuses the cell.
So, in the method cellForRowAtIndexPath, you will have to check for a particular cell (of a particular section and row), if it needs to be checked on, provide the accessory type.
If not needed for that cell, provide accessory type as none.
You need to put your logic to set accessory type for cell in cellForRowAtIndexPath, and to identify the cell to mark with check mark you can mark the object in the list in didSelectRowAtIndexPath: or manage an array of selected/unselected objects of the list here.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[NSMutableArray addObject:[AnotherMutableArray objectAtIndex:indexPath.row]];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[NSMutableArray removeObject:[AnotherMutableArray objectAtIndex:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
Also in your viewDidLoad, instantiate you both mutable arrays-
yourmutableArray1 = [[NSMutableArray alloc]init];
yourmutableArray2 = [[NSMutableArray alloc]init];

Resources