Detect tap outside tableview cells to perform action - ios

I am new to iOS development, i've been following apple intro to iOS and implemented the to-do list app.
I have tableview that shows the list of the current to-do list, and another view controller that allows the user to add a new item in a textfield then add to the list.
I have a little + on top of my tableview that performs a segue action to the add to-do item view.
I want to be able to perform the same action if the user taps on an empty cell
I tried to put this code in and it works but I want to put it in the right position so it only triggers when the user hits empty space in the table:
[self performSegueWithIdentifier:#"showAddItem" sender:self];
Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
ABCTodoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}

In my opinion this solution is the cleanest:
Add a tap gesture to your UITableView's backgroundView. If the backgroundView is nil, add a transparent UIView, and add a gesture to it.
Now hook up your action to this gesture recognizer.

I can see at least two solutions here.
Solution 1. Adding en empty cell by modifying the data source
After you have populated your toDoItems add this:
[self.toDoItems addObject:#""];
Notice that toDoItems should be type of NSMutableArray.
Now let's handle creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
NSObject *objItem = [self.toDoItems objectAtIndex:indexPath.row];
if([objItem isKindOfClass: [ABCTodoItem class]]) {
ABCTodoItem *toDoItem = (ABCTodoItem*) objItem;
cell.textLabel.text = toDoItem.itemName;
}
else if([objItem isKindOfClass: [NSString class]]) {
NSString *item = (NSString*) objItem;
cell.textLabel.text = item;
}
return cell;
}
Touch action:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSObject *objItem = [self.toDoItems objectAtIndex:indexPath.row];
if([objItem isKindOfClass: [ABCTodoItem class]]) {
ABCTodoItem *toDoItem = (ABCTodoItem*) objItem;
// Open your toDoItem
}
else if([objItem isKindOfClass: [NSString class]]) {
self performSegueWithIdentifier:#"showAddItem" sender:self];
}
}
Solution 2. Adding an empty cell without modifying the data source
This makes it possible to create one extra cell:
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
return [self.toDoItems count] + 1;
}
Now again let's handle creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if([indexPath.row < [self.toDoItems count]) {
NSObject *objItem = [self.toDoItems objectAtIndex:indexPath.row];
if([objItem isKindOfClass: [ABCTodoItem class]]) {
ABCTodoItem *toDoItem = (ABCTodoItem*) objItem;
cell.textLabel.text = toDoItem.itemName;
}
}
else {
cell.textLabel.text = #"";
}
return cell;
}
And then handling the touch action:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath.row < [self.toDoItems count]) {
NSObject *objItem = [self.toDoItems objectAtIndex:indexPath.row];
if([objItem isKindOfClass: [ABCTodoItem class]]) {
ABCTodoItem *toDoItem = (ABCTodoItem*) objItem;
// Open your toDoItem
}
}
else {
self performSegueWithIdentifier:#"showAddItem" sender:self];
}
}

