How to prevent TableViewCell from duplicating during recycle? - iphone

So my situation is pretty unique. I have a to-do list app with a bunch of tasks. Each task has a UITableViewCell. After each table view cell is tapped, it creates a view controller with the task at that row's index path's property. These view controllers are all stored in a NSDictionary. This is the code representation of what I just said:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC;
if (![self.detailViewsDictionary.allKeys containsObject:indexPath]){
detailVC = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
[self.detailViewsDictionary setObject:detailVC forKey:indexPath];
detailVC.context = self.managedObjectContext;
}else{
detailVC = self.detailViewsDictionary[indexPath];
}
Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailVC.testTask = task;
[[self navigationController] pushViewController:detailVC animated:YES];
NSLog(#"%#", self.detailViewsDictionary);
}
So this method of creating unique view controllers and storing them with a certain key almost always works. The problem arises when I delete or move the view controllers:
I was under the impression that a cell's index path gets recycled as you scroll down (dequeue). Doesn't that mean marking each cell with a number identifier would result in multiple cells for the same identifier?
Also, if you stored each view controller with a indexPath key, how do you make sure the key isn't set to two view controllers..? For example. Let's say you have 4 cells, which means 4 view controllers. You delete cell 3. Cell 4 moves down to cell 3s spot. You create a new cell which goes to spot 4. Now you have two controllers with the same indexPath key! How do you avoid this?? It's screwing up my app right now because tasks that have already been moved are loading their properties in the wrong view controller/cell!
I was suggested this to solve the problem before: "You maintain an NSMutableArray that "shadows" the contents of the table." However, I don't understand what this means/how to implement it.

You can use a technique we used to use on old databases. You store an NSInteger as a class var, and use that to assign a unique id to each of the cells as you create them. As you create each cell, you increment the unique id. Like this:
in your interface:
#property (nonatomic, assign) NSUInteger nextUniqueId;
then in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.tag = self.nextUniqueId++;
and then track those tags in your viewControllers. Just give them an assignable property, or customize the init to include the id.

Better to set the tag value for each row in cell for row at index path method.Store that tag value globally,and use that tag in did select row method.

Related

one timer per detail view controller

