Populate a tableView doesn't update - iphone

Here is my problem. I have a table view that is filled depending on the content of an array. Up to now, everything is fine. This array is used to store some maps name coming from a server. I have a refresh button so that when I click on it, it will update the array with the new maps name (refresh method).
When I click on the refresh button, the maps array is modified. Either some maps are added or deleted (if a user deleted a map or created a new map for example). However, this situation does not reflect on the screen.
Let's say I have 5 maps (named 1,2,3,4,5). If I delete one (let's say the map 3) and call refresh, the maps array (mapsModel->mapsNameList) will contain 4 maps and the content of this array is proper. However, on the iphone screen, I would see, in the table view, (1,2,4,5,5). I don't know why it doesn't remove a row if it is not in the maps array anymore.
I get the same problem if I try to add a map (let's say a map 0), I would get (0,1,2,3,4) and the number 5 would not be there.
If I restart the application, then all the maps appear properly...
Here is my code, if some variables name aren't clear of obvious, please let me know !
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int numberOfRows = [[MapsModel sharedMapsModel] numberOfMapsInSection:((UITabBarController*) self.parentViewController).tabBar.selectedItem.tag];
// Return the number of rows in the section. THIS FUNCTION WORKS
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MapsModel* mapsModel = [MapsModel sharedMapsModel];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:[[mapsModel->mapsNameList objectAtIndex:((UITabBarController*) self.parentViewController).tabBar.selectedItem.tag] objectAtIndex:indexPath.row]];
return cell;
}
-(void) refresh {
[[MapsModel sharedMapsModel] populateMapsNameListWithMapState:MAP_STATE_ALL];
[self updateView];
}
-(void) updateView {
//Reorder the maps by alphabetical order
NSSortDescriptor *sortDescriptor;
MapsModel* mapsModel = [MapsModel sharedMapsModel];
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[[mapsModel->mapsNameList objectAtIndex:self.tabBarItem.tag] sortUsingDescriptors:sortDescriptors];
//Update the table view
[self.tableView reloadData];
}

