I have a viewcontroller called BNRItemViewcontroller
In the implementation:
- (instancetype)init{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++) {
[[BNRItemStore sharedStore]createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style{
return [self init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore] allItems] count];
}
I add a breakpoint in the cellForRowAtIndexPath: method,but it didn't get in the method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
cell.textLabel.text = [item description];
return cell;
}
Try this in cellForRowAtIndexPath to fix the crash issue and also check that the delegate properly set or not
static NSString *CellIdentifier = #"yourCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Thank you
Please make sure you have followed these steps:
Step 1: Conform to UITableViewDataSourceDelegate and UITableViewDelegate
Step 2: Set self.tableView.delegate = self and self.tableView.datasource = self
Please check that you have properly connected "Data Source" and "Delegate" of the tableview to the View Controller either from Code or from storyboard.
Please confirm
add Protocols in your BNRItemViewcontroller just like this : #interface BNRItemViewcontroller() <UITableViewDelegate,UITableViewDataSource>
if you use Storyboard or xib, you should confirm dataSource & delegate with the outlets of Storyboard/xib or write self.tableview.dataSource = self & self.tableview.delegate = self instead.
there are 2 ways
1.Please check that you have properly connected "Data Source" and "Delegate" of tableview
2. self.tableView.delegate = self & self.tableView.datasource = self
Make sure numberOfRowsInSection is not return 0;
- (instancetype)init{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
self.tableView.dataSource = self;
self.tableView.delegate = self;
for (int i = 0; i < 5; i++) {
[[BNRItemStore sharedStore]createItem];
}
}
return self;
}
EDIT
write this line in init method.
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"UITableViewCell"];
There can be seven reasons:
Like in accepted answer
Controller needs to conform to UITableViewDataSourceDelegate and UITableViewDelegate
You must assign delegates self.tableView.delegate = self and self.tableView.datasource = self
Futhermore:
numberOfSectionsInTableView must return number > 0 (1 if you want to have one main section)
numberOfRowsInSection must return number > 0
UITableView needs different frame than CGRectZero. You should't create UITableView in that way: self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
heightForRowAtIndexPath must return number > 0
Some other controller (in inheritance) can take delegate methods. Only if you have one tableView for more than one controller.
Related
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
}
I'm creating UITableViewController without a help from the Storyboard.
I've got the following code:
- (void)viewDidLoad {
[super viewDidLoad];
self.cellReuseIdentifier = #"myViewControllerCellReuseIdentifier";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellReuseIdentifier];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:self.cellReuseIdentifier];
}
}
In this case the cell style is not there. How do I fix that?
I think if you just add the UITableView, set your viewcontroller as the delegate and datasource and that's enough. If you set a breakpoint in cellForRowAtIndexPath, does it trigger?
Not sure what you're doing with,
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellReuseIdentifier];
Rather do this,
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
Let me know if that's not what you're looking for, good luck.
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.
I'm trying to add a UITableView to an existing UIViewController programmatically.
My run.h file has this:
#interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}
#property (strong, nonatomic) UITableView *tableView;
My app runs through a speed test and at the end of the test I'm trying to add a TableView to the View. At the moment I have this:
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"newCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Testing";
return cell;
}
The app never makes it into the cellForRowAtIndexPath method but does make it into the other two above. I can't see any error in the Xcode output.
Anything I need to be doing here:
When the app fails all I get is this:
If width of UITableView is zero, it will never call datasource
So change your code like this
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
Good luck!
You should init your UITableView with a frame (and optionally a style), or at least define the frame somewhere.
For example:
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
You need to assign the property tableView to the instance you are creating in the addSpeedTable method, then reference it using self.tableView.
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
Edit
As pointed out by the other answers, you'll probably need to set the frame on the UITableView as well:
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
You use init method to initialize a view, which means the view's frame is CGRectZero.
So the numberOfRowsInSection is called but SDK finds no cell need to be shown as the frame is CGRectZero.
-(void)addSpeedTable {
.....
tableView.frame = self.view.bounds;
[self.view addSubview:tableView];
}
What's more you should assign your tableView to the property by self.tableView = tableView or your tableView property will never be valid state.
You can add breakpoint at the thirdLine, and print out the frame see if one of its bounds is 0.
I think you may have declare the wrong syntax. I can't upload image, So notice the difference the code below. The capitalization may be the problem.
{
// 服务条款
static NSString * cellId = #"justCommon";
static NSString *CellIdentifier = #"newCell";
}
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;
}