I have a tab bar control and I want to show table view in first tab.
In my story board I have Tab Bar view controller with Items and I put the tableview and tableview cell in it. ( I am not sure about it. Should I put them to show data in tableview or should I do it programmatically?) I am getting the data from database (there is not any problem in this side) but can not bind the data in tableview.
What is wrong in my code?
My code:
.h file
#interface CategoryTabController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
DatabaseProcess *databaseProcess;
UITableView *categoryTableView;
NSMutableArray *categoryTableArray;
}
.m file
#implementation CategoryTabController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
databaseProcess = [[DatabaseProcess alloc]init];
//Get Category data to array
categoryTableArray = [[NSMutableArray alloc]initWithArray:[databaseProcess getAllActiveCategory]];
categoryTableView = [[UITableView alloc]init];
categoryTableView.delegate = self;
categoryTableView.dataSource = self;
[self.view addSubview: categoryTableView];
}
- (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.
return categoryTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CategoryCell" forIndexPath:indexPath];
cell.textLabel.text = [categoryTableArray objectAtIndex: indexPath.row];
return cell;
}
You must make categoryTableView as IBOutlet property and connect it to your table in IB. Also you must set "CategoryCell" as cell identifier in IB to 'dequeueReusableCellWithIdentifier' to work.
Related
I have two view controllers/
1. SetupRingBoardViewController
2. SetupRingBoard*ADD*ViewController
the first view controller is UITableViewController.
when we first launch the view - the ViewController has a 1 fixed section with a 1 fixed row.
In that ViewController, there is a UIBarButton that is calling the SetupRingBoardADDViewController (modal - default, I'm using storyboard).
the second view controller is UIView controller.
this viewController contains a UITableView and a UINavigationBar.
the UITableView is actually a one big form, that the user can enter data into it.
the UINavigationBar contains an 'Add' UIBarButton.
When this button is being pressed, the method 'addButton' is being called.
The 'addButton' method should refresh the UITableView in the SetupRingBoardViewController.
In the end, after pressing the 'addButton' button - there should be 2 sections in the SetupRingBoardViewController's UITableView:
1. The fixed section with 1 row in it.
2. A section with X rows in it, each row will have a title: #"A Row!";
(X = the number of 'addButton' being pressed).
Finally, here's the code:
SetupRingBoardViewController.h :
//
// SetupRingBoardViewController.h
//
//
// Created by on 12/24/12.
// Copyright (c) 2012 Noam. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SetupRingBoardADDViewController.h"
//#import "StudyHour.h"
#interface SetupRingBoardViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *listOfStudyHours;
#end
SetupRingBoardViewController.m :
//
// SetupRingBoardViewController.m
//
//
// Created by on 12/24/12.
// Copyright (c) 2012 Noam. All rights reserved.
//
#import "SetupRingBoardViewController.h"
#import "SetupEmptyListViewController.h"
#interface SetupRingBoardViewController ()
#end
#implementation SetupRingBoardViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(id)init
{
self = [super init];
if(self != nil)
{
if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(_listOfStudyHours) NSLog(#"%#",_listOfStudyHours);
[self.tableView reloadData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if(![_listOfStudyHours count])
{
NSLog(#"numberOfSectionsInTableView: 1");
return 1;
}
else return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0) return 1;
else return [_listOfStudyHours count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(![indexPath section])
{
NSLog(#"It got to the first");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
else
{
NSLog(#"It got to the second");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [_listOfStudyHours objectAtIndex:indexPath.row];
return cell;
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
SetupRingBoardADDViewController.h :
// SetupRingBoardADDViewController.h
//
//
// Created by on 12/26/12.
// Copyright (c) 2012 Noam. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SetupRingBoardViewController.h"
#import "UITableViewCell+Checkmark.h"
//#import "StudyHour.h"
#interface SetupRingBoardADDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
- (IBAction)addButton:(id)sender;
#end
SetupRingBoardADDViewController.m :
- (IBAction)addButton:(id)sender {
SetupRingBoardViewController *rbVC = [[SetupRingBoardViewController alloc] init];
[rbVC.listOfStudyHours addObject:#"A row!"];
NSLog(#"%#",rbVC.listOfStudyHours);
[[rbVC tableView] reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
}
(This is not the whole code, but it is the only thing that is relevant.)
The problem is that the method cellForRowAtIndexPath is not being called when I call the [tableView reloadData].
I hope you'll help me, I'm trying to figure it out for a long time :/
Please check if you have set/connected Delegate and Datasource to the File's Owner.
And check the array count of your model, if it contains value of not?
NSLog in the numberOfRowsInSection method and check it by using breakpoints and step over.
I think you need to make 2 changes. One, put the creation of the array in viewDidLoad instead of init (neither initWithStyle: nor init will be called if you created your table view controller in a storyboard -- initWithCoder: will be, so you could use that instead of viewDidLoad):
- (void)viewDidLoad
{
[super viewDidLoad];
if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
if(_listOfStudyHours) NSLog(#"%#",_listOfStudyHours);
[self.tableView reloadData];
}
Secondly, In your button method, you need to go back to the same instance that you came from, not create a new one. You can use the presentingViewController property to do that:
- (IBAction)addButton:(id)sender {
SetupRingBoardViewcontroller *rbVC = (SetupRingBoardViewcontroller *)self.presentingViewController;
[rbVC.listOfStudyHours addObject:#"A row!"];
NSLog(#"%#",rbVC.listOfStudyHours);
[[rbVC tableView] reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
}
Please bear with me as I am completely new at objective-c. Thanks in advance for any help you can provide!
So here is basically what I am trying to accomplish: I have 3 main tables whose contents will never change, that I therefore chose to construct in the mainstoryboard. Think of these are different grouping drilled down step by step into more and more details. So you have:
Table 1 (higher level to table 2) > Table 2 (higher level to table 3) > Table 3
Now I need to add a 4th table, but whose contents will be changed, based on a CSV file. For now I am ignoring how use CSV files and there seems to be quite a bit of info on this already. So I am electing to use Arrays using (NSArray) to store and retrive the information.
I first build the prototype of this table in the mainstoryboard so that I have an idea of what it will look like. Then I wrote the code below which ideally will update the information in table 4:
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#end
VIEWCONTROLLER.M file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSArray *nominalManagers;
NSArray *tipsManagers;
NSArray *tipsAmt;
NSArray *nominalAmt;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
NSUInteger rowNum;
if (section == 0) {
rowNum = 1;
}
else if (section == 1) {
rowNum = 4;
}
else {
rowNum = 0;
}
return rowNum;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSUInteger row = [indexPath row];
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
cell.textLabel.text = [tipsManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [tipsAmt objectAtIndex:[indexPath row]];
break;
case 1: // Second cell in section 1
cell.textLabel.text = [nominalManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [nominalAmt objectAtIndex:[indexPath row]];
break;
default:
cell.textLabel.text = #"WRONG SECTION";
break;
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
My main issue is that I am unable to connect the delegate and sourcedata of this class to Table 4 (the table whose information will change). Can you help? Do you have any suggestions to better accomplish my goal?
What I understand from your question is you do not have a uitableview to connect your data source and delegate !?
I am not sure if you have added 4th viewcontroller to your storyboard yet, If not just add a UItableviewcontroller to your storyboard, create a push segue then in identity inspector choose your VIEWCONTROLLER as class name.
Add IBOutlet UITableViewto your .h file like below and in your interface builder connect your datasource and delegate. When you need refresh your tableview call [self.tableviewname reloadData];
You can use NSMutableArray to edit the items in your array, if you use NSArray array items will be static.
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UITableView *fourthTable;
#end
VIEWCONTROLLER.M file
#implementation ViewController
#synthesize fourthTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
[self.fourthTable reloadData];
}
I am new to this so please bear with me:
I have a .xib in IB containing a UIScrollView that has a UITableView embedded in it. I would like to load custom UITableViewCells into the UITableView. I have four/five custom cells created in IB but cannot get the first to load correctly.
Here are relevant screenshots from IB:
I have set the class for the custom UITableViewCell created in IB to the UITableView class I created called "itemCell". Additionally, I have connected the three text fields outlets and delegates to "itemCell".
Here is itemCell.h:
#import <UIKit/UIKit.h>
#interface itemCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextField *quantityTextField;
#property (weak, nonatomic) IBOutlet UITextField *itemTextField;
#property (weak, nonatomic) IBOutlet UITextField *priceTextField;
#end
Here is itemCell.m:
#import "itemCell.h"
#implementation itemCell
#synthesize quantityTextField,itemTextField,priceTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
In filecontroller.m, which is the rootcontroller .xib's implementation file I create a new cell based on the user pressing a button (add it to an array) and then reload the tableview. I am getting no errors but I am getting a normal cell loaded in rather than what I have created in IB. Here is rootcontroller.m. Any help is appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView =(UIScrollView *)self.view;
items = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return [items objectAtIndex:[indexPath row]];
}
- (UITableViewCell *)initiateNewCell{
itemCell *cell = [[itemCell alloc] init];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
- (IBAction)addItem:(id)sender {
//creates the new cell to be added
[items addObject:[self initiateNewCell]];
[self.tableView reloadData];
As of iOS 5 you can now load a UITableViewCell from a nib for cell reuse with the following function.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
First make sure you create an IB file that contains only the UITableViewCell
Under Identify inspector: Set the class of the UITableViewCell to "itemCell"
Under attributes inspector: Set the identifier of the UITableViewCell to "itemCellIdentifier"
In viewDidLoad: place the following code, and replace itemCellNibName with the name of the nib that contains the UITableViewCell.
NSString *myIdentifier = #"itemCellIdentifier";
[self.tableView registerNib:[UINib nibWithNibName:#"itemCellNibName" bundle:nil] forCellReuseIdentifier:myIdentifier];
In cellForRowAtIndexPath: place the following code, you can now access your IBOutlet properties you set on the itemCell, and connected in the nib.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *myIdentifier = #"itemCellIdentifier";
itemCell *cell = (itemCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"Test Row:%i!",indexPath.row];
return cell;
}
Also: You do not want to include the UITableView inside a UIScrollView, a UIScrollView is already built into the UITableView, which allows for proper cell queing.
Also: I suggest you just get the bare bones of this going first, before you start pulling data from an array.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
Also: As someone else had mentioned, the array contains the data that will populate the UITableViewCells, not the actual cells themselves.
In your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//This identifier string needs to be set in cell of the TableViewController in the storyboard
static NSString *CellIdentifier = #"Cell";
// Resuse the cell
SomeCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If there are no cell to be reused we create a new one
if(cell == nil) {
cell = [SomeCustomCell new];
}
// Configure the cell...
NSString *someString = [yourArrayWithStringsOrWhatEver objectAtIndex:indexPath.row];
// Setting the some property of your custom cell
[cell.yourSpecificCellProperty someString];
[cell.yourOtherSpecificCellProperty setText:#"What Ever your want!"];
}
Your cell's properties could be whatever your have connected to your custom cell in interface builder.
Hope it helps!
Well there are a few things that aren't right:
The class name begins with upper case letter itemCell -> ItemCell
You should reuse the cell. The way you want to do this, you will create a separate cell for each index path.
the items array should only contain the data source of your table, not your cells
instead of calling reload data, you can add just one row to the table, and it can be animated too
Try something like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView =(UIScrollView *)self.view;
items = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:#"itemCell" owner:self options:nil].lastObject;
cell.reuseIdentifier = CellIdentifier;
}
// Do any additional setup to the cell
return cell;
}
- (IBAction)addItem:(id)sender {
[items addObject:[NSNumber numberWithInt:items.count]]; // Here comes the data source
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:items.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
PS: I wrote it without an IDE so there might be some errors
I have a first table view and a segue that pushes to another table view. I'm already able to push the second table view when the user touches a cell from the first table view, but I can't but data in the second table view. My second table view is using my custom controller which inherits from UITableViewController. I use the method "cellForRowAtIndexPath:(NSIndexPath *)indexPath" to put text in my cells, but the code never gets to that method. I don't understand what the problem is.
Here is my custom class:
#import "TableViewControllerTop.h"
#interface TableViewControllerTop ()
#end
#implementation TableViewControllerTop
#synthesize tableView;
#synthesize cells;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[self setTableView:nil];
[self setCells:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Top 50 list";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = #"Test";
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"selected");
}
#end
Thanks for your help. I can post more code if necessary.
Looks like your code is returning 0 for number of sections. So therefore it thinks there is nothing to show.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}
My goal is to have something like a "select" option in a HTML form, but now in my app. After doing research it's probably best to do this with a table view. I though of the picker view, but the fixed height is too big.
With the interface builder I simply placed a table view on my subclass of UIViewController.
How do I fill the Table view with options? I've seen many tutorials, but those are all for having a UITableView as their own class and filling up the entire screen. In my application this is just a small piece of the entire form.
What a nightmare to create a relatively simple thing like a table view. It either crashes or I get a table view that covers my entire view and that is not filled with anything.
The variable countryTable is connected to the object in the interface builder.
Frustrated after a hard day of work. Anyone got the complete working code? That would be great. I already had a great look at apple's explenation AND various tutorials, but I can't figure it out.
I've tried multiple things, but this is my current code:
#interface myView: UIViewController
{
NSArray *countryArray;
IBOutlet UITableView * countryTable;
}
#property (nonatomic, retain) UITableView *countryTabel;
#end
and in my .m file
#implementation myView
#synthesize countryTable;
- (void)loadView
{
self.countryTable.dataSource = self;
}
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc] initWithObjects:#"test1", #"test2",
#"test3",nil];
self.countryTable = array;
[array release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void) dealloc
{
[countryTable release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.countryTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [countryTable objectAtIndex:row];
return cell;
}
#end
You are on the right track. If you were to use a UITableViewController subclass, you would obviously have a full screen table view by default. Going the route of using a UIViewController subclass with a UITableView as a subview in the UIViewController's view is the right way to go. A few things that you will need to address are:
1) In the UIViewControllers header file you will need to add <UITableViewDatasource, UITableViewDelegate> as your view controller is responsible for fill implementing this functionally.
2) In viewDidLoad:, set self.contryTable.delegate = self; and self.countryTable.datasource = self;
The following protocol methods need to be implemented like so:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return countryArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [countryArray objectAtIndex:row];
return cell;
}
Hope this helps.