UITabBarController with UITableView - iphone

i am trying to display a list on my first view so added this in the first.h :
#import <UIKit/UIKit.h>
#interface argospineFirstViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *Journals;
IBOutlet UITableView *myTableView;
}
#property (nonatomic,retain) NSMutableArray *Journals;
#property (nonatomic, retain) UITableView *myTableView;
#end
and then i added this on my first.m :
#implementation argospineFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Journals=[NSMutableArray arrayWithObjects:#"journal1",#"journal2",nil];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.text=[Journals objectAtIndex:indexPath.row];
return cell;
}
I am newbie so i don't really know what kind of connections i have got to make, i am also using a storyboard with a tableview dropped on the first view.
is there something i have to add to the delegate?
Any help?
Thank you for your time

Right click on the tableView in the storyboard. You will see "delegate" and "dataSource" under Outlets. Drag the bubble on the right of those to the view controller icon at the bottom of the view. This will make your viewcontroller the delegate and datasource for the table view if you don't want to do it programmatically.

Do not make property of your table view object.
Also,
in viewDidLoad method write:
myTableView.dataSource = self;
myTableView.delegate = self;
Tell me if it helps!

Use initWithStyle instead of initWithFrame for creating your cell.
In your storyboard, select your table view and open the Connections Inspector. Make sure that the delegate and datasource connections are linked to your argospineFirstViewController object.

in IBOutlet set delegete and datasource of the tableview to filesOwner

you use cell.text for show the data i think its not work in tableview just try this line:-
cell.textlabel.text=[yourArrayname objectatindex:index.row];
no need to connect delegate you already define in protocol.

Related

Label in CustomCell stays nil

I created a custom UITableViewCell with an UILabel in it.
In the cellForRowAtIndexPath method I initialise the custom cell and give the UILabel a value.
While the custom cell is loaded (the heights of the cells are higher than default cells), I can't seem to give the UILabel a value.
SBDownloadCell.h
#import <UIKit/UIKit.h>
#interface SBDownloadCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UIProgressView *progressbar;
#property (weak, nonatomic) IBOutlet UILabel *details;
- (IBAction)pause:(id)sender;
#end
SBDownloadCell.m
#import "SBDownloadCell.h"
#implementation SBDownloadCell
- (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
}
- (IBAction)pause:(id)sender {
}
#end
SBViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SBDownload";
SBDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[SBDownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
SBDownload *dwnld = [[SBDownload alloc] init];
dwnld = [SBdownloads objectAtIndex:indexPath.row];
//cell.title.text = [dwnld name];
cell.title.text = #"Test";
cell.progressbar.progress = [dwnld percentDone];
cell.details.text = [NSString stringWithFormat:#"%f MB of %f MB completed, %#", [dwnld completed], [dwnld length], [dwnld eta]];
return cell;
}
Storyboard
I break just after cell.title.text = #"Test"; and still this is what I see:
What could it be?
note: i use Xcode DP-5 with iOS7
I see your properties are marked with IBOutlet, which means you have an Interface Builder file (either xib or storyboard) with your table view cell. Make sure to give the correct cell identifier in Interface Builder to the prototype cell in your table view. You should not be calling initWithStyle:. If dequeueReusableCellWithIdentifier: returns nil when using a UITableViewController and storyboards, this means incorrect setup, as dequeueReusableCellWithIdentifier: should always return a cell (it creates the new one if it has to).
To elaborate a bit further, when using xibs or storyboards, a table view cell's initWithStyle: will never be called. When a nib is loaded, the correct init method is initWithCoder:.
The problem is in static NSString *simpleTableIdentifier = #"SBDownload";. In your storyboard, you have set up the identifier as SBDownloadCell.
You need to allocate your UILabels and UIProgressView. Right now you set properties for them, but in your -initWithStyle method you need to call things like
self.title = [[UILabel alloc] initWithFrame:etc...];
If you do this for each of your properties on SBDownloadCell, the labels should be allocated properly.
If you custom your prototype cells in Storyboard, you need to present your TableViewController with segue in storyboard, or if you you need to present the TableViewController by programming, you need to use instantiateViewControllerWithIdentifier: method,
- (IBAction)showTableView:(id)sender
{
UIStoryboard *storybaord = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
LJTableViewController *vc = (LJTableViewController*)[storybaord
instantiateViewControllerWithIdentifier:#"TableViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];
}
if you initiate TableViewController with [TableViewContrller new], this will let you get an nil label in tableView:cellForRowAtIndexPath mehtod.
I encounter same problem but my identifier is correct for creating custom cell and show data in IBOutlets custom lable, but after cell allocation lables values are null. So for this I need to add method of tableview for showing lables values
- (void)tableView:(UITableView *)tableView willDisplayCell:(DropViewTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.lblHSNCode.text = objClass.HSNCode;
}

Switch from UIView to UITableview on press of a button

I made a button and connected it to an action in interface builder. What do I need to do in the action method to switch from displaying a view to displaying a table view?
Here is some of my code:
// SwitchToTableViewController.h
#import <UIKit/UIKit.h>
#interface SwitchToTableViewController : UIViewController {
}
-(IBAction) switch : (id) sender;
#end
and
//SwitchToTableViewController.m
#import "SwitchToTableViewController.h"
#import "Table.h"
#implementation SwitchToTableViewController
-(IBAction) switch : (id) sender{
// what is i need to write here to switch to tableview
}
#end
and
//table.m
#import <UIKit/UIKit.h>
#interface Table : UITableViewController {
}
#end
and
//table.h
#import "Table.h"
#implementation Table
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Set up the cell...
return cell;
}
#end
You need:
Create an instant of Table.
Pass any data over to this instant, most likely an array so that you can use it to display cells data. You need to create some iVars in your Table class first. Currently you don't have any.
Chose one of many ways to present your Table instant.
Look into Apple's doc. It has quite a bit sample code.
Edit for sample code:
In your SwitchToTableViewController.h file, add this line:
#import "Table.h"
In your SwitchToTableViewController.m, make this changes
-(IBAction) switch : (id) sender{
    // what is i need to write here to switch to tableview
Table *myTable = [[Table alloc]init];
myTable.cellArray = [NSArray arrayWithObjects:#"Item1", #"Item2", #"Item3", nil]; //only an example to be displayed
[self presentViewController:myTable animated:YES completion:nil]; // present your Table - view controller
}
Your Table.h file should look like this:
#import <UIKit/UIKit.h>
#interface Table : UITableViewController {
}
#property (nonatomic, strong) NSArray *cellArray; // adding this line to get data from the parent
#end
In your Table.m, make this changes
#implementation Table
#synthesize cellArray; //add this line right after #implementation Table
- (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 cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
}
return cell;
}
You have two view controllers here, not two views. It's very important to realise the difference between those two concepts. I've put in bold in the answer below some other key words you need to read up on.
To show another view controller in response to something like a button press, you need to create a new instance of your destination view controller, then get it on the screen somehow.
There are many ways to do this but the simplest way, for a beginner, would be to do all this in storyboards. Add both of your view controllers to the storyboard.
Your initial view controller (with the button) should be embedded in a UINavigationController. Control-drag from the button to your table view controller. This will create a segue. Choose "push" from the list of options.
Hey presto, your table view will now appear on screen when you press the button.
First Create a SingleView Application (UIViewController based) name it as SwitchingView.
Then add a UITableView above the view. Connect using Interface builder. Set delegate and datasource of tableview as the SwitchingView.
Also add a button on the view (before adding table view). Set table view is Hidden using IB(interface builder).
In button Action do
tableview.hidden = NO;
This will show your Table view!!! If you want to show view again, just make
tableview.hidden = YES;
in tableView:didSelectForIndexPathRow method.