I hope I have understood your question correctly; correct me if I am wrong. You have a to-do List populated in a TableViewController which has some tasks and one (or more) empty cells. When you click on these cells, you would want to call [self performSegueWithIdentifier:#"showAddItem" sender:self]; a method that presents a ViewController that allows you to add more items into the list.
Once you have populated your TableViewController in such a way that you have an empty cell, you can always get the NSIndexPath of the empty cell.
When you tap on a cell, a method will be called, called: (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. As you can see, you will get the index information of the tapped cell. There, you can add the code like so:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Add your logic for the rest of the items
//If you have 7 items in the to-do list, you would have 8 cell,
//the eight one being empty. If indexPath.row is equal to 8 then,
//it says that it MUST be the last item, that is empty
if(indexPath.row == self.itemsInTheList.count) {
[self performSegueWithIdentifier:#"showAddItem" sender:self];
}
}
Alternatively, you can set the tag property of the cell; and that's the story for a different day.

Related

Can I execute didSelectRowAtIndexPath only for specific cell identifier?

I'm using two different custom table cells in my table view. That said, I only want didSelectRowAtIndexPath to fire when, for example, Custom Cell Identifier 2 is tapped (but not when Custom Cell Identifer 1 is tapped). How might I go about this? See code below. Right now, didSelectRowAtIndexPath fires when either cell is tapped...
- (void)viewDidLoad {
[super viewDidLoad];
static NSString *ChatTableIdentifier = #"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = #"SwapDetailTableViewCell";
UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];
UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = self.messages[indexPath.row];
id swaptime = data[#"swaptime"];
if ([swaptime isKindOfClass:[NSString class]]) {
static NSString *ChatTableIdentifier2 = #"SwapDetailTableViewCell";
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];
NSString *time = data[#"swaptime"];
cell.startTime.text = time;
NSString *timeEnd = data[#"endswaptime"];
cell.endTime.text = timeEnd;
return cell;
} else {
static NSString *ChatTableIdentifier = #"ChatTableViewCell";
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
NSString *userName = data[#"name"];
cell.sendingUser.text = userName;
NSString *messageBody = data[#"body"];
cell.messageDisplayed.text = messageBody;
NSString *timeReceived = data[#"published at"];
cell.timeStamp.text = timeReceived;
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SwapDetailsViewController *detailViewController = [[SwapDetailsViewController alloc]
initWithNibName:#"SwapDetailsViewController" bundle:nil];
detailViewController.swapDetails = [self.messages objectAtIndex:indexPath.row];
[self presentViewController:detailViewController animated:YES completion:nil];
}
You can put an if condition inside didSelectRowAtIndexPath as the one you put in cellForRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *data = self.messages[indexPath.row];
id swaptime = data[#"swaptime"];
//perform action based on this, and don't do anything in second case
if ([swaptime isKindOfClass:[NSString class]])
//this indexPath will always contain cell of one kind
}else{
// this indexPath will contain second cell kind
}
}
Or alternatively you can also do-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// this will give you cell at the selected indexPath
id cell = [tableView cellForRowAtIndexPath:indexPath]
//perform action based on this, and don't do anything in second case
if ([cell isKindOfClass:[SwapDetailTableViewCell class]])
}else{
}
}
If you don't want a particular row to be selected, the proper solution is to implement the tableView:willSelectRowAtIndexPath: delegate method and return nil for rows you don't want selected.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (some condition) {
return nil;
} else {
return indexPath;
}
}
So now you need to determine the proper condition. I can think of three ways in your case:
Use the same if condition you have in cellForRowAtIndexPath:.
Get the row's cell and look at the cell's class.
Get the row's cell and look at the cell's reuse identifier.
Option 1 isn't ideal because if you change the logic in cellForRowAtIndexPath you have to remember to change it here too. Options 2 and 3 are essentially the same in this case. I'd go with option 3.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.reuseIdentifier isEqualToString: ChatTableIdentifier) {
return nil;
} else {
return indexPath;
}
}
Since you are now using ChatTableIdentifier in three places, I would move the lines:
static NSString *ChatTableIdentifier = #"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = #"SwapDetailTableViewCell";
to before the #implementation line and remove them from everywhere else in the file. No sense in recreating the same variables over and over.
In didSelectRowAtIndexPath you can call method
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for the indexPath of a current row and get the cell you are currently selecting. Every cell has a reuse identifier, so you can check the identifier of the cell you are selecting. if it equals "SwapDetailTableViewCell", then you can perform everything you need(for example move to another viewController)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.reuseIdentifier isEqual: #"SwapDetailTableViewCell"]) {
SwapDetailsViewController *detailViewController = [[SwapDetailsViewController alloc]
initWithNibName:#"SwapDetailsViewController" bundle:nil];
detailViewController.swapDetails = [self.messages objectAtIndex:indexPath.row];
[self presentViewController:detailViewController animated:YES completion:nil];
}

Get checkmark values of uitableview after clicking a button

I want to get the values of multiple selection of tableview on clicking a button.I checked lot of stackoverflow questions but unable to get the answer. I know this is already achieved by many people so i request someone to guide me about getting the values from tableview. I am able to select mulitple values from tableview.I am able to see checkmark.
Below is my code
.h file
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *specTableView;
#property (nonatomic, retain) NSArray *arForTable;
#property (nonatomic, retain) NSMutableArray *arForIPs;//to capture ids
- (IBAction)btnActionToGetValues:(id)sender;
.m file
- (void)viewDidLoad {
self.arForTable=[NSArray arrayWithObjects:#"value1",#"value2",#"value3",#"value4", nil];
self.arForIPs=[NSMutableArray array];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arForTable count];
}
- (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.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:indexPath]){
[self.arForIPs removeObject:indexPath];
} else {
[self.arForIPs addObject:indexPath];
}
[tableView reloadData];
}
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arForIPs);
}
I am getting this output
ids (
" {length = 2, path = 0 - 1}",
" {length = 2, path = 0 - 2}" )
My expected output is
ids (
"Value1",
"Value2" )
If any one has already worked on this please let me know.
try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[self.arForIPs removeObject:[self.arForTable objectAtIndex:indexPath.row]];
} else {
[self.arForIPs addObject:[self.arForTable objectAtIndex:indexPath.row]];
}
[tableView reloadData];
}
on your CellforRowatIndexpath
- (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.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
output you can get
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arForIPs);
}
I guess you can create one new array,example:
NSMutableArray *arr = [NSMutableArray array];
- (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.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[arr addObject:self.arForTable[indexPath.row]];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
[arr removeObject:self.arForTable[indexPath.row]];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(#"ids %#",arr);
}
What you are seeing in your log is the indexPath value of your cells. The length value is how many items are in the path (2) and the path value is showing the row and section of the cell the user tapped.
If you will use the index path value to get a reference to the cell; you can ask the cell if it has a checkmark turned on. You are also holding the values in your arForTable array. So, you use the index path from the tableView to ask the arForTable array for the value. I might write the didSelectRowAtIndexPath like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *valueFromArray = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueFromArray]){
[self.arForIPs removeObject:valueFromArray];
} else {
[self.arForIPs addObject:valueFromArray];
}
[tableView reloadData];
}
Of course, this will cause your cellForRowAtIndexPath to break. So you will need to update that as well. Perhaps replace this:
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
with something like:
NSString *valueString = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueString]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=valueString;
Often in a tableView you will find that the array (arForTable) holds the data and since the index property of the array matches the row property of the tableView we can use that match to query the array for the data that corresponds to the tableView row that we are displaying.

