UItableview inside a view based application - iphone

I have a view based application and a UITableview in the view.
I set the dataSource , delegate and tableView to "File's Owner" when i click on the tableview
then clicking on the Files owner i set the tableView to Table View , view to View , datasource to Table View and delegate to Table view in the outlets.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)videoView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)videoView:(UITableView *)videoView numberOfRowsInSection:(NSInteger)section {
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)videoView:(UITableView *)videoView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [videoView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
return cell;
}
- (void)tableView:(UITableView *)videoView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
when i run the application for the simulator i get the following error
"tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6029710
"
i get the feeling that tableviews require different implementation when used in view based app rather than a navigation based application. I would be very thankful if anyone can guide me to what needs to be done to get this displayed properly.thanks.

The implementation is the same whether you are in a view-based or navigation-based application. What this error message is telling you is your table view tried to call tableView:numberOfRowsInSection: on its data source, but that the data source didn't implement a method with that name. Sure enough, looking at your sample code, you implement a method called videoView:numberOfRowsInSection: instead of tableView:numberOfRowsInSection:.

For a delegate implementation you can't change tableView to videoView. Should be:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
You need to fix the other cellForRowAtIndexPath as well!

I set the dataSource , delegate and tableView to "File's Owner" when i
click on the tableview then clicking on the Files owner i set the
tableView to Table View , view to View , datasource to Table View and
delegate to Table view in the outlets.
It's difficult to understand what you've done here. You set the table's data source to the table view itself?
when i run the application for the simulator i get the following error
"tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x6029710 "
What object is at address 0x6029710? It's surely the object that you set as the table's data source, but it doesn't implement the UITableViewDataSource protocol.
i get the feeling that tableviews require different implementation
when used in view based app rather than a navigation based
application.
There's really no difference between a "view based app" and a "navigation based app." There are "view based" and "nav based" project templates, but those are just two different starting points for your app. UITableView doesn't care which one you use.

Related

Troubles with datasource and tableviews

I've got an UIViewController with 2 tableviews:
1- the main tableview, which is shown in the whole view controller
2- the second viewcontroller, which is loaded in a popup view.
The second tableview is shown on swiping a cell of main tableview.
Depending on which cell is swiped, there are different data in popup view.
I've already loaded the whole data in viewdidload method and stored everything in nsmutablearray, so are ready to be loaded.
My problem is that I don't know how to work with tableview's DataSource, in my project i linked both tableview's datasource to file's owner, but in this way it loads the numberofrows from the main view, and it doesn't take the correct count which should have the second tableview.
So, if in main tableview i have for example 3 elements, and in the second tableview it should load 5 elements, it gives me an error, ('NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index…).
I think that i should link my tableviews to different datasource, even if i really don't know.
I hope to be clear, if you need more info, or a better description, ask me and i'll do it for you.
In the delegate method, you should compare the tableview.
See the example,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == maintableView)
return VALUE_FOR_MAIN_TABLE_DATA;
else
return VALUE_FOR_POP_TABLE_DATA;
}
No Problem
you just set tags to your tables.
[tableView1 setTag:1];
[tableView2 setTag:2];
and then
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView.tag == 1) {
return 4;
}
else if (tableView.tag == 2)
{
return 5;
}
}
do similar thing all data source method
You can use one View Controller as an unique data source for multiple table view, but you'll need to check which table view is requesting data using the tableView arguments of the UITableViewDataSource methods.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTableView)
{
// code for your main table view
}
else
{
// code for your popup table view
}
}
The same instance of a view controller serving as data source/delegate for two tables is technically possible but has a number of traps. One of these is: to which of the two tables does self.view refer?
However, most of the data source protocols hand down a reference to the table. You could use that rerference to determine which table acutally sends the request. But not all methods of the protocol inclulde a reference to the table. And that is where it starts getting tricky.
You are far better off with two distinctive view controllers, one for each table. Establish a protocol between them so that the main view controller can hand down the data to the one in the popup window so that the popup can initialize/load it self with the proper data and can refresh its view when ever the data changes.
You will need to check for the tableview in all your delegate and datasource methods as follows:
if (tableView == mainTable)
{
// code for your main table
}
else if (tableView == popupTable)
{
// code for your popup table
}
You do same for 2 or more table views.
Hope this helps.
You can do it both ways:
Make separate classes as data source for separate tables. Instantiate their objects as datasources for tables and bind them at viewDidLoad method in proper view controller.
Make one datasource for 2 tables which I don't recommend as it is not comply with proper OOAD. You'll have tight coupling this way between view controller and the table which can be cause of trouble in the near future.
You have method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
in which you can distinguish to which table you are referring:
if (tableView == mainTableView) {
// initialize cell for main table view
}
if (tableView == secondTableView) {
// intialize cell for second table view
}