I have a view controller with a table view property and a detail view controller connected to each cell in the tableview via the navigation bar. In the detail view controller is a countdown timer, with an interval specified when the user creates the task. I am trying to make it so each cell (or task) has a unique detail view controller. I am using core data. This is what I have now:
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (!self.detailViewController) {
self.detailViewController =
[[DetailViewController alloc] initWithNibName:#"DetailViewController"
bundle:nil];
}
Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
self.detailViewController.testTask = task;
[[self navigationController] pushViewController:self.detailViewController
animated:YES];
}
DetailViewController.m
#interface DetailViewController : UIViewController <UIAlertViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *timeLeft;
#property (weak, nonatomic) IBOutlet UILabel *timerLabel;
#property (nonatomic, strong) Tasks *testTask;
#end
I feel like this is the correct way to implement the detail view controller because it minimizes the amount of memory that needs to be created, however it doesn't really suit my needs. Currently when a user taps a cell, and taps back, and taps a different cell, only the first cell's properties are loaded. Also, if I were to delete a cell there would be no way to invalidate its timer (i think) with this method. Any suggestions?
---edit---
I guess the question I should be asking is: How should I make it so that each Detail View has a decrementing label (that gets its information from a timer)?
Your best solution is to follow the MVC properly in this scenario. In your case you are storing data for each detailViewController you are creating (such as task and the countdown timer/interval etc).. and in rdelmar's answer he is suggesting that you store all the view controllers in a mutableArray. I disagree with both approaches as yours will have memory problems when you dismiss the view controller (as u've seen for yourself) and in rdelmar's case, you are storing viewControllers (along with the data they reference) in a mutable array.. which is wasteful.
think about it this way.. you want to keep track of the data in one place (that's unaffected with which view is on display right now.. it could be detailVC 1 or 100 or the VC with the tableView or whichever) and at the same time you want to allocate one detailVC at a time that simply displays whatever the data source tells it to display. That way you can scale your app (imagine what would happen if you had hundreds of indexes in your table.. will you store hundreds of view controllers? very expensive and redundant).
so simply create a singelton.. the singelton will have a mutableArray that stores the timers pertaining to each tapped table index and so on.. the singelton will also launch the timers every time a cell has been tapped and keep a reference to it (ie store the NSIndexPath), so that when you jump back and forth between detailVCs and the table.. the timers are still in operation as required by you (b/c they are referenced by the singelton). the DetailVc will simply ask the singelton for what it should display and display it.
hope this helps. Please let me know if you need any further clarification.
The trouble with your code is that you're only creating one instance of DetailViewController, so each cell is pushing to the same one. You have to have some way in didSelectRowAtIndexPath to look at the index path and use that to determine which instance of DetailViewController to go to. One way to do that would be to create a mutable dictionary to hold references to the instances of DetailViewController. You could have the keys be NSNumbers that correspond to the indexPath.row, and the value would be an instance of DetailViewController. So, your code might look something like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC;
if (![self.detailControllersDict.allKeys containsObject:#(indexPath.row)]) {
detailVC = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
[self.detailControllersDict setObject:detailVC forKey:#(indexPath.row)];
}else{
detailVC = self.detailControllersDict[#(indexPath.row)];
}
Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailVC.testTask = task;
[[self navigationController] pushViewController:detailVC animated:YES];
}
detailControllersDict is property pointing to an NSMutableDictionary.

UISegment Control in UITableView

I have added a segment control to my table view and what my problem/question is I need to display text in tableview cells based on segment selected.
I have followed some tutorials, in that they gave me text in labels based on segment selected. I used segmentControl.selectedIndexPath also. So can anyone tell me how can we set that array of objects to my tableview cells based on segment selected?
Correct me if any mistakes in my english.
Please help me.
Thanks a lot for help.
First of all you have to divide your data in different arrays depends on your requirement ie Number of segmentControl.
Here if there are three segment controls then create three array an in the table view's delegate methods depending on segment control's selected index change the array to display in table view.
Like if segmentControl.selectedIndex == 0 then array1 if == 1 then array2 and if == 2 then array3.
In all delegate and datasource methods of table view. And on segment control's selecedIndexChange: method call reload table.
Happy Coding :)
EDIT 1
For change data in table view on segmented control's index change you must have one IBOutlet for tableView and use that IBOutlet to change the data using [tableView reloadData]; here tableView is IBOutlet for table view.
Happy Coding :)
Common use for tableview;
In Controller you have an array which has the objects, lets say _tableObjects
You have implemented in your controller datasource methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =......
.................
cell.labelText.text = [_tableObjects objectAtIndex:indexPath.row].name;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableObjects.count;
}
So if it is like this, you can change _tableObjects array in somewhere in you code.
_tableObjects = myOtherArray;
[self.tableView reloadData];

Using a Table Cell's Text to Access Corresponding Object

Currently, I have a savedWorkout class that is simply a Table View populated with different exercises in each cell. My goal now is for the user to be able to click on each individual exercise, which will take you to a new view filled with detailed information about it.
For this, I have created an Exercise class that will hold the detailed information about the new object. Is this possible?
Here is some pseudo-code I have written up:
if (Table View Cell's Text == ExerciseObject.exerciseName) {
Populate a view with the corresponding information;
}
Being new to iPhone programming, I'm not exactly sure what would be the best way to do this, and this is what i'm thinking would be the best way to go about it.
My Exercise class holds an NSString to keep track of the exercise name, and three NSMutableArray's to hold different information.
Please let me know if I am going in the right direction.
EDIT:
After trying to implement my pseudo-code this is what I came up with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Exercise *exerciseView = [[Exercise alloc] initWithNibName:#"Exercise" bundle:nil]; //Makes new exercise object.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.
exerciseView.exerciseName.text = str;
[self presentModalViewController:exerciseView animated:YES];
}
However, this doesn't seem to work. When the new view is presented, the label doesn't show up (I connected the UILabel exerciseName to my desired string). Am I implementing this wrong?
Yes, of course it's possible. Just use the delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
and check your data source cell based on the index location.
You might need to post your cellForRowAtIndexPath method. If done traditionally, it uses the indexPath.row to access an array of exercises to get a particular exercise, then changes cell properties based on the particular exercise. Is that about right?
It so, then you're halfway home.
EDIT
1) Use the code inside your cellForRowAtIndex path to init your str, as indicated here.
2) The new view controller view hasn't been built yet. You can't initialize a subview in the view hierarchy before the VC is ready. You need to pass the string to a property in that view controller (in a custom init method if you want), then on that class's viewDidLoad, you can set the exerciseName field to the string property you saved earlier. That subview shouldn't be part of the classes public interface.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// There should be an array of exercises, the same one used in cellForRowAtIndexPath:
NSString *str = [self.myArrayOfExercises objectAtIndex:indexPath.row];
// Just made code up here, but however you get a string to place in the cell
// in cellForRowAtIndexPath do that same thing here.
Exercise *exerciseView = [[Exercise alloc] initWithNibName:#"Exercise" bundle:nil];
// Might be wise to rename this ExerciseViewController, since it's probably (hopefully) a ViewController subclass
// no need to get a table cell, you have the info you need from your exercise array
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.
exerciseView.exerciseName.text = str;
[self presentModalViewController:exerciseView animated:YES];
}