Are you sure that [[MapsModel sharedMapsModel] numberOfMapsInSection: returns correct value after update? Your table code has no problem, it seems that you should check your MapsModel.
Probably your mapsModel returns wrong values in cellForRowAtIndexPath: and numberOfRowsInSection:

I know the problem, I had a tab bar controller and three table views. However, I tried to use only one view controller for all the three table views... One view controller per table view resolved my problem !

Related

UITableViewCell background disappears?

The UITableViewController in my app pulls data from a json data source. I have also created a custom UITableViewCell background using CG. There is a very interesting bug that happens and I have no idea why. I will walk you through what happens and how I recreate it:
Tap to enter table view.
Without scrolling the table at all I immediately tap on an item in view.
After tapping on that item I press the back button to return to the table view.
If I then scroll down the first cell to appear from off screen will not have my custom back ground. It will just be the default for a cell. Then if I continue to scroll down every 10th cell will have the same issue.
This bug only occurs in this exact process. If I were to scroll the table view at all before tapping on an item it would not happen.
Here is the relevant code for the tableview controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Will remove all of the used codes from the table if setting is enabled
if (self.shouldHideCodes) {
NSMutableArray *tempArray = [self.jsonCodeData mutableCopy];
[tempArray removeObjectsInArray:[self.usedCodes usedCodes]];
self.jsonCodeData = tempArray;
}
return [self.jsonCodeData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (self.jsonCodeData) {
cell = [tableView dequeueReusableCellWithIdentifier:#"code cell"];
if ([cell isKindOfClass:[CodeCellTVC class]]) {
CodeCellTVC *tvcCell = (CodeCellTVC *)cell;
if (![tvcCell.backgroundView isKindOfClass:[CustomCellBackground class]]) {
tvcCell.backgroundView = [[CustomCellBackground alloc] init];
}
NSDictionary *codeDict = [self.jsonCodeData objectAtIndex:indexPath.row];
// Retrieve code string from dictionary
NSString *codeText = [NSString stringWithFormat:#"%#", [codeDict objectForKey:#"code"]];
tvcCell.codeTableLabel.text = codeText;
}
}
return cell;
}
The thing that confuses me is how it reacts. That when the bug happens every 10th cell has the issue and not every one. I don't have anything outside of these method's that deal with the tableviewcell itself.
I understood your problem, you did a wrong at the time of initializing the cell,Every time your intializing the cell, so that every time memory will allocate for that cell, it will create memory issue.
Edit the code like bellow it will work for you.
cell = [tableView dequeueReusableCellWithIdentifier:#"code cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"code cell"];
}

Take data from a static label and use it to populate tableView

I'm working on an application that has multiple views, taking data from one view and storing it in a table in another. I have labels that are updated with data when a calculate button and a button that is hopefully going to store the data from those labels on their own cell in the UITable in another cell. I'm currently lost on how I would set up my UITable to create a new cell and pass the data to that cell every time the validate button is pressed.
This is basic MVC behavior. Table cells are loaded at display time in the UITableView datasource delegate methods. The data should be loaded from some type of store, in your case most likely an array.
When you want to update the data (from anywhere), simply update the data store (array).
Reload the UITableView at will with the reloadData method (or whenever the view appears).
So the idea is that you want to store your UILabels' text values in UITableViewCells on the press of a button?
If this is the case, I would store each text value as an element in an NSArray after every time your button is clicked, like so:
// Given:
// 1.) Your labels are IBOutlets
// 2.) Your labels follow the naming convention label1, label2, label3, etc
// 3.) You have an initialized class variable NSMutableArray *labels
// 4.) NUM_OF_LABELS_IN_VIEW is the number of UILabels in your view
// 5.) myTableView is an outlet to your UITableView, and its delegate and datasource are set to your view controller
-(IBAction)buttonPressed:(id)sender{
self.labels = [[NSMutableArray alloc] init];
for (int i=0; i < NUM_OF_LABELS_IN_VIEW; i++){
[labels addObject:[self valueForKey:[NSString stringWithFormat:#"label%i", i]].text ];
}
[self.myTableView reloadData];
}
And your data source methods should look something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.labels count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];
}
cell.textLabel.text = self.labels[indexPath.row];
return cell;
}
If the UITableView is in a separate view controller, just assign the NSArray *labels to a #property on the presenting view controller.

How can I store my UITableViewCells in a NSMutableArray?

Basically I'm making a list view that you can add things to the top of. The best way I can think of doing this is to store the UITableViewCells themselves in a NSMutableArray — Because I can simply pull them from the array them with all their data inside the object, and this list view will never be over 10 cells long.
Also note that I'm using Storyboards, hence the initWithCoder use.
The following code is what I'm trying, and it doesn't work:
// This is where my NSMutableArray is initialized:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
if (!_CellsArray) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TestCell"];
_CellsArray = [NSMutableArray arrayWithObject:cell];
}
}
return self;
}
//UITableView Delegate & DataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TestCell"];
[_CellsArray insertObject:cell atIndex:0];
return [_CellsArray objectAtIndex:indexPath.row];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
I realize I may be approaching this in the wrong way, that's why I'm here though :)
Thank you.
edit: fixed a type in the code (TimerCell -> UITableViewCell)
Let's look at the order things get called in and what happens.
Your view controller is unarchived, so your initWithCoder: method is called. This method creates a mutable array and puts one instance of TimerCell into it. Said instance is not further configured (unless you've overridden initWithStyle:reuseIdentifier: to do some configuration).
Your data source method tableView:numberOfRowsInSection: is called, and it tells the table view there are ten rows.
Thus, your tableView:cellForRowAtIndexPath: is called ten times. Each time, it creates a new instance of UITableViewCell and inserts it into your mutable array. (After ten calls, your mutable array contains one TimerCell at index 10 and ten UITableViewCells at indices 0-9.) It does nothing to configure the cell's contents or appearance, then it returns the cell at the specified row index. On the first call, you're asked for row 0, so the cell you just created and inserted at index 0 is returned. On the second call, you're asked for row 1, so the cell at index 1 in your array is returned -- since you just inserted a new cell at index 0, the cell you created on the last call has shifted to index 1, and you return it again. This continues with each call: you return the same unconfigured UITableViewCell ten times.
It looks like you're trying to out-think UIKit. This is almost never a good thing. (It's been said that premature optimization is the root of all evil.)
UITableView already has a mechanism for cell reuse; it's best to just keep track of your own cell content and let that mechanism do its thing. I took so long to type this that other answers have been written describing how to do that. Look to them, or to Apple's documentation or any third-party UITableView tutorial.
Why don't you just store the cell information in an array. Then in the -cellForRowAtIndexPath: method, just extract the data needed to change each cell.
Here is a simple example:
//Lets say you have an init like this that inits some cell information
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
cellArray = [NSArray alloc] initWithObjects:#"firstCell",#"secondCell",#"thirdCell",nil];
}
return self;
}
//then for each cell, just extract the information using the indexPath and change the cell that way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
return cell;
}
Table views don't store things. Rather, they just ask for the data they want to display, and you typically get that data from elsewhere (like an NSArray, or an NSFetchedResultsController). Just store the things you want into some data container, and let the table display them for you.
// Probably your data model is actually a member of your class, but for purposes of demonstration...
static NSArray* _myArray = [[NSArray alloc] initWithObjects:#"Bob", #"Sally", #"Joe", nil];
- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [_myArray count];
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = #"TestCell";
// Make a cell.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Setup the cell with the right content.
NSString* aString = [_myArray objectAtIndex:[indexPath row]];
cell.textLabel = aString;
return cell;
}
Now if you want more stuff in the list, add it to your array, and you're done.
Edit: On another note, initWithCoder: isn't generally the best place to do initialization for a view controller. Reason being, at the point that it's called, there's a good chance that stuff isn't loaded yet (IBOutlets, for example). I tend to prefer viewDidLoad (don't forget to cleanup in viewDidUnload in that case), or awakeFromNib.

Populating two grouped tableviews from the same PLIST

In my app, there is a portion that holds a static contacts directory. I would like the directory to appear indexed (alphabetically) and the detail view will need to be grouped also. (I am somewhat trying to replicate the look and feel of the Contacts app.) I have it working, just no index and a detail page that is just a view with a collection of buttons.
For some reason, I cannot get the a-ha moment when dealing with the table view.
Does anyone have any examples of how I can do this? Even better, what is the absolute best book to show how to work with UITableViews (especially when grouping them) using a PLIST as a source?
Apples documentation and other searches have gotten me some good information, but feels far from comprehensive enough to fully "get it".
First of if youre using pLists as the source then they wont stay in any form of order, well not the order there in the pList anyway. A way around this is to have an array within your pList which then has your elements. To us data from a plist you might want to do something like this if you had a plist populated with NSDictionarys:
NSDictionary* dict = [NSDictionary initwithContentsOfFile:#"ApList.plist"];
//Get all of the elements in the dictionary
NSArray *array = [dict allKeys};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier;
MyIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Create a new dict based of the indexpath of the table cell, we need the array otherwise we wouldnt be able to get the key value for the dict.
NSDictionary* currentDict = [NSDictionary valueForKey:[array objectAtIndex:indexPath.row]];
//Then once you;ve got the dict create a string from a String held in that dict, in this case the key for the String is LABEL.
NSString *title = [currentDict valueForKey:#"LABEL"];
cell.textLabel.text = title;
return cell;
}
Hope this helps

Selecting a default item when using UITableViewController as a checkbox list

In a lot of iPhone apps, I see a UITableViewController being used as a checkbox list. (See, for an example of what I mean, Auto-Lock under Settings)
While trying to implement this myself, I had to jump through a lot of hoops in order to have an item selected programmatically by default (ie., the current value for what the list represents). The best I've been able to come up with is by overriding the viewDidAppear method in my view controller class:
- (void)viewDidAppear:(BOOL)animated {
NSInteger row = 0;
// loop through my list of items to determine the row matching the current setting
for (NSString *item in statusItems) {
if ([item isEqualToString:currentStatus]) {
break;
}
++row;
}
// fetch the array of visible cells, get cell matching my row and set the
// accessory type
NSArray *arr = [self.tableView visibleCells];
NSIndexPath *ip = [self.tableView indexPathForCell:[arr objectAtIndex:row]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:ip];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.lastIndexPath = ip;
[super viewDidAppear:animated];
}
Is this the best/only/easiest way to get a reference to a particular cell and indexPath if I want to mark a row by default?
In order to display the status items, you have to implement tableView:cellForRowAtIndexPath: anyway, don't you? So, why not just set the accessory type of the cell before returning the cell, like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// dequeue or create cell as usual
// get the status item (assuming you have a statusItems array, which appears in your sample code)
NSString* statusItem = [statusItems objectAtIndex:indexPath.row];
cell.text = statusItem;
// set the appropriate accessory type
if([statusItem isEqualToString:currentStatus]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Your code is extremely fragile, especially because you use [self.tableView visibleCells]. What if there are more status items than rows fitting on the screen (as the name suggests, visibleCells only returns the currently visible cells of the table view)?