Can't select Cell in UITableView For UITableViewCellAccessoryCheckmark

If you have used Messages application in iOS, you know how we could invoke UITableViewCellAccessoryCheckmark in any message through edit button and then select each bubble/cell for forward or deletion purpose.
I'm trying to do the same in my application. I can tap on edit and UITableViewCellAcessoryCheckMark is shown, but I can't select the cells using it. What more do I need to implement?
Any help would be appreciated. Here is the code -
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellAccessoryCheckmark;
}
For a table view as shown in the picture, where one or more cells can be selected with a checkmark symbol, you have to set allowsMultipleSelectionDuringEditing = YES on the table view. This can be done either in viewDidLoad with
self.tableView.allowsMultipleSelectionDuringEditing = YES
or in the Attributes Inspector of the table view in the NIB/Storyboard file by setting "Editing" to "Multiple Selection During Editing".
The tableView:editingStyleForRowAtIndexPath: method is not needed for this.
(And btw your method returns UITableViewCellAccessoryCheckmark which is a UITableViewCellAccessoryType and not a UITableViewCellEditingStyle.)

Table cell as the delegate of the text field inside it?

I unfortunately still have not seen the light when it comes to organising my iphone app nicely into controllers and views. Let me illustrate with an example:
I am working on a sign up page which consists of a table view with a list of custom table cells. Some of these cells have a text field inside them and when the user touches one of those a keyboard slides up from the bottom. The keyboard has a return key in its lower right corner and when the user hits this key I would like the keyboard to slide down again.
Now, where do I put the
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
? Currently I have made my custom table cell conform to the text field delegate protocol and have put the method in there, but it does seem a bit wrong to have stuff like that inside a view class? On the other hand I do not find it appropriate in the table view controller either.
you can set your table view controller as the text field's delegate...
just remove the code in the custom cell where you set it as the delegate and instead set the delegate in the table view controller's cellForRowAtIndexPath method where you actually create and return the cell..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
MyCustomCell *myCell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil)
{
myCell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:CellIdentifier] autorelease];
myCell.myTextField.delegate = self;
}
//other cell specific code goes here
return myCell;
}

Calling a New View when selecting a Row in a 'UITableView'

