I have a UITableview made up with a custom cell loaded from a nib. This custom cell has 9 UILabel s and thats all. When scrolling the table on my iPhone the tables scrolling motion is slightly jerky, its not as smooth as other tableviews! (On the simulator it scrolls fine but I guess its using the extra power of my mac)
Are there any tips to help optimize this or any tableview or methods to help find the bottleneck.
Many Thanks
UPDATE:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [[fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Returns the title for each section header. Title is the Date.
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
NSArray *objects = [sectionInfo objects];
Data *myData = [objects objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSDate *headerDate = (NSDate *)myData.dataDate;
NSString *headerTitle = [formatter stringFromDate:headerDate];
[formatter release];
return headerTitle;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *dataEntryCellIdentifier = #"dataEntryCellIdentifier";
DataEntryCell *cell = (DataEntryCell *)[tableView dequeueReusableCellWithIdentifier:dataEntryCellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DataEntryCell" owner:self options:nil];
cell = self.dataEntryCell;
self.dataEntryCell = nil;
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
//Method to setup the labels in the cell with managed object content.
- (void)configureCell:(DataEntryCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Data *myData = [fetchedResultsController objectAtIndexPath:indexPath];
cell.label1.text = myData.data1;
cell.label2.text = myData.data2;
cell.label3.text = myData.data3;
cell.label4.text = myData.data4;
cell.label5.text = myData.data5;
cell.label6.text = myData.data6;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//Check to see if its in Delete mode
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Delete Object from context.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
//Save Context to persitant store
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Delete Error");
exit(-1);
}
}
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Table is not to be manually reordered
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Create a detailView instance.
DetailViewController *detailView = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
//Pass selected data to the detailView.
Data *myData = [fetchedResultsController objectAtIndexPath:indexPath];
detailView.myData = myData;
//Push the detailView onto the Nav stack.
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
Take a look at this question, which references the author of Tweetie. In short: do your drawing manually, not using subviews.
The original Tweetie article can now be found here.
Related
I am newbie with Xcode 5 and start do some coding with appcode.com's tutorial.
I can't get the the proper search result printing back to tableview(core data), any suggestion will be highly appreciated, thanks in advance! cheers.
#import "RecipeStoreTableViewController.h"
#import "AddRecipeViewController.h"
#interface RecipeStoreTableViewController () {
NSMutableArray *recipes;
NSArray *searchResults;
}
#end
#implementation RecipeStoreTableViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:#selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the recipes from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Recipe"];
recipes = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
// NSLog(#"test %#", [recipes valueForKey:#"name"]);
// Reload table data
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}else{
return [recipes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *recipe = [recipes objectAtIndex:indexPath.row];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults valueForKey:#"name"];
NSString *description = [[NSString alloc] initWithFormat:#"%# - %#", [searchResults valueForKey:#"image"], [searchResults valueForKey:#"prepTime"]];
cell.detailTextLabel.text = description;
}else{
cell.textLabel.text = [recipe valueForKey:#"name"];
NSString *description = [[NSString alloc] initWithFormat:#"%# - %#", [recipe valueForKey:#"image"], [recipe valueForKey:#"prepTime"]];
cell.detailTextLabel.text = description;
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[recipes objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
}
// Remove recipe from table view
[recipes removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"UpdateRecipe"]) {
NSManagedObject *selectedRecipe = [recipes objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
UINavigationController *destViewController = segue.destinationViewController;
AddRecipeViewController *recipeViewController = (AddRecipeViewController*)destViewController.topViewController;
recipeViewController.recipe = selectedRecipe;
}
}
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
#end
You are calling the valueForKey on NSMutableArray object, which should be called on NSManagedObject
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *recipe;
if (tableView == self.searchDisplayController.searchResultsTableView)
recipe = searchResults[indexPath.row];
else
recipe = recipes[indexPath.row];
cell.textLabel.text = [recipe valueForKey:#"name"];
NSString *description = [[NSString alloc] initWithFormat:#"%# - %#", [recipe valueForKey:#"image"], [recipe valueForKey:#"prepTime"]];
cell.detailTextLabel.text = description;
return cell;
}
I am trying to display a dictionary plist into UITableView , I have read many question and answer here but no luck ! it seems cells return null ! here is my plist :
and my codes :
-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"feed" ofType:#"plist"];
newsFeed = [NSArray arrayWithContentsOfFile:path];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return newsFeed.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dictionary.allKeys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.section];
cell.textLabel.text = [[newsFeed objectAtIndex:indexPath.row] valueForKey:#"title"];
return cell;
}
and the result is nothing :
Try this in you want to display only title then try this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return newsFeed.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary valueForKey:#"title"];
return cell;
}
EDIT
And if you want to display all record then try this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return newsFeed.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dictionary.allKeys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.section];
NSString *key =[[dictionary allKeys] objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary valueForKey:key];
return cell;
}
You should learn how to debug problems like this. Or if you tried, you should tell us what you tried in the question.
Set a breakpoint in viewDidLoad and make sure, the plist is loaded correctly. The path might be wrong. The file might be corrupt or have a different root object then what you're expecting.
Set a breakpoint in numberOfSectionsInTableView. Is it called at all? Is it called before or after you load your plist? Probably after. In that case you need to call [tableView reloadData]. If it's called what do you return.
Continue like this, step for step until you find the reason.
Try this,I use plist's a lot.
In ViewDidLoad
NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:self.plistName];
self.tableContents=[NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
self.sortedKeys = [[self.tableContents allKeys] sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
NSUInteger row = [indexPath row];
cell.textLabel.text = [NSString stringWithFormat:#" %#",[listData objectAtIndex:row]]; //take your object out
return cell;
}
Set the Root As NSDictionary, put your newsFeed in the dictionary.
Use NSDictionary *dict = [NSDictionary ...fromFile:...];
newsFeed = [dict valueForKey:#"some_key"];
I'm trying to reload a UITableView when the row of a different UITableView's row is selected. One of the problems I'm having is that both the UITableView's are in the same view. I would like a selection in Table1 to change the NSMutableArray that's used to populate Table2. And then reload the table.
At the moment it works fine, but only when the app re-lauches (or the view is popped off the stack and then re-visited) and viewWillAppear is called again
Here's my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.toolbarHidden = YES;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
selectedTableString = [prefs stringForKey:#"selectedTableString"];
if ([selectedTableString isEqualToString:#"Italy"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=it";
}
else if ([selectedTableString isEqualToString:#"Spain"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=es";
}
else if ([selectedTableString isEqualToString:#"UK"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=uk";
}
else if ([selectedTableString isEqualToString:#"Brazil"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=br";
}
NSLog(#"from two t selectedTableString %#",selectedTableString);
// Download the yoodeal JSON
NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:jsonStringCountry] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
NSLog(#"jsonStringCountry is %#", jsonStringCountry);
NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];
// Create parser for the yoodeal api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
itemsTMP = [results objectForKey:#"results"];
self.displayItems = [itemsTMP copy];
}
My UITableView methods:
- (int)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"name"];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
return cell;
}
You'll need to use the delegate method of UITablveView. The method is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This methods gets called when you click on the row of UITableView. Use this in following manner:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == Table1)
{
//do the changes in array you want to do.
[table2 reloadData];
}
else
{
//do the changes in array you want to do.
[table1 reloadData];
}
}
Create outlet for the table view
[self.yourTableView reloadData];
You should create a property of the UITableView called secondTableView and then use [secondTableView reloadData].
Use [tablename reloadData]; onclick.
When you are updating tableview contents, it will not refresh the view. You can only see changes once the view controller is reloaded.
For updating the table instantly you need to call the above code
Till now I have implemented UITableView by populate the contents from the database
By retrieving in an array from sqlite data base
storedContactsArray = [Sqlitefile selectAllContactsFromDB];
so no multiple sections , section headers and returns storedContactsArray.count as number of rows.
Now i need to populate the same data in table view but The data set in Alpabetical sections in alphabetical order.
I tried with
alphabetsArray =[[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabetsArray count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabetsArray;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [alphabetsArray objectAtIndex:section];
}
But in case of numberOfRowsInSection it fails since there is no contacts in storedContactsArray initially
Error occurs: -[__NSArrayM objectAtIndex:]: index 25 beyond bounds for empty array
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[storedContactsArray objectAtIndex:section] count]
}
any advice of use full links pls
To achieve your requirement you need to first separate out all the data in to section as Alphabetical order. which is as following.
Here section is a Mutable Dictionary in which we will get all data as alphabetical sets.
//Inside ViewDidLoad Method
sections = [[NSMutableDictionary alloc] init]; ///Global Object
BOOL found;
for (NSString *temp in arrayYourData)
{
NSString *c = [temp substringToIndex:1];
found = NO;
for (NSString *str in [sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
for (NSString *temp in arrayYourData)
{
[[sections objectForKey:[temp substringToIndex:1]] addObject:temp];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[sections allKeys]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] 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];
}
NSString *titleText = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = titleText;
return cell;
}
Please try it I am using it and it working fine
Hope it helps you !!!
I have data that is stored in a plist but when I pull it to my UITableView it gets reordered for some reason. Here are my table view data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.lunch_Dinner count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[self.lunch_Dinner allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:section];
return [[self.lunch_Dinner valueForKey:typeOfEntree] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EntreeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:indexPath.section];
NSString *entree = [[self.lunch_Dinner valueForKey:typeOfEntree] objectAtIndex:indexPath.row];
cell.textLabel.text = entree;
return cell;
}
Here is the order of the plist:
Appetizers
Soups
Pastas
Pizzas
Specials
And this is the resulting order in the UITableView after being compiled:
Pizzas
Soups
Appetizers
Pastas
Specials
Any help would be great!
Thanks in advance.
If you want to store table in plist it is better to consider the following structure:
NSArray *sections = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name 1"],
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name 2"],
...
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name N"],
nil];
This code representation is easy to reproduce as plist. Create it as in example below
This structure can be easily used as UITableView datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.datasource count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDictionary *dict = [self.datasource objectAtIndex:section];
return [dict.allKeys lastObject];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *dict = [self.datasource objectAtIndex:section];
return [dict.allValues.lastObject count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [self.datasource objectAtIndex:indexPath.section];
id cellPresenter = [dict.allValues.lastObject objectAtIndex:indexPath.row];
...
}
It looks like lunch_Dinner is a NSDictionary. NSDictionarys are unordered, so you are not guaranteed a specific output order from them.
It is a little hard to see exactly what value you're getting from having a key/value pair here, but one option would be to keep a separate NSArray with the correct ordering, and use that to correctly populate things.