TableViewCell checkmarks not disappearing when tapped? - ios

Here is my method in my delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
PFRelation *friendsRelation = [self.currentUser relationforKey:#"friendsRelation"];
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryNone;
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user];
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.friends addObject:user];
[friendsRelation addObject:user];
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog (#"Error %# %#", error, [error userInfo]);
}
}];
}
When I run the app, everything works just fine with no errors or warnings but when I tap on friends to remove the checkmark by their name it does not happen.

This code syntax might help you check that it's working. Then you'll probably see as Matthias said that the issue is coming from your isFriend: method because your code looks fine for me.
Or another way, I think it's cleaner to separate the two logics of selecting/deselecting, so don't forget to enable the "multiple selection" on the tableview :
self.tableView.allowsMultipleSelection = YES;
And on the tableview delegate :
- (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;
}
Then to retrieve all selected items :
- (void)handleSelectedItems
{
NSArray *selectedItemIndexPaths = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *indexPath in selectedItemIndexPaths) {
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
// do what you want
// ...
}
}

Related

ImageView is zooming in on table row deletion Part II

I have the exact same problem as the question posted here.
Which looks exactly like this gif.
I'm not which method to begin troubleshooting the problem. I've sub-classed the UICell and have used the standard UICell, both with the same results. I've also tried to set the image in the cell to nil when it edit mode: cell.imageView.image = nil;
Any thoughts would be appreciated, I can post more code if needed...
// Cell For Row At Index Path
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Item cell func");
static NSString *CellIdentifier = #"detailCell";
MCTableViewCell *cell = nil;
if (cell == nil)
{
cell = [[MCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Item * record = [_itemsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageWithData:record.photo];
cell.textLabel.text = [NSString stringWithFormat:#" %#", record.itemName];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
//cell.shouldIndentWhileEditing = NO;
NSLog(#"Detail Record.Name %#", [_itemsArray objectAtIndex:indexPath.row]);
return cell;
}
// Editing Cells
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Begin editStyle Func");
if (editingStyle == UITableViewCellEditingStyleDelete) {
//UITableViewCellEditingStyleNone
// 1
[tableView beginUpdates];
// Delete the row from the data source
NSLog(#"after Updates editStyle Func");
// Delete item from Table
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"after Row Animation editStyle Func");
// Delete Item from Database
[self.managedObjectContext deleteObject:[self.itemsArray objectAtIndex:indexPath.row]];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Save: %#", [error localizedDescription]);
}
[self showObjects];
NSLog(#"End of if stmnt");
// 5
[tableView endUpdates];
}
NSLog(#"End of Function");
}
This problem seems to happen in ios7.
in ios8, works fine. so, this might be a bug.
To work around, add your own UIImageView to cell and use it instead of UITableVewCell's imageView.

checkmark does not appear on first cell

i need to add checkmark when we tap on each cell, and it must show when i return back to tableview.
I tried with this code but its not working,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (self.thread == nil) {
cell.textLabel.text = #"Loading, please wait...";
} else if ([self.thread count] == 0) {
cell.textLabel.text = #"No Results!";
} else {
//
NSDictionary *msg = [self.thread objectAtIndex:indexPath.row];
NSLog(#"yourMutableDictionary - %#",msg);
if (indexPath.row == 0) {
cell.textLabel.text = [thread objectAtIndex:0];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"%# ", [msg objectForKey:#"id"]];
if ([self.idSelected count] != 0) {
if ([self.idSelected containsObject:[NSString stringWithFormat:#"%#", [msg objectForKey:#"id"]]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
}
return cell;
}
#pragma mark - Table view delegate
//In a xib-based application, navigation from a table can be handled in - tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
{
if (self.thread == nil || self.thread.count == 0) {
return;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (path.row == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;// this checkmark not working
}
else {
NSDictionary *user = [self.thread objectAtIndex:path.row];
NSString *uName = [NSString stringWithFormat:#"%# %#", [user objectForKey:#"first_name"], [user objectForKey:#"last_name"]];
NSString *uId = [NSString stringWithFormat:#"%#",[user objectForKey:#"id"]];
NSLog(#" click id%#", uId);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[userNames removeObject:uName];
[userIds removeObject:uId ];
cell.accessoryType = UITableViewCellAccessoryNone; // this check mark is working perfectly
} else {
[userNames addObject:uName];
[userIds addObject:uId];
cell.accessoryType = UITableViewCellAccessoryCheckmark; // this check mark is working perfectly
}
}
}
first cell check mark is not working but others working perfectly. I want to add check mark on first cell too, how its possible
Use below code to display checkmark on multiple table view cell :
- (void)viewDidLoad
{
[super viewDidLoad];
self.arraySelectedCell = [NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.arraySelectedCell containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// do here..
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//the below code will allow multiple selection
if ([self.arraySelectedCell containsObject:indexPath])
{
[self.arraySelectedCell removeObject:indexPath];
}
else
{
[self.arraySelectedCell addObject:indexPath];
}
[tableView reloadData];
}
happy coding!

Checkmarks not working in a tableview

In my iOS app, I have a tableView and when you click on the cell, a checkmark shows up. When you click on the cell again though, the checkmark doesn't do away. I have checked my code over and over again. These are the two methods that have to do with UITableViewAccessoryCheckmark:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFRelation *friendsRelation = [self.currentUser relationforKey:#"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryNone;
for(PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user];
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.friends addObject:user];
[friendsRelation addObject:user];
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
Can someone please post the correct code? Thank you!
Try setting the accessory view property on UITableViewCell to nil.
cell.accessoryView = nil;

Search and Checkmarks in tableViews

I am trying to make a search bar in iOS and have made it to where it filters results and then when you click on it, it shows a checkmark. When I delete the search text, the check mark goes away and the full page of cells appears but the cell that I selected in the search is not selected with a check mark. Can someone please fix the code below to help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableSet *selectedUsers = [NSMutableSet set];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user;
if (self.userResults != nil)
{
user = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user = [self.allUsers objectAtIndex:indexPath.row];
}
cell.textLabel.text = user.username;
if ([selectedUsers containsObject:user])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
if ([selectedUsers containsObject:user])
{
// user already selected
}
else
{
// select user
[selectedUsers addObject:user];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
PFRelation *friendsRelation = [self.currentUser relationforKey:#"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
} }];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSMutableSet *selectedUsers = [NSMutableSet set];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user;
if (self.userResults != nil)
{
user = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user = [self.allUsers objectAtIndex:indexPath.row];
}
cell.textLabel.text = user.username;
if ([selectedUsers containsObject:user])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFRelation *friendsRelation = [self.currentUser relationforKey:#"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
} }];
PFUser *user1;
if (self.userResults != nil)
{
user1 = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user1 = [self.allUsers objectAtIndex:indexPath.row];
}
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedUsers removeObject:user1];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedUsers addObject:user1];
}
}
See if this helps.
The thing is that when you close the search bar the table view is reloaded.this time you have to record all the cells index path that user select.That is you do it in didSelectRowAtIndexPath delegate Method,and save in an array value that user select.
and in cellForRowAtIndexPath you can check all the cell whose indexPath in in the array.
Hope you got it...
NSMutableArray *array=[[NSMutableArray alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if
([selectedBillsArray containsObject:indexPath]) {
cell.accessoryType=UITableViewCellAccessoryCheckmark; }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
[selectedBillsArray removeObject:[NSString stringWithFormat:#"%i",indexPath];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedBillsArray addObject:[NSString stringWithFormat:#"%i",indexPath];
}
}
Do something like this...

Getting the Row for Section indexPath in a tableView

Recently I put implemented an indexing feature for the user names in my app. I have been trying to access the rows in each section and add them to my recipients array when the user taps the index path. I have tried passing many different values in my objectAtIndex: method but none seem to be working. I am able to log the correct index row and section in indexPathForRow:inSection: method but the return value is an indexPath and not an integer. So something with my objectForIndex: method is obviously not right. Any help or references would be super. cheers!
This line:
PFUser *user = [self.friends objectAtIndex:indexPath.section];
Returns only the first username in each section, no matter what name I tap in the section. So I need to access the Row in addition, not just the section.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
int section = indexPath.section;
int row = indexPath.row;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSLog(#"newIndexPath: %#", newIndexPath);
PFUser *user = [self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(#"added:%#",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(#"removed:%#",user);
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
}
}];
}
Hi this is enough try and let me know if you face any issues...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray * nameArray = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = (PFUser*)[nameArray objectAtIndex:indexPath.row];
// PFUser *user = (PFUser*)[self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(#"added:%#",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(#"removed:%#",user);
}
// this is enough
}
Just like the code Spynet suggested only a little bit different:
NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = [array objectAtIndex:indexPath.row];
Using that code I was able to get the proper indexPath for row in each section. Also, in order to get the cells accessory checkmarks to work correctly I had to create a newIndexPath using the section and row values for each index. My final code is here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
int section = indexPath.section;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSLog(#"newIndexPath: %#", newIndexPath);
[self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
NSLog(#"sectionsArray:%#",self.sectionsArray);
NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = [array objectAtIndex:indexPath.row];
NSLog(#"array:%#",array);
NSLog(#"user:%#",user);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user];
NSLog(#"recipients:%#",self.recipients);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user];
NSLog(#"recipients:%#",self.recipients);
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
}
}];
}

Resources