UITableView data is not showing - ios

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.

Related

Create a table on objective-c code without storyboard

I can't create table on objective-c, cause I start learning objective-c.
Google can't help me, I tried.
I hope what u can help me
//Update: Below Nick showed the correct answer. After some time, I was prompted to answer this question and now I can share with you the answer. I want to be useful for such guys as I was
- (void)viewDidLoad
{
[super viewDidLoad];
//Here we initialize the table, create delegates and display it on the screen
arrData=[[NSArray alloc]initWithObjects:#"ONE",#"TWO",,#"THREE", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Cells initialization
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
return cell;
}
For default tableview cell with array:
- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSArray alloc]initWithObjects:#"ONE",#"TWO",,#"THREE", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
return cell;
}
IF YOU ARE USING XIB
You have to create tablviewcell file and design your own cell.
Import your cell file too..
#import "selectedcell.h"
- (void)viewDidLoad {
[super viewDidLoad];
array =[[NSArray alloc]initWithObjects:#"AAAA",#"BBBB",,#"CCCC", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.tablview registerNib:[UINib nibWithNibName:#"SelectedTableViewCell"
bundle:nil] forCellReuseIdentifier:#"cellidentifier"];
self.tablview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SelectedTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:#"cellidentifier" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.label.text = [Nsstring stringwithformate:#"%#",[array objectatindex:indexpath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//After selecting row
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

Objective - C - UITableView cellForRowAtIndexPath never called

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
}

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;
}

Load Array in UITableView Cell

I want to make paging swipe left and right using UIScrollView after view detailController.
First, main.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OZDetailViewController *detailViewController = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
detailViewController.arrDetailNews = [_arrNews objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
OZDetailViewController *arrNewsAll = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
arrNewsAll.allNewsArray = _arrNews;
[self.navigationController pushViewController:arrNewsAll animated:YES];
}
When I selected content in tableviewcell, arrDetailNews can loaded in method viewDidLoad() and cellForRowAtIndexPath(). But arrNewsAll cannot loaded in method cellForRowAtIndexPath().
This is my detailViewController.h:
#property (nonatomic, copy) NSArray *allNewsArray;
And detailViewCOntroller.m:
#synthesize allNewsArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"dataArray: %#", allNewsArray);
}
- (int)numberINSectionsInTableView: (UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"dataArrayCell: %#", allNewsArray);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"New Cell"];
}
if(indexPath.row != self.arrDetailNews.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor whiteColor];
[cell addSubview:line];
}
tableView.allowsSelection = NO;
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
if (indexPath.row==0) {
cell.textLabel.text = #"1st";
}
if (indexPath.row==1) {
cell.textLabel.text = #"2nd";
}
if (indexPath.row==2) {
cell.textLabel.text = #"3rd";
}
return cell;
}
If allNewsArray can loaded in cellForRowAtIndexPath() I can continue next step for paging with UIScrollView. Note, numberOfRowsInSection I set to 4 because I need 4 rows (custom view).
Assuming you setup your delegate & dataSource outlets correctly from xib/storyboard, you still need to specify the number of rows per section (or number of sections).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return allNewsArray.count;
}
Alternatively, the method for number of sections is:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

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