table view does not display data,iphone

I am working with UITableView now and what I an having is
MyClass.h
#interface MyClass : UIViewController {
}
#property (strong , strong) UILabel *name;
#property (strong , strong) UILabel *add;
and
MyClass.m
-(NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section{
return 3 ;
}
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
tableCell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
...................................................
return tableCell;
}
When I put i break point at numberOfRowsInSection, it seems that it is not being executed at all.
It is confusing me now.
Please advice if you have encountered it before.
Thanks
The most common cause of this is not setting your view controller as the table view delegate and datasource. If it's built in storyboard, you can do this by selecting the connections inspector on the table and dragging those properties to the view controller.
Otherwise, set it in code like this:
self.tableView.delegate = self;
self.tableView.datasource = self;

UITableView Custom Cell Throwing SIGABRT Error

I am trying to create a custom cell for my UITableView. I am using Xcode 4.2 and using the storyboard along with ARC.
I have created a class to represent the custom cell like so:
ResultsCustomCell.h
#import <UIKit/UIKit.h>
#interface ResultsCustomCell : UITableViewCell
{
IBOutlet UILabel *customLabel;
}
#property (nonatomic, retain) IBOutlet UILabel* customLabel;
#end
ResultsCustomCell.m
#import "ResultsCustomCell.h"
#implementation ResultsCustomCell
#synthesize customLabel;
#end
I have then implemented the UITableView method in my view controller as follows:
ViewController.m
#import "ViewController.h"
#import "ResultsCustomCell.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
myCell.customLabel.text = #"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
#end
The application builds successfully but then crashes instantly and give me the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
What part am I missing out?
This is an irritating problem because the debugger gives you no clue as to what happened. It will break on the exception and then simply shut down if you click Continue Execution.
This drove me nuts for 30 minutes until I right clicked on a UILabel in the custom cell that was giving me problems, which revealed the answer: One of the controls in your cell has (or had, as you apparently fixed it, perhaps without noticing) a spurious outlet. By "spurious outlet" I mean one that is wired to an IBOutlet property that no longer exists in your class. In my case I had changed the name of the property and rewired it, but had forgotten to remove the old referencing outlet connection.
As soon as I removed the offending outlet the problem went away.
You forgot to create a new UITableViewCell when it's not dequeued
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil) {
myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
myCell.customLabel.text = #"helloWorld";
return myCell;
}
You have forgotten to add the reuse identifier ("CellIdentifier") in the storyboard against your prototype cell, so when it tries to create a new cell, there is no prototype with that identifier in the storyboard, and it returns nil.
#property (nonatomic, retain) IBOutlet UILabel* customLabel;
Automatic Reference Counting (ARC) forbids the explicit call of retain. Try to remove this.
As for the error, you do return a UITableViewCell, not your custom cell. Additionally, you never allocate your ResultsCustomCell.
- (ResultsCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *cell = (ResultsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ResultsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.textLabel.text = #"Text";
return cell;
}
Furthermore, your subclass of UITableViewCell does not declare the (obviously obligatory) method init.
ResultsCustomCell.h:
#import <UIKit/UIKit.h>
#interface ResultsCustomCell : UITableViewCell
#property (strong, nonatomic) UILabel *myLabel;
#end
ResultsCustomCell.m:
#import "ResultsCustomCell.h"
#implementation ResultsCustomCell
#synthesize myLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
#end
EDIT: I saw, that you are using storyboard. My suggestion might or might not be of help to you. I have never used storyboard.
I'm not entirely sure what I have done to resolve my problem but It is now working correctly.
Here is what I currently have in my ViewController.m
Note: The ResultsCustomCell.h and .m are the same as above.
#import "ViewController.h"
#import "ResultsCustomCell.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
// Make sure there are no quotations (") around the cell Identifier.
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
myCell.customLabel.text = #"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
#end
I then made sure the following things where correct:
The table view cell has the correct custom class (ResultsCustomCell).
The cell Identifier for the UITableViewCell was correct in Interface Builder.
The UILabel in interface builder was correctly linked to the IBOutlet.
The UITableView view had the correct controller (ViewController)
After using this code and checking all of the above it appears to work.
I was also fighting this error with the same setup as yours. I realized that it, for me, was caused by the new "Auto layout" feature of iOS6. To disable it I opened the custom cell xib, and then on the right hand side Utilities I opened the File inspector tab (the tab to the far left). In there I could un-check "Use Autolayout".
I created this exception by mistakenly registering the identifier with the wrong class type.
//The Identifier is register with the wrong class type
self.listView.register(CustomCellClass1.self, forCellReuseIdentifier: "CustomCellClass2")
And then dequeuing and casting to a different class type:
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellClass2", for: indexPath) as! CustomCellClass2

Add an external TableView inside a small view on the Main Screen

I have a simply MainWindow file created by the Interface Builder in black background. Inside this plain view, I've added from top to bottom a small view (300x100), a textLabel and a couple of buttons.
In the MainApplication.h I've defined:
IBOutlet RoundRectView* smallView;
IBOutlet UILabel* label;
In the MainApplication.h I've #synthesized these variables and with that:
[label setText:#"Test"];
I can see all in my screen and it works perfect.
Now my problem:
I want to add a table with two rows inside my small white view, and what I've done is:
Create a tableController.h who's the delegate and the data source:
#interface IncomeExpenseController :
UITableViewController
{ IBOutlet
UITableView* smallTable; NSArray
*content; }
#property (nonatomic, retain)
UITableView *smallTable; #property
(nonatomic, retain) NSArray *content;
#end
Create the .m file with:
#synthesize smallTable; #synthesize
content;
(void)viewDidLoad {
[super viewDidLoad]; content = [[NSArray alloc]
initWithObjects:#"Item1", #"Item2",
nil]; }
(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
// Return the number of sections.
return 0; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2; }
// Customize the appearance of table
view cells.
- (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]
autorelease];
} NSLog(#"inside cell");
// Configure the cell... cell.textLabel.text = [content
objectAtIndex:indexPath.row];
return cell; }
Now in the Interface Builder, I drag and drop a UITableView inside the white small view and drag and drop a Table View Controller along with the File Owner's window and I do these connections:
Class of the Table View Controller to tableController
Outlets:
DataSource and Delegate to my Table Controller
Referencing Outlets:
customTable and View to my Table Controllers
When I execute, I can see the lines of the table view but with no content, I cannot see the NSLog either but if I put a NSLog inside the viewDidLoad I can see it.
I'm really new (2 days) on that and any help would be really appreciated.
Thanks!
Solved.
First of all, the TableView is grouped so I must return almost one section.
Second, from the Interface Builder, I had to link the "view" from the Table to my external controller.
Now it works perfectly.