I a UITableView called tableView. It's data array called namesArray.
I have a function that adds a name to the array that looks like this:
-(void)addName:(NSString*)name
{
[self.namesArray addObject: name];
[self.tableView reloadData];
}
After I call reloadData on tableView, the last cell (the one that was added) is not showing on tableView, numberOfRowsInSection return the actual number so there is a space for another cell but there is not an actual cell.
I was debugging cellForRowAtIndexPath and I was found out that when cellForRowAtIndexPath called for the new cell, dequeueReusableCellWithIdentifier returns nil while when it called to the other cells (except when indexPath.row == 0 of course) it returns a cell.
The code for cellForRowAtIndexPath:
-(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.text=[self.namesArray objectAtIndex:indexPath.row];
return cell;
}
numberOfRows:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.namesArray.count;
}
Note: if I try to print the last object of namesArray using NSLog it's looking fine (the last object is the new one that was created) so it's a problem with reloading the data of tableView
Can you please help me? Thanks!
check the number of rows you returns in numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
it should be something like:
[self.namesArray count];
First dataSource and delegate the tableview. after that make sure using below method with perfection
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [your array count];
}
-(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.text=[self.namesArray objectAtIndex:indexPath.row];
return cell;
}
After that reload tableView with
[_tableViewName reloadData];
-(void)addName:(NSString*)name
{
[self.namesArray addObject: name];
[self.tableView reloadData];
}
If you are calling this function again and again to fill the array then, do not reload tableview in this method. Because, its getting reloaded overtime you call this method and cellForRowAtIndexPath gets called every time. So, after your array gets filled completely then, reload the tableview
.h
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
IBOutlet UITextField *txtName;
IBOutlet UITableView *tableObject;
NSMutableArray *namesArray;
}
- (IBAction)btnAdd:(UIButton *)sender;
.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
namesArray = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return namesArray.count;
}
- (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.text=[namesArray objectAtIndex:indexPath.row];
return cell;
}
-(void)addName:(NSString*)name
{
[namesArray addObject: name];
[tableObject reloadData];
}
- (IBAction)btnAdd:(UIButton *)sender {
[self addName:txtName.text];
}
Related
Please if you are going to add the link of the documentation, please don't do that
I have a problem her and i don't know how to solve it and need a clear answer
I have build a table view from xcode UI and add 5 sections each section contains some cells except section2, what I want to do is to add cells to section2 in viewDidLoaded is that possible or not
this is the header file
#interface Menu : UITableViewController;
this is the implementation file
#implementation Menu
#synthesize drawerTableView;
- (void) viewDidLoad {
NSDictionary *object = [drawerTableView dataSource];
KhawaterDataManager *sharedManager = [KhawaterDataManager instance];
CategoriesResponse *categories = [sharedManager categories];
[categories printObject];
}
/*- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"%d", section);
switch (section) {
case 1:
return 5;
break;
}
return [tableView.dataSource[section] count];
}*/
/*- (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 setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
cell.textLabel.text = #"hello";
return cell;
}*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
// my logic
}
#end
I think you are using static cells in your UITableView. What you can do is make use of dynamic tableview cells. You have to uncomment and use both methods to create your cell:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
I am relative new to iOS development. Right now, I created 2 ViewController using storyboard. The one consist one button that lead into another controller using segue (show).
This controller is TableViewController that embedded in Navigation Controller and already connected with its responsible class that inherit from UITableViewController.
My problem is this page doesn't load its data (NSMutableArray) that already initialized in viewDidLoad using
_dataClient = [NSMutableArray arrayWithObjects:#"First City",#"Second City",#"Third City",#"Forth City",#"Fift City", nil];
This is my table delegate and datasource:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataClient count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"cellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_dataClient objectAtIndex:indexPath.row];
return cell;
}
Is there any step that I forgot to do?
You must return 1 section at least. Change this line:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; /// here
}
I am new to iOS and I am trying to build an lightweight app for practice. I am taking two textfield entries from one view controller and storing them in NSDictionary. I am transferring those data in NSArray. Now I want to display those data from NSArray to table view. I am using this line of code:
cell.textLabel.text=[self.myArray objectAtIndex:indexPath.row];
But my app is crashing when cursor comes at this line. Help is appreciated.Thanks in advance.
For UITableView you have to define at least 2 methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It seems your method numberOfRowsInSection is not implemented.
Try to add this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
Of course your ViewController have to be delegated for UITableViewDataSource and UItableViewDelegate.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.yourArry count];
}
-(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];
}
//Configure Cell
cell.textLabel.text = self.yourArry[indexPath.row];
return cell;
}
Please try this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[self.myArray objectAtIndex:indexPath.row]
return cell;
}
Trying using a pointer to an NSString (or what object you are trying to make)
NSString *string = [self.myArray objectAtIndex:indexPath.row]
Make sure you have the count of cell in the tableview set like so
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return self.myArray.count
}
Step One.
In YourViewController.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
In YourViewController.m
#import "YourViewController.h"
#interface YourViewController ()
{
UITableView * YourTableview;
}
- (void)viewDidLoad
{
YourTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 300, 500) style:UITableViewStylePlain];
YourTableview.delegate=self;
YourTableview.dataSource=self;
[self.view addSubview:YourTableview];
}
-(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=#"Cellid";
UITableViewCell * SoftCell=[YourTableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (!SoftCell)
{
SoftCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
return SoftCell;
}
I don't know what I am doing wrong but the cellForRowAtIndexPath method of table view is getting called only once.
Here is my code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"--->%d",[collaboratoresdata count]);
return [collaboratoresdata count];
}
When my collaboratoresdata array is filled, then I am calling [_collaboratortblView reloadData] to reload the table view.
I am successfully getting 3 in NSLog(#"--->%d",[collaboratoresdata count]); but cellForRowAtIndexPath is getting called only one time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
NSLog(#"tableView:%d",[indexPath row]);// i am getting 0 here than after its not calling
return cell;
}
check the height of cell i think thats the issue
please try to set height of cell manually(custom). So, if problem of cell height then it will be resolved. below method set cell height 30 pixels.
#pragma mark- TableView delegate Methods
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}
I'm trying to populate a grouped UITableView using an NSMutableArray. I want each element in the array to have its own section. i.e : One element (one row) per one section.
This is my code I have written so far.
#import "MailViewController.h"
#interface MailViewController ()
#end
#implementation MailViewController
NSMutableArray *mailboxes;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mailboxes = [[NSMutableArray alloc] initWithObjects:#"Inbox", #"Drafts", #"Sent Items", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return mailboxes.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (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];
}
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
return cell;
}
#end
Currently this is how it looks.
How can I get this the way I described above? (first section: Inbox, second section: Drafts, Third section: Sent Items etc.) I've gotten close but not quite there yet.
Thank you.
You should change:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
to
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
These lines should go in the cellForRowAtIndexPath: method.
Your array index should be based on the sections and not the rows. Row is always fixed at zero.
Replace
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
With
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
If you want section headings as well implement the - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section and return a string based on the section..
With the code from other answers, you will be able to display one cell inside each section. What I prefer to do is have a multi-dimensional NSArray of pre-built cells and when I need them they're in index [section][row].
You currently have one array with just sections and trying to access them as rows (as other answers suggest). Your code won't work if you have more mailboxes in one table group..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mailboxes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (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];
}
switch(indexpath.section){
case 0:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 1:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 2:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
default:
break;
}
return cell;
}