I am currently writing my first iPhone app, but have encountered an issue. I have a view which contains a UITableView. This is the first time that I have attempted this, and this is the behaviour that I am trying to achieve:
When the user selects one of the rows, I would like this to call a new view, taking the user to a different page displaying info in reference to what they have selected.
I have it currently, so when the user selects a row it displays a UIAlert in the same view, but this doesn;t suit my needs. I have set the UITableView up through interface builder, and inputted the following code into my .m file to set it up.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//return the value
return 10;
}
//now we define the cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Identifier for retrieving reusable cells.
static NSString *cellIdentifier = #"MyCellIdentifier";
// Attempt to request the reusable cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// No cell available - create one
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
// Set the text of the cell to the row index.
cell.textLabel.text = [NSString stringWithFormat:#"iPad %d", indexPath.row];
return cell;
}
This creates a list of ten rows. The following codes gives me my UIAlert when tapped, however, I want to remove this and make it call a new view of my choice;
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Show an alert with the index selected.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"iPad Selected"
message:[NSString stringWithFormat:#"iPad %d", indexPath.row]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Can anyone help with this last piece of code? the view I want it to call is called 'ProteinView'.
Alrighty, what we need to do is use one of the UITableView methods that are already readily available to us. We'll do the following.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProteinView *detailViewController = [[ProteinView alloc] initWithNibName:#"ProteinView" bundle:nil];
// It is here we'd pass information from the currently selected UITableViewCell to the ProteinView.
// An example of this is the following.
// I would do it like this, but others would differ slightly.
NSString *titleString = [[[NSString alloc] initWithFormat:#"iPad %d",indexPath.row] autorelease];
// title is an object of detailViewController (ProteinView). In my own instances, I have always made a NSString which in viewDiDLoad is made the self.navigationBar.title string. Look below for what my ProteinView.m and .h would look like.
detailViewController.stringTitle = titleString;
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
EDIT
// -------- ProteinView.m -------- //
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Here we set the navigationItem.title to the stringTitle. stringTitle is declared in the .h. Think of it as a global scope variable. It is also propertised in the .h and then synthesized in the .m of ProteinView.
self.navigationItem.title = stringTitle;
}
I haven't compiled this, so I don't know if it'll fully work. But that is definitely the fastest and most easiest way to do it!
You could present the view modally like this
YourViewController2 *viewController2 = [[YourViewController2 alloc]initWithNibName:#"YourViewController2" bundle:nil];
[self presentModalViewController:viewController2 animated:YES];
Do you have more than one view to present? If so you will need to create an array with the names, pass it into the tableview and then present the correct view for the row selected based on the indexPath.row.
you'll have to open your MainWindow.xib and add a navigation controller to it. Then add a navigation controller outlet to your app delegate and connect them. Then you'll need to set the navigation controller's view as your main window's view.
You can add a table view to any iPhone app fairly easily, just by creating a new UITableViewController subclass from the File -> New command.
Even if you go this route, I would suggest creating a new navigation-based project to use as a template/cheat-sheet.

cellForRowAtIndexPath not called; sections returns 1 and rows returns 4

After parsing JSON data in a Data class, I set the UIViewController's NSArray *headlines property in a fillArrays method of the same Data class. In the viewDidAppear method of my UIViewController, I call reloadData on my UITableView. numberOfSectionsInTableView fires and returns 1, then numberOfRowsInSection fires and returns an array count of 4 (for 4 strings in the array). However, control never gets to cellForRowAtIndexPath and I'm having the hardest time understanding why, especially since I have valid sections and rows. The cells are all visible.
I've added the UITableViewDataSource and UITableViewDelegate protocols to the UIViewController interface and set the UITableView's delegate and dataSource to self in viewDidLoad (which also is verified by the row and section count methods being called).
I'm wondering if it has something to with me reinitializing the UIViewController in Data.m in order to set its properties.
In Data.m:
- (void)fillArrays:(NSArray *)jsonObjs {
NSLog(#"fillArrays");
HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
hrvc.headlines = [self getJsonValuesForKey:#"headline" inArrayOfObjects:jsonObjs];
[hrvc viewDidAppear:NO];
}
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad");
// Table view
headlineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
[headlineTableView setDelegate:self];
[headlineTableView setDataSource:self];
// Temporary
self.headlines = [[NSMutableArray alloc] initWithObjects:#"headline1", #"headline2", #"headline3", #"headline4", nil];
[self.view addSubview:headlineTableView];
self.headlineTableView = headlineTableView;
[headlineTableView release];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"viewdidappear");
NSLog(#"headlines: %#", self.headlines); // Returns an array of 4 headlines
if( [self.headlines count] != 0 ){
[self.headlineTableView reloadData];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"numberOfSectionsInTableView: 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"numberOfRowsInSection: %d", [self.headlines count]);
return [self.headlines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cellForRowAtIndexPath");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [[NSString alloc] initWithFormat:#"%#", [self.headlines objectAtIndex:indexPath.row]];
return cell;
}
In fillArrays, you create another view controller - but you never do anything with it or its view, you would never see that view. You would never call viewDidAppear manually either, that happens automatically when a view controllers view is displayed (ONLY in the context of a navigation controller though).
Normally the flow is, you create a view controller and either add that view as a subview of a current view, or push it as a new window via a navigation controller. I'm pretty sure your whole issue is that they table is never added to a view anyone actually sees, so the table calls the other methods but never calls cellForRow because its layoutSubviews code is simply not being called.
Have you added your tableView to the view of UIViewController?
It happened to me, and when I added this
[self.view addSubview:table];
[table release];
then cellForRowAtIndexPath started working.
For Google's sake:
If tableView:numberOfRowsInSection returns zero for whatever reason tableView:cellForRowAtIndexPath will not get called because there are no rows to call it for.
Check to make sure that the tableView delegate and dataSource are pointed to the viewController
I cannot see anything wrong with the code as-is, have you verified with breakpoints that cellForRow is never reached (even though I see you have a log statement)?
Also I would try just for a sanity check to return "1" explicitly in rowsInSection, and hardcode a string in the cell you are returning in cellForRow.
If all else fails, create a new table view controller from the XCode templates and put your calls in there - then when that works, work backwards to why your code does not.
Also, it would be good to see your viewDidLoad setup code (add to answer above please).
if you're setting the delegate and datasource at viewDidLoad, then that may be the source of your bug. Can you set the datasource and delegate in init?
I'm not sure that you add your UITableView as subview to UIViewController.view. This was my approach anyway.
In this approach, I found execution did not get into cellForRowAtIndexPath until I sent UIViewController.view to the back after adding UITableView as subview.
Getting this far was only part of the problem. At this point, it seemed that my other view controllers no longer respond to touch events. I found that when I also add the UITableView as a subview to the rootViewController, all my views got the appropriate touch events.
Thank you so much pxl. When I move the UITableView initialization from viewDidLoad to:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil"
it works perfectly when ever I make delete or update some rows the UITableView gets reloded to my UIView.
Swift version
Add self.table.layoutIfNeeded() and then self.tableView.reloadData()