How to display a list of item in tableview when the table did select row at index method is called?

I have an application in which I have tableview in which I return 3 rows in the tableview. When I select a particular row in the table view another tableview should open which should display list of items and when I select a particular item the value of that particular item should be set to the textlabel of previous tableview.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView != secondTableview) {
self.secondTableviewArray =[[NSMutableArray alloc]initWithObjects:#"secondfirst",#"secondSecond",nil];
[secondTableview reloadData];
}
}
Pass the second controller (the one that will appear) a weak reference to the first just before showing it. Once the second view controller has appeared and the user has made the selection (you can catch that in (-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;), send a message to the first giving the value selected.
The first view controller, then can store the new value, and even dismiss/pop the second.
This can also be achieved, by writing your own delegate protocol, so the first conforms to it, and is the delegate for the second.
Take NSString in second view and use the property.from the first view to second view set the string and in second view use that String
If you want to open new table view on click of row then you make new xib file and call like as define in below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XMLBooksViewController * xmlBooksViewController = [[XMLBooksViewController alloc] initWithNibName:#"DetailView" bundle:nil];
XMLBooksAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
xmlBooksViewController.author = [appDelegate.authors objectAtIndex:indexPath.row];
[self.navigationController pushViewController:xmlBooksViewController animated:YES];
[xmlBooksViewController release];
}
In next file you define another table view, so the list or items comes as table view.

Why does reordering my tableView mess up my data?

I have two different table views in which I use the exact same code in tableView:MoveRowAtIndexPath:ToIndexPath: to support user reordering of the rows. One of these tableViews works perfectly. The other one, however, gets confused and starts displaying the same subview no matter which row is selected - i.e. its row indexing seems to have got messed up.
I've temporarily fixed this by adding a [tableView reloadData] at the end of theMoveRowAtIndexPath method, but I don't understand why it wasn't working in the first place - especially since another view with the exact same code works perfectly. Obviously, there must be another method in this view controller which is messing it up, but I don't know where to look.
Here is the code that is the same in both:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
userDrivenDataModelChange = YES;
NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];
NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:fromIndexPath];
[things removeObject:thing];
[things insertObject:thing atIndex:[toIndexPath row]];
int i = 0;
for (NSManagedObject *mo in things)
{
[mo setValue:[NSNumber numberWithInt:i++] forKey:#"displayOrder"];
}
[things release], things = nil;
[managedObjectContext save:nil];
userDrivenDataModelChange = NO;
}
(For what it's worth, the one that works is the child view of the one that doesn't, and they are in a to-many Core Data relationship).
Use a different identifier for each tableviews in
(UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
If you use the same identifier string, they will pickup each other's cached cell objects and show inconsistent data.
I forgot that I was using a custom cell on the parent view, and that the user presses a UIButton within the cell, rather than the cell itself. The custom cell has its index path set as a property by tableView:cellForRowAtIndexPath:, which of course never gets called again after the table cells are moved. Hence, I had to add a [tableView reloadData] to make sure the cells get updated.