Objective - C - UITableView cellForRowAtIndexPath never called - ios

I know that this question have been asked so many times but unfortunately non of the solutions in those questions worked for me.
Here are some of the things I tried:
making sure that numberOfSectionsInTableView is not returning 0
making sure that numberOfRowsInSection is not returning 0.
insuring that reloadData is being called on the main thread by using performSelectorOnMainThread as shown below:
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]
setting the delegate :
self.tableView.dataSource = self; self.tableView.delegate = self;
making sure that tableview background color was changed.
making sure that tableview frame was adjustable.
But cellForRowAtIndexPath method never called
Please find the code:
UITableView *tableView;
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (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 == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = "test";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
If you have any idea what to do, please let me know.
Thanks

You created the tableView added it as subView to your ViewController's view in ViewDidLoad() which is fine :) But where are you calling reload Data on tableView buddy :)
Add
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
[self.tableView reloadData];
}
It will work like a charm :)

just uninstall app from simulator and run again. sometimes its not work.

you have to delegate and datasource Protocol to view controller
#interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

First
#interface yourController ()<UITableViewDelegate,UITableViewDataSource>
Second
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
[self reloadData]; //call your webservice here
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
[self.tableView reloadData]; //most imp
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return yourArray.count; //your array which is get from webservice
}

Related

Create TableView in some UIView, Objective c

In my ViewController I have a little UIView. How do I programmatically create a TableView instead of this "blue" UIView.
I have this code for my UIView.
self.viewForTableView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2,
self.navigationController.navigationBar.frame.size.height,
self.view.frame.size.width/2,
self.view.frame.size.height)];
self.viewForTableView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.viewForTableView];
That's how I would like to have it preview.
Swift
let tblVW:UITableView = UITableView(frame: viewForTableView.frame)
viewForTableView.addSubview(tblVW)
tblVW.delegate = self
tblVW.dataSource = self
Objective-c
UITableView *tblView = [[UITableView alloc]initWithFrame:viewForTableView.frame];
[viewForTableView addSubview:tblView];
tblView.delegate = self;
tblView.dataSource = self;
you need to implement following methods:
#pragma mark - Table View Data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 0//number rows you want in table
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //by default is 1
}
viewcontroller.h
#interface yourViewController ()<UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *tbleView;
}
Viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
tbleView.delegate=self;//for delegate methods
tbleView.datasource=self;//for datasource methods
tbleView.backgroundColor=//yourColor
tbleView.frame=CGRectMake(//your frame);
[yourView addSubview tbleView];
}

TableViewDatasource and delegate not called when tableview is inside collectionview cell

I have placed a uitableview inside collectionviewcell and have coded as below in the collectionviewcell class.But the datasource and delegate methods are not being called can any help me out to fix the issue
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
arrayMenuAlerts=[NSMutableArray arrayWithObjects:#"Suri",#"John",#"Phillip",#"Sachin", nil];
[self.contentView addSubview:tableView];
tableView.dataSource=self;
tableView.delegate=self;
}
return self;
}
- (void)awakeFromNib {
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
tableView.dataSource=self;
tableView.delegate=self;
}
This happens because on the init Method the object "self" is not created yet. you can put tableView dataSource and Delegate in another method. like i did.
#synthesize tableview;
NSMutableArray *arrayMenuAlerts;
- (void)drawRect:(CGRect)rect{
arrayMenuAlerts = [NSMutableArray arrayWithObjects:#"Suri",#"John",#"Phillip",#"Sachin", nil];
tableview.dataSource = self;
tableview.delegate = self;
[tableview reloadData];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
return [super initWithCoder:aDecoder];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrayMenuAlerts count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = arrayMenuAlerts[indexPath.row];
return cell;
}

UITableView data is not showing

I created a custom class ("customClass") that is a subclass of UIView. I then added a tableView to it's subview, and created an array for the datasoucre. I set the delegate and datasource to self. Then in the storyboard, I set a UIViews class to customClass.
Then in the mainVC class, I set an array to some strings and set that array to the array in the customClass. When I run the app, it doesn't crash or give me any errors, but I don't see any results in the tableView
Here's how it's setup in mainVC.m:
self.myArray = [[NSArray alloc] initWithObjects:#"First", #"Second", #"Third", #"Fourth", #"Fith", #"Sixths", #"Seventh", nil];
self.myView.resultArray = self.myArray;
[self.myView.tableView reloadData];
Here is the code in customClass:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setupTableView];
}
return self;
}
- (void)setupTableView
{
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
[self addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.resultArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
[cell.textLabel setText:self.resultArray [indexPath.row]];
return cell;
}
When I run the app, I don't see any results in the tableView although I do see a tableView shown. What am I doing wrong, and how can I fix it?
Edit
Just realized, cellForRow doesn't even get called! I did a reloadData, and it still didn't get called.
initWithFrame: - It is recommended that you implement this method. You
can also implement custom initialization methods in addition to, or
instead of, this method.
initWithCoder: - Implement this method if you load your view from an
Interface Builder nib file and your view requires custom
initialization.
initWithCoder is called much before init and viewDidLoad methods
In MainVC
viewClass *vc = [[viewClass alloc] initWithFrame:self.view.frame];
vc.resultArray = [[NSArray alloc] initWithObjects:#"First", #"Second", #"Third", #"Fourth", #"Fith", #"Sixths", #"Seventh", nil];
[vc.tableView reloadData];
[self.view addSubview:vc];
In Custom Class...
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupTableView];
}
return self;
}
- (void)setupTableView
{
self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
[self addSubview:self.tableView]; // Datasource and delegate after alloc init
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.resultArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
[cell.textLabel setText:self.resultArray [indexPath.row]];
return cell;
}
Register Cell when using Custom UItableviewCell, Otherwise don't use [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];, it will crash.
You can set customClass's backgroundColor to red,and then check the customClass is added on the mainVC's view.

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

UITableView for iOS

I am very new to Objective-C. I just searched and tried to learn about UITableView.
I have created this UITableView. instead of "a" in all the rows i want it to be in a sequence like "a b c d..." and if i increase the no of rows it should scroll. its not scrolling so here is my code.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
cell.textLabel.text = #"a";
return cell;
}
#end
set table view Delegates in IB or if u created Programmatically then in viewDidLoad Take one array fill it Dynamically or Statically.Give array count in NumberOfRowsInsection method and reload table view at appropriate place
in your cellForRowAtIndexPath: add these lines
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
Check this tutorial, I'm sure it will help you
UItableView Guide
and for scrolling, You don't need to do anything yourselves, as the number of cells increases beyond the provided size, it automatically becomes scrollable and make sure that scrolling is enabled in your UITableView attribute inspector, check the image
Happy Coding
Regards
Use below code.....define alphabetarr in globally then use it in code...
- (void)viewDidLoad
{
alphabetarr = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k",#"l",#"m",#"n",#"o",#"p",#"q",#"r",#"s",#"t",#"u",#"v",#"w",#"x",#"y",#"z", nil];
CGRect frame = self.view.frame;
UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
tableview.backgroundColor = [UIColor cyanColor];
tableview.separatorColor = [UIColor blackColor];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];
return cell;
}

Resources