iOS programmatic grouped table empty - ios

I have a programmatically implemented tableView, in the grouped style.
All I am getting is the gray pinstripes when it should be populated. So it is loading, but not ... something...
What more is necessary?
If no more, then where else ought I look?
Also, how can I make the background color of the table the same as the cell's white color?
- (void)loadView {
[super loadView];
UITableView *view = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
self.view = view;
[view reloadData];
}
Is viewDidLoad necessary?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Thank you,
Morkrom

You have to provide your tableView with data.
For staters you'll need to define a dataSource. Its common to just use your viewController as the dataSource.
// in the .h find something similar and add <UITableViewDataSource>
#interface ViewController : UIViewController <UITableViewDataSource>
then when you make the tableView.
view.datasource = self;
Then you'll need to provide the data itself. Implement these methods:
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#"A Cell"];
return cell;
}
Those methods will create 3 sections each with 3 rows. All the cells will just say A Cell. This is the basis for all tableViews. Just customize the data :)

You need to set dataSource and delegate properties for your table view so it will be able to pull data from them:
UITableView *view = ...
view.dataSource = self;
view.delegate = self;

have protocol in .h file and attach delegate and source with file owner

Related

Custom table view cells show up blank

I have a regular view controller (not a table view controller) and on that view I have a tableview. My table view cells are custom and I just made them through storyboards (didn't do anything in the code) but when I run my application, the tableview is blank. Any ideas as to why tho is happening to me? I have looked at other things on here but all of these other scenarios have to do with the person using an NSArray to fill out the tableview in the code, but mine is custom so I am not doing that. Thanks for any help. And before you mark this duplicate, please actually read this.
my code is as follows:
#interface TTViewController ()
{
NSArray *messageComponents;
}
#end
#implementation TTViewController
#synthesize dateTimePicker, messageSetupTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Message Setup";
messageComponents = [[NSArray alloc] initWithObjects:#"Recipient",#"Message", #"Date",#"haha", nil];
messageSetupTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
messageSetupTableView.alpha = 0.9;
}
#pragma mark -
#pragma mark Table view data source
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
return [messageComponents count];
}
-(NSInteger)tableView:(UITableView *) tableVew numberOfRowsInSection:(NSInteger)section{
return 1;
}
// 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];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
}
}
// Configure the cell.
cell.textLabel.text = [messageComponents objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[messageComponents objectAtIndex:indexPath.row]];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor whiteColor];
UIColor *selectedColor = [UIColor blueColor];
UIView *myBackgroundColor = [[UIView alloc] init];
[myBackgroundColor setBackgroundColor:selectedColor];
[cell setSelectedBackgroundView:myBackgroundColor];
return cell;
I want my table view to have a date picker in one section, a textview in the other, and a button and a few text fields in the other. Thanks
A couple of suggestions:
First make sure that UITableView's delegate and datasource are mapped or not either from IB or programmatically.
Put a break point on cellForRowAtIndexPath delegate function and see if it comes in this functions, and make sure you are using this function to create your custom cells.
Double check your have at least one row in your tableView using something similar:
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return 5; // for test pass value 5 so tableView should aware to display 5 rows.
}
Hope it helps!

UITableView not displaying content when its parent is not UITableViewController

In XCode 5, when you add a UITableViewController with more than 1 prototype cell which all contain labels etc., the content is displayed because the UITableViewController automatically sets the data source and delegate of the table view and sends a reloadData message to it.
iOS Developer Library - Basics of Table View Creation
But when you drag a UITableView to a UIViewController I believe you have to create instance methods similar to the default ones below in order for the content to be reloaded and made visible.
My question is how can I target a UITableView somewhere within a UIViewController and set its data source and delegate and have it reloaded and its content displayed properly?
Here is the default code for the UITableViewController:
- (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 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
Here is my code that works with UITableViewController but not in UIViewController for the UITableView that it contains:
...
#property (nonatomic, strong) NSArray *menuItems;
...
- (void) viewDidLoad
{
[super viewDidLoad];
self.menuItems = #[#"rowOne", #"rowTwo", #"rowThree", #"rowFour"];
}
...
- (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 [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
IN your code where u have created your cell if it not exists? And also make cell identifier as static.
You should create cell if it not exists in cellForRowAtINdexPath like as following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
//Create your cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Configure your cell
return cell;
}
You can set data source and delegate in a few ways. If you add table view to view controller in storyboard just control drag from table view to view controller and select delegate and do it again and select data source.
You can also create outlet and in your view controller in viewDidLoad do it by:
self.tableView.dataSource = self;
self.tableView.delegate = self;
The other way is create table view in code and add it to your view controller view. You can do it in viewDidLoad like that:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(TABLE_VIEW_FRAME) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
If you want to reload the data just call:
[self.tableView reloadData];
Hope this help.
Make sure you implement UITableViewDelegate and UITableViewDataSource protocols and set these in the initWithNibName or initWithCoder initializers.
This is how your .h file should look like:
#interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Here is what you need to do in the .m file:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
if you are using storyboards, please use initWithCode as given below:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
You are only dequeuing the cell but you are not creating one if dequeueReusableCellWithIdentifier returns nil. You should implement in following way.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// here you can configure the cell
return cell

Trying to add a Tableview inside of a UIView. What am I doing wrong here?

I'm not sure how to approach this. I'm loading a separate table view controller nib file to my viewcontroller. How do I position it the way I want? Also, Is the below query all I need, or am I missing something? It keeps crashing on me.
- (void)viewDidLoad
{
[super viewDidLoad];
HSTableViewController *tableViews = [[HSTableViewController alloc]initWithNibName:#"HSTableViewController" bundle:nil];
[self addChildViewController:tableViews];
tableViews.view.frame = CGRectMake(0, 0, 100, 100);
//tableViews.view.frame = self.view.bounds;
[self.view addSubview:tableViews.view];
[tableViews didMoveToParentViewController:self];
To add a tableview in code to a view controller wherein that VC will act as both the data source and delegate you would do the following.
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = #"Hello World";
return cell;
}
And then in your .H file you need to inform the program that you're going to act as both the Delegate and DataSource
#interface NSSViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
From what I gathered out of your question, that should get you where you need to go. I will edit accordingly if you require further assistance.
Set your UITableView's frame in -viewWillAppear: like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
tableViews.view.frame = self.view.bounds;
}
If anyone else needs this. See this tutorial I found on youtube that helped a lot. Thanks for all the help.
http://www.youtube.com/watch?v=DzedmcFlm1c

Set delegate to an object

This is the first time im trying to set a UITableView delegate/datasource to an instance of an object.
I have a UIView managed by a class called hMain, and a UITableView inside of main managed by an instance of a class called vTable.
hMain.h:
#interface hMain : UIViewController
#property (strong, nonatomic) IBOutlet vTable *voteTbl;
#end
hMain.m:
- (void)viewDidLoad
{
[super viewDidLoad];
voteTbl = [[vTable alloc]init];
[self.voteTbl setDelegate:voteTbl];
[self.voteTbl setDataSource:voteTbl];
}
vTable.h:
#interface vTable : UITableView <UITableViewDelegate , UITableViewDataSource>
#end
vTable.M:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [UITableViewCell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor] reuseIdentifier:CellIdentifier inTableView:(UITableView *)tableView];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor]];
}
cell.textLabel.text = #"Hello there!";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row pressed!!");
}
This is really my first time straying away from IB and doing things programatically so im not sure im settings delegates and things correctly. This is also my first attempt and setting a delegate/datasource outside of self.
My problem is the table is coming out blank every time.
What is vTable? Looks like you have a "voteTable" class (BTW: classes should start with an uppercase char, instance variables should start with lowercase). Anyhow, looks like your main problem is you forgot to add the table as a subview and set its frame. eg:
self.voteTbl = [[VoteTable alloc] init];
self.voteTbl.delegate = self;
self.voteTbl.dataSource = self;
self.voteTbl.frame = self.view.bounds;
[self.view addSubView:self.voteTbl];

My table view doesn't populate

I'm using this tutorial on how to create a pop out menu.
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
I got it working when going through the tutorial, now i'm trying to implement it into my app.
I'm at the stage where i can press the menu button and the popout menu appears. The problem is, it doesn't populate itself with my table view cells.
I set the identifiers for each table view cell and then in the code reference them to full an array.
I know when woking through the tutorial, if I misspelled one of the identifiers when defining what's in the array, the program would crash. In my app, that's not the case. Hopefully that can help pin down the problem. It doesn't even change the colours which is the first part of the code.
Here's the code.
#import "SidebarViewController.h"
#import "SWRevealViewController.h"
#interface SidebarViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation SidebarViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
_menuItems = #[#"markup",#"tax"];
}
- (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.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Any help would be great.
Thanks
All you're doing is creating an array with two string literals. You must set the textlabel's text property in the following method to display the strings in 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];
}
cell.textLabel.text = [_menuItems objectAtIndexPath:indexPath.row];
return cell;
}
OMG I feel like such a noob. It turns out the ViewControler didn't have a target, hence why it wasn't responding.
I'm so sorry for wasting your time, I really feel bad now.

Resources