store selected cells textLabel text in an array

How do i store all selected cells text label text in a NSMutableArray?
How do i remove the correct cells text from the NSMutableArray when a cell is deselected?
What i have this far:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.allSelectedUsers containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(#"Yeeees");
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Configure the cell
cell.textLabel.text = object[#"username"];
return cell;
}
This is when i'm selecting a cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
}
else{
[self.allSelectedUsers addObject:indexPath];
}
NSLog(#"%d", self.allSelectedUsers);
[tableView reloadData];
}
When i check the log it doesn't display anything about the cells text label.
As I can't see how you're getting object instance, I suggest you to get back cell and read the title again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Lazy initialise array
if (!self.allSelectedUsers) {
self.allSelectedUsers = [NSMutableArray new];
}
// Take action
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
} else {
[self.allSelectedUsers addObject:indexPath];
}
[tableView reloadData];
// Logging all selected users
for (NSIndexPath *sIndexPath in self.allSelectedUsers) {
NSLog(#"%#", [tableView cellForRowAtIndexPath:sIndexPath].textLabel.text);
}
}
You are currently storing NSIndexPath objects, not NSString objects, so it's not exactly what your question is asking. With your PFTableViewController, you have access to the selector objectAtIndexPath:.
for (NSIndexPath *indexPath in self.allSelectedUsers) {
NSLog(#"%#", [self objectAtIndexPath:indexPath][#"username"]);
}
Note: You shouldn't be calling reloadData in your didSelectRowAtIndexPath: responder; change the accessory type for the cell instead.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
You should also implement didDeselectRowAtIndexPath: to know when a user deselects a row.

Not selecting the correct object in UI table View Cell

I have an array of user displayed in a table view when pushing the send button the cell dosen't selected right object. It can be quit random:). How do i make send the object displayed on the selected cell?
This is how i send my message
- (void)sendMessage:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
self.SendToUsername = [object objectForKey:#"username"];
self.SendToName = [object objectForKey:#"name"];
}
And this is my cell, where the send button is located.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = #"LampCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UIButton *sendbutton = (UIButton*) [cell viewWithTag:105];
[sendbutton addTarget:self action:#selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
It is quite easy. Tableview itself provide method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
variable=[array objectAtIndex:indexPath.row];
}
In "Variable" you have value which is selected. If your array is accessible in whole controller then you can save "indexPath.row" and use on click event of send button to fetch selected record.
tableView indexPathForSelectedCell will not give you the index path you are expecting in the action method of a button that is in a cell. The cell wasn't selected - you touched the button.
To get the index path of the row for that button, there are a couple of different methods.
My preferred method is to traverse the view hierarchy to find the cell that contains the button, and use that to get the index path. See this question for more info:
Button in custom cell in dynamic table: how to know which row in action method?
My answer from this question is as follows. You could add these two methods to a category on UITableViewController, or you could just add them to your view controller, if you like.
- (NSIndexPath *)indexPathForCellSubview:(UIView *)subview
{
if (subview) {
UITableViewCell *cell = [self tableViewCellForCellSubview:subview];
return [self.tableView indexPathForCell:cell];
}
return nil;
}
- (UITableViewCell *)tableViewCellForCellSubview:(UIView *)subview
{
if (subview) {
UIView *superView = subview.superview;
while (true) {
if (superView) {
if ([superView isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell *)superView;
}
superView = [superView superview];
} else {
return nil;
}
}
} else {
return nil;
}
}
You could then get the index path in your button action method like so:
NSIndexPath *indexPath = [self indexPathForCellSubview:sender];
You don't need to set the tag for buttons to get the indexpath. You can simply get it using this piece of code:
- (void)sendMessage:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForCell:clickedCell];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
self.SendToUsername = [object objectForKey:#"username"];
self.SendToName = [object objectForKey:#"name"];
}

UITableView checkmark imageview is set on multiple cellls

I have a table view that list currencies ... I would like that only one can be selected at one time (if the user clickes an already selected cell it should remain selected)... the check mark is an uiimageview...
Here is the code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.currencyList fetchedObjects] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CurrencyCell";
CurrencyCell *cell = (CurrencyCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Currency *currency = [self.currencyList objectAtIndexPath:indexPath];
cell.currencyName.text = currency.name;
cell.curencyAbreviation.text = currency.abreviation;
if ([currency.isDefault boolValue] == YES) {
cell.selectedCurrency.image = [UIImage imageNamed:#"Checkmark.png"];
checkedCell = indexPath;
NSLog(#"The default currency cellfor row is :%#",currency);
NSLog(#"the checkedcell index path is:%#",checkedCell);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [self.delegate theSelectedCurrencyIs:[self.currencyList objectAtIndexPath:indexPath]];
if (![indexPath isEqual:checkedCell]) {
CurrencyCell *cell = (CurrencyCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.selectedCurrency.image = [UIImage imageNamed:#"Checkmark.png"];
selectedCurrency = [self.currencyList objectAtIndexPath:indexPath];
NSLog(#"The selected currency is %#",selectedCurrency);
NSLog(#"The indexpath of the new selected currency is %#",indexPath);
CurrencyCell *previoslySelectedCell = (CurrencyCell *)[self.tableView cellForRowAtIndexPath:checkedCell];
NSLog(#"name of the previously selected cell %#",previoslySelectedCell.currencyName);
previoslySelectedCell.selectedCurrency.image = nil;
// [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,checkedCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView reloadData];
checkedCell = indexPath;
}
}
The problem is that the check-mark image is showing up in more than one selected cell.. the selected indexpath is ok... but a lot of cells look check-marked ... seems like a few on every scrolled screen
What would be the problem here?

Resources