I have a problem with my tableview.
It has a space on top like this:
When I open the TasksTableViewController, the problem doesn't show. But when I open another viewcontroller from TaskTableVC like this:
FilterTasksTableViewController * fttvc = [self.storyboard instantiateViewControllerWithIdentifier:#"FilterTasksTableViewController"];
fttvc.delegate = self;
UINavigationController * navVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PopoverNavigationController"];
[navVC setViewControllers:#[fttvc]];
[self presentViewController:navVC animated:YES completion:nil];
and go back to TaskTableVC, the problem occurs.
When I "pull down to refesh", it goes back to normal.
in my code for TaskTableVC:
- (void)viewWillAppear:(BOOL)animated {
//other code
[self populate];
}
- (void)viewDidLoad {
dispatch_async(dispatch_get_main_queue(), ^{
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#" "];
[self.refreshControl addTarget:self action:#selector(refresh) forControlEvents:UIControlEventValueChanged];
[self setRefreshControl:self.refreshControl];
});
[self populate];
}
- (void)populate {
TaskHandler *handler = [[TaskHandler alloc] initWithContext:_context];
NSArray *allTasks = [handler getAll];
_tasks = [self filterTasks:allTasks];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"startDate" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObjects:descriptor, nil];
_tasks = [[NSMutableArray alloc] initWithArray:[_tasks sortedArrayUsingDescriptors:descriptors]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
});
}
I hope you can help me with this weird problem.
I solved my problem with the code.
I thought there was something wrong with the Refresh control so I moved it out of the dispatch_aysnc(dispatch_get_main_queue() and added [self.tableview reloadData . That fixed my problem. Thanks everyone for answering. :)
In view viewWillAppear set .
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
[self setEdgesForExtendedLayout:UIRectEdgeBottom];
}
hope it help you .
You Need to make Sure that your UITableViewCell has correct Height.
For doing that you can simply implement the UITableViewDelegate method :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90.0f; //CHANGE TO YOUR DESIRED HEIGHT.
}
I hope this helps.
Related
I've added a 'pull down to refresh' function to my TableView inside a ViewController. The data refresh works perfectly, but I'm wondering how I can add the classic activity indicator into the top when the refresh is executed?
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[_refreshControl addTarget:self action:#selector(refreshData) forControlEvents:UIControlEventValueChanged];
//[self.mytable addSubview:refreshControl];
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.tableView;
tableViewController.refreshControl = _refreshControl;
}
-(void)refreshData
{
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.tableView;
[self.tableView reloadData];
[tableViewController.refreshControl endRefreshing];
}
It looks like the issue is that both viewDidLoad and refreshData are creating new tableViewControllers but they should be updating your existing one.
If your class in ViewController.m is a subclass of UITableViewController you can update your code to use the current view controller.
- (void)viewDidLoad {
[super viewDidLoad];
[_refreshControl addTarget:self action:#selector(refreshData) forControlEvents:UIControlEventValueChanged];
self.refreshControl = _refreshControl;
}
-(void)refreshData
{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}
Another issue is that refreshData immediately calls endRefreshing so the refresh Controller wont have any time to be visible before it ends. Normally you'd have an asynchronous task running, like a network request, then call endRefreshing once that completes. To fake it you can delay calling endRefreshing with the following.
-(void)refreshData
{
// Wait 1 sec before ending refreshing for testing
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
});
}
This is what ended up working!
ViewController.h
#property (nonatomic, strong) UIRefreshControl *refreshControl;
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:#"Pull To Refresh"];
[self.refreshControl addTarget:self action:#selector(refreshData) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:self.refreshControl];
}
-(void)refreshData
{
// Wait 1 sec before ending refreshing for testing
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
});
}
[self.tableView reloadData] not updating numberOfRowsInSection when called.
Delegate and datasource set programmatically in viewDidLoad
Also, put in a UIRefreshControl even though I'm using the tableView in a normal UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
[self.tableView addSubview:refreshControl];
}
Refresh Control calls location object to get location. Self gets notified when location object has received the location. Notification calls selector getLocation to make API call with location as parameters
- (void)refresh:(UIRefreshControl *)refreshControl {
[self.loadTableArray removeAllObjects];
LPLocationManager *locationObject = [LPLocationManager sharedManager];
[locationObject.locationManager startUpdatingLocation];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(getLocation:) name:#"locationNotification" object:nil];
[refreshControl endRefreshing];
}
GET request parameters sent to API request object and request object calls method on self that loads mutableArray. MutableArray is copied to NSArray and [self.tableView reloadData] is called but numberOfRowsInSection doesn't update.
-(void)getLocation:(NSNotification*)notifications{
self.latitude = notifications.userInfo[kSetLat];
self.longitude = notifications.userInfo[kSetLong];
self.requestObject = [[LPRequestObject alloc]init];
[self.requestObject getLocationMediaWithLat:self.latitude andLong:self.longitude];
}
-(void)updateMutableArray:(NSArray*)array{
self.loadTableArray = [[NSMutableArray alloc]init];
for (NSDictionary *dictionary in array) {
[self.loadTableArray addObject:dictionary];
}
self.loadTableArrayCopy = [self.loadTableArray copy];
[self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.loadTableArrayCopy count];
}
The tableView is added to a subview in a custom container view with code to achieve in mainContentVC
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationViewController = [[LPLocationViewController alloc]init];
self.locationViewController.view.frame = self.view.bounds;
self.currentViewController = self.locationViewController;
[self addChildViewController:self.currentViewController];
[self.currentViewController didMoveToParentViewController:self];
[self.containerView addSubview:self.currentViewController.view];
self.diaryViewController = [[LPDiaryViewController alloc]init];
self.diaryViewController.view.frame = self.view.bounds;
self.settingsController = [[LPSettingsViewController alloc]init];
self.settingsController.view.frame = self.view.bounds;
if ((self.currentViewController = self.locationViewController)) {
self.locationButton.enabled = NO;
}
}
I have a view controller where the user can populate an item. Upon exiting the view that object should be saved, and loaded by the previous view which contains a list of all these objects. My NSLog shows that the object is being saved, but I can't get it to appear in the list view.
Code for both controllers is below. Thanks
List Controller
#import "ItemsViewController.h"
#import "Calculation.h"
#import "CalculationItemStore.h"
#import "CalculationDetailViewController.h"
#interface ItemsViewController()
#property UISegmentedControl *segment;
#end
#implementation ItemsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = #"MACS";
// Create a new bar button item that will send
// addNewItem: to CalculationsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
navItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
self.tableView.tableHeaderView = _segment;
[_segment addTarget:self action:#selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[CalculationItemStore sharedStore] allCalculations] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
cell.textLabel.text = item.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc] init];
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.calculation = selectedItem;
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
[[CalculationItemStore sharedStore] removeItem:item];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[CalculationItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
toIndex:destinationIndexPath.row];
}
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://ferm.forestry.oregonstate.edu/facstaff/leshchinsky-ben"]];
}else if(_segment.selectedSegmentIndex ==1){
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:#"MACSLoad#2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;
}
}
- (IBAction)addNewItem:(id)sender
{
// Create a new Calculation and add it to the store
Calculation *newItem = [[CalculationItemStore sharedStore] createCalculation];
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc]initForNewItem:YES];
detailViewController.calculation = newItem;
detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
// [self presentViewController:navController animated:YES completion:NULL];
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
Item controller save method
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//clear responder
[self.view endEditing:YES];
//save changes
BOOL success = [[CalculationItemStore sharedStore]saveChanges];
if(success){
NSLog(#"Saved all calcs");
}else{
NSLog(#"failure saving");
}
}
I think the issue is that by the time the table reloadData is called on the first controller, the second one is not done loading. You could notify the first VC to reload the data once the second one is done saving It can be notified using
create a delegate on the second one or
using notifications
Let me know if you need more details on how to do this!
Looks like there is also an issue with the object creation.
your calculation elements are not getting created properly. When you are ready to save.. look at the contents of self.privateItems. They values filled in are nil. You will need to instantiate the Strings properly.
engineerName needs to be alloc'd and other strings as well.
and when user hits Done, the values from the text boxes need to be set to the your data model.
I'm using the following code to create a UIRefreshControl:
- (void) viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(doLoad) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
- (void) doLoad
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// Instead of sleeping, I do a webrequest here.
[NSThread sleepForTimeInterval: 5];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadData];
[self.refreshControl endRefreshing];
});
});
}
It works great. If I navigate to my view, drag the table, the code runs and the data displays.
However, what I would like to do is have the view in the 'loading' state as soon as it appears (that way the user knows something is going on). I have tried adding the following:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.refreshControl beginRefreshing];
}
But it does not seem to work. When I navigate to the view, it looks like a regular view (refresh control is not visible), plus when I try to pull the refresh control, it never finished loading.
Obviously I'm going about this the wrong way. Any suggestions on how I should handle this?
Try this:
- (void) viewWillAppear: (BOOL) animated
{
[super viewWillAppear: animated];
self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
[self.refreshControl beginRefreshing];
// kick off your async refresh!
[self doLoad];
}
Remember to call endRefreshing at some point!
EDIT to add full working sample:
This sample view controller, built and run in iOS6.1 as the root viewcontroller starts with the UIRefreshControl already visible and animating when the app launches.
TSTableViewController.h
#interface TSTableViewController : UITableViewController
#end
TSTableViewController.m
#import "TSTableViewController.h"
#implementation TSTableViewController
{
NSMutableArray* _dataItems;
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget: self
action: #selector( onRefresh: )
forControlEvents: UIControlEventValueChanged];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
[self.refreshControl beginRefreshing];
[self onRefresh: nil];
}
- (void) onRefresh: (id) sender
{
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
_dataItems = [NSMutableArray new];
for ( int i = 0 ; i < arc4random() % 100 ; i++ )
{
CFUUIDRef uuid = CFUUIDCreate( NULL );
[_dataItems addObject: CFBridgingRelease(CFUUIDCreateString( NULL, uuid)) ];
CFRelease( uuid );
}
[self.refreshControl endRefreshing];
[self.tableView reloadData];
});
}
#pragma mark - Table view data source
- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection: (NSInteger) section
{
return _dataItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: nil];
cell.textLabel.text = [_dataItems objectAtIndex: indexPath.row];
return cell;
}
#end
Manually modifying the contentOffset is insecure and wrong and can lead to unexpected behavior in some cases. This solution works without touching the contentOffset at all:
func showRefreshControl(show: Bool) {
if show {
refreshControl?.beginRefreshing()
tableView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: true)
} else {
refreshControl?.endRefreshing()
}
}
Another option is fire a UIControlEventValueChanged in your viewDidAppear: to trigger an initial refresh.
- (void) viewDidAppear: (BOOL) animated
{
[super viewDidAppear: animated];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(doLoad) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
[self.refreshControl beginRefreshing];
}
You never set self.refreshControl
I'm trying to create an app for the iPhone where I pull data from a database and then display it on a table (a completely new view from the main screen in which I ask the user to enter in data). I've used this framework to help switch views in my app:
http://www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/
And basically, I modified it a little bit. I have it implemented fine, but when I populate the table I have to pass the array of information from a class where I pull the data from into the view where I display the table. I found the problem with my code (displayed below).
-(void) displayView:(int)intNewView{
NSLog(#"%i", intNewView);
[currentView.view removeFromSuperview];
[currentView release];
ServiceProvider *g = [[ServiceProvider alloc] init];
ServiceProvider *l = [[ServiceProvider alloc] init];
[g setSPNAME:#"george"];
[l setSPNAME:#"luuuuuusaaaa"];
passInTableToTOI = [[NSMutableArray alloc] initWithObjects:g, l, nil];
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
TableOfItems *tOI = [[TableOfItems alloc] init];
switch (intNewView) {
case 1:
currentView = [[SearchPage alloc] init];
break;
case 2:
[tOI setPassedThroughTable:passInTableToTOI];
[rP setResultsTable:tOI];
currentView = rP;
break;
case 3:
currentView = [[ShowAllPage alloc] init];
break;
/*default:
break;*/
}
//[rP release];
//[tOI release];
[self.view addSubview:currentView.view];
}
The table gets passed fine, but when I try to display the view I get this error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ResultsPage tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e396c0". I feel like I'm not giving enough information, so please if you'd like more information don't hesitate. I'm not sure what else I'm being vague on so it'd help to let me know how I can be more specific. Thanks a lot everyone.
[[EDIT 1]]
//===========ResultsPage.h
#import <UIKit/UIKit.h>
#class TopBottomRectangles;
#class TableOfItems;
#class SearchTextBox;
#interface ResultsPage : UIViewController {
TopBottomRectangles *tbRects;
TableOfItems *resultsTable;
SearchTextBox *sTB;
}
#property (nonatomic, retain) IBOutlet TableOfItems *resultsTable;
#end
//===================ResultsPage.m file
#import "ResultsPage.h"
#import "TopBottomRectangles.h"
#import "TableOfItems.h"
#import "SearchTextBox.h"
#import "MultiviewAppDelegate.h"
#implementation ResultsPage
#synthesize resultsTable;
-(void)goToShowAllPage
{
MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:3];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(#"load the results page");
tbRects = [[TopBottomRectangles alloc] init];
sTB = [[SearchTextBox alloc] init];
[self.view addSubview:[resultsTable view]];
[self.view addSubview:tbRects.bottomBG_View];
[self.view addSubview:tbRects.topBG_View];
[self.view addSubview:sTB.textBox_BG_border];
[self.view addSubview:sTB.textBox_BG];
[self.view addSubview:sTB.textBox];
//[self.view addSubview:btnTwo];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
NSLog(#"dealloc results page");
[sTB release];
[resultsTable release];
[tbRects release];
[super dealloc];
}
#end
[[EDIT 2]]
//====TableOfItems.h file
#import <UIKit/UIKit.h>
#interface TableOfItems : UITableViewController {
NSMutableArray *listOfItems;
NSMutableArray *passedThroughTable;
}
#property (nonatomic, retain) NSMutableArray* passedThroughTable;
#end
//=====TableOfItems.m file
#import "TableOfItems.h"
#import "MyTableCell.h"
#import "ServiceProvider.h"
#implementation TableOfItems
#synthesize passedThroughTable;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationController.navigationBarHidden = YES;
//set the size of the table
[self fixTableSize];
NSMutableArray *spName = [[NSMutableArray alloc] init];
for (int i = 0; i < [passedThroughTable count]; i++){
ServiceProvider *willBeGone = [[ServiceProvider alloc] init];
willBeGone = [passedThroughTable objectAtIndex:i];
[spName addObject:willBeGone.SPNAME];
NSLog(#"%#", [spName objectAtIndex:i]);
//[willBeGone release];
}
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:#"Iceland", #"Greenland", #"Switzerland", #"Norway", #"New Zealand", #"Greece", #"Rome", #"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
NSArray *countriesLivedInArray = [NSArray arrayWithObjects:#"India", #"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:#"Countries"];
[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];
}
-(void)fixTableSize{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
NSInteger height = screenBounds.size.height * screenScale;
NSInteger windowHeight = 100;
NSInteger yFromTop = 60;
NSInteger yFromBottom = height + 30 - windowHeight;
self.tableView.frame = CGRectMake(0,yFromTop,320,yFromBottom - yFromTop);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [listOfItems count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *sectionTitles = [[NSArray alloc] initWithObjects:#"Countries to visit", #"Countries visited", nil];
return [sectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
NSLog(#"====");
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"Countries"];
return [array count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Countries"];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(1, 1, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:60];
//label.tag = FIRCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:#"%d:00", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(70.0, 0, 110,
tableView.rowHeight)] autorelease];
[cell addColumn:260];
//label.tag = SECCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:#"%#", [array objectAtIndex:indexPath.row]];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(270.0, 0, 30,
tableView.rowHeight)] autorelease];
//[cell addColumn:180];
//label.tag = THIRCOL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:#"$%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
#end
I think the problem is not in the code that shows you. When you read the Exception, the problem is in ResultsPage class
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
in tableview data delegate method :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
So maybe the returned value is wrong or some arguments. We need ResultsPage code.
Bye
The error message is telling you that you're calling a UITableViewDataSource method on something that is not a UITableViewDataSource, because it does not implement the method: tableView:numberOfRowsInSection.
This method has to do with UITableView objects, and I don't see any UITableView objects anywhere in your code (nor in the PushPlay examples.)
Without going through it with a fine-toothed comb, I'd say you're trying to use an NSMutableArray in place of a UITableView and it's delegates.
An array makes a fine data source for a UITableView, but is not a direct substitute for a tableView.
Hope that helps.
-Mike
I found my problem. It was this clause:
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
TableOfItems *tOI = [[TableOfItems alloc] init];
It's supposed to be
ResultsPage *rP = [[ResultsPage alloc] init];
TableOfItems *tOI = [[TableOfItems alloc] initWithNibName:#"TableOfItems" bundle:[NSBundle mainBundle]];
I had a .xib file called "TableOfItems" and that should be corresponding to the class that populates the user interface, TableOfItems. This class hass all those delegates which creates the rows and sections and whatnot.
I'm sorry that I made such a foolish error...I don't think this would have been easily found by someone who didn't have access to the .xcodeproj