delete tableView data after clicking button on cell IOS - ios

I need to delete a row in my tableview to update my changes.
I have a delete button in each cell (tableViewCellController) - look at the picture.
Screenshot
After I click the delete button, the UI button method calls the delegated method in tableViewController. The delete method update the data source (my model) and i want to update also the screen (now the method reload all the data, but I want to update the new change - delete row from screen).
I tried to do this with the following function but i don't have a sender (as i said the button is pressed in the cell, but I actually make the change on the tableView)
Function:
- (IBAction)contactDelete:(id)sender{
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview];
[self.tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
favoritesTableViewCell:
#import "favoritesTableViewCell.h"
#import "ModelUser.h"
#implementation favoritesTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)favoritesDeleteFromFav:(id)sender {
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
[self.delegate onFavDeleteClick];
}
favoritesTableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.actualLoggedUser = [NSString stringWithFormat:#"2"];
[self reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)reloadData {
NSLog(#"Favorites tab was loaded");
//get id of my favorite contacts
myFavListId = [[ModelUser instance] getUser:self.actualLoggedUser].contactsFavoriteList;
//get data of my favorites contacts
myFavListContactsData = [[NSMutableArray alloc] init];
for (int i=0; i < [myFavListId count] ; i++) {
User* us = [[ModelUser instance] getUser:([myFavListId objectAtIndex:i])];
[myFavListContactsData addObject:us];
}
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (void) viewDidAppear:(BOOL)animated {
[self reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return myFavListContactsData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
favoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"favoriteCell" forIndexPath:indexPath];
User *us = [myFavListContactsData objectAtIndex:indexPath.row];
//setting cell data
cell.actualLoggedUser = self.actualLoggedUser;
cell.contactUserId = us.userId;
cell.contactName.text = [NSString stringWithFormat:#"%# %#",us.fname,us.lname];
[cell.contactImage setImage: [UIImage imageNamed:us.imageName]];
cell.delegate = self;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
User *us = [myFavListContactsData objectAtIndex:indexPath.row];
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
userDetailsProfile* udVC = [sb
instantiateViewControllerWithIdentifier:#"userDetailsProfile"];
udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
udVC.userDetailId = [NSString stringWithFormat:#"%#", us.userId];
[self showViewController:udVC sender:self];
}
- (void)onFavDeleteClick {
[self reloadData];
}
#end

You need to update datasource and delete the cell in your delegate callback that is invoked from your cell class (onFavDeleteClick delegate method of favoritesTableViewCell class, in your case).
The process should be something like this:
In your "favoritesTableViewCell.h", declare a protocol containing onFavDeleteClick method. I think you have already done with this step. What you need to do is to update the method signature as -(void) onFavDeleteClick:(favoritesTableViewCell*)cell.
From "favoritesTableViewCell.m" call favoritesDeleteFromFav method like this:
-(IBAction)favoritesDeleteFromFav:(id)sender {
[self.delegate onFavDeleteClick:self];
}
Now in your view controller where the main UITableView exists implement the callback method like this:
-(void)onFavDeleteClick:(favoritesTableViewCell*)cell {
//update model
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
//update table view
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (indexPath) {
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
And this is everything you need to do to get your desired effect.

Related

Need to Reload view after updating the details Objective C

I have a small issue in Updating the details in the view. How to reload a view after successful updation to change the details.
Below is my code. Please help me to find out the solution. Thanks in Advance.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
[self viewDidLoad];
[super viewWillAppear:animated];
[self.view setNeedsDisplay];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
profilearray=[[NSMutableArray alloc]initWithObjects:#"First Name:",#"Last Name:",#"Date of Birth:",#"Email:",#"Gender:",#"Address:",#"Country:",#"State:",#"City:",#"ZipCode:",#"Phone:",#"Guardian / Caretaker Details:",#"Name:",#"Relationship:",#"Email:",#"Doctor Details:",#"Name:",#"Phone:",#"Email:",#"Insurance Details:",#"Name:",#"Email:",nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [profilearray count];
}
- (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 = [profilearray objectAtIndex:indexPath.row];
if(indexPath.row==0)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"FirstName"];
}
if(indexPath.row==1)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"LastName"];
}
if(indexPath.row==2)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"DOB"];
}
if(indexPath.row==3)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Email"];
}
if(indexPath.row==4)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Gender"];
}
if(indexPath.row==5)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Address"];
}
return cell;
}
use [tableView reloadData] for updating tableview
Simple Use this Do not need to view refresh or reload
-(void)viewWillAppear:(BOOL)animated
{
[tableView reloadData]
[super viewWillAppear:animated];
}
Your data source array profilearray keeps getting re-assigned the same value every time the view appears. Maybe initialise it once in viewDidLoad.
After the array is updated, use [tableView reloadData] to reload the table view.
Do not call viewDidLoad on your own, and get rid of setNeedsDisplay for now.
Just use this.
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
profilearray=[[NSMutableArray alloc]initWithObjects:#"First Name:",#"Last Name:",#"Date of Birth:",#"Email:",#"Gender:",#"Address:",#"Country:",#"State:",#"City:",#"ZipCode:",#"Phone:",#"Guardian / Caretaker Details:",#"Name:",#"Relationship:",#"Email:",#"Doctor Details:",#"Name:",#"Phone:",#"Email:",#"Insurance Details:",#"Name:",#"Email:",nil];
[tableView reloadData];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
profilearray:- Perhaps this is your array from where you are getting data in tableview and also storing data in it
viewWillAppear :- this method is called when view is appear
[tableView_Outlet reloadData] :- this method reload your tableview with new data
So in this scenario never call viewWillAper Because this is not standard way to do
Find point where your data is updated then call "reloadData"
For more about viewWillApear hit
Thanks

reload data in tableview

I can't reload my data in my contactsTableView in iOS. I figured out that the cellForRowAtIndexPath function is not called.
I call onGroupClick on screenController and reloadData get 4 objects but I can't see them on my contactsListView.
screenController:
-(void)onGroupClick:(NSString *)groupClickedId {
NSLog(#"Group id: %# was clicked on group tab", groupClickedId);
contactsTableViewController* ctVC = (contactsTableViewController*)[childControllers objectAtIndex:1];
ctVC.groupSelectedId = groupClickedId;
[ctVC reloadData];
[self switchTabs:1 ];
}
contactsTableViewController:
contactsTableViewController:
#import "contactsTableViewController.h"
#import "contactsTableViewCell.h"
#import "ModelUser.h"
#import "ModelGroup.h"
#import "userDetailsProfile.h"
#interface contactsTableViewController (){
NSArray* usersId;
NSMutableArray* usersData;
}
#end
#implementation contactsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.groupSelectedId = #"";
[self reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)reloadData {
if([self.groupSelectedId isEqualToString:#""])
NSLog(#"Contacts list is empty on first app run");
else {
NSLog(#"Contacts list for group id: %# was loaded on contacts tab", self.groupSelectedId);
//get id of my contacts in selected group
usersId = [[ModelGroup instance] getGroup:self.groupSelectedId].contactsIdList;
//get data of my contacts in selected group
usersData = [[NSMutableArray alloc] init];
for (int i=0; i< [usersId count] ; i++) {
User* us = [[ModelUser instance] getUser:([usersId objectAtIndex:i])];
[usersData addObject:us];
}
}
}
#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 usersData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
contactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"contactCell" forIndexPath:indexPath];
User *us = [usersData objectAtIndex:indexPath.row];
cell.contactsUserId = us.userId;
cell.contactsName.text = [NSString stringWithFormat:#"%# %#",us.fname,us.lname];
[cell.contactsImage setFrame:CGRectMake(0, 0, 10, 10)];
[cell.contactsImage setImage: [UIImage imageNamed:us.imageName]];
[cell contactsSave:cell.contactsSave];
[cell contactsAddToFav:cell.contactsAddToFav];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
User *us = [usersData objectAtIndex:indexPath.row];
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
userDetailsProfile* udVC = [sb
instantiateViewControllerWithIdentifier:#"userDetailsProfile"];
udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
udVC.userDetailId = [NSString stringWithFormat:#"%#", us.userId];
[self showViewController:udVC sender:self];
}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<##"reuseIdentifier"#> forIndexPath:indexPath];
// Configure the cell...
return cell;
}
*/
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)contactsAddToFav:(id)sender {
}
- (IBAction)contactsSave:(id)sender {
}
#end
You have a UITableViewController. You have added a method reloadData to your table view controller, and you call that. That is not the same thing as calling the tableView's reloadData method.
If you want that custom method to cause the table view to reload, it needs to call the table view's reloadData method:
- (void)reloadData {
if([self.groupSelectedId isEqualToString:#""])
NSLog(#"Contacts list is empty on first app run");
else {
NSLog(#"Contacts list for group id: %# was loaded on contacts tab",
self.groupSelectedId);
//get id of my contacts in selected group
usersId =
[[ModelGroup instance]
getGroup:self.groupSelectedId].contactsIdList;
//get data of my contacts in selected group
usersData = [[NSMutableArray alloc] init];
for (int i=0; i< [usersId count] ; i++) {
User* us = [[ModelUser instance] getUser:([usersId objectAtIndex:i])];
[usersData addObject:us];
}
}
[self.tableView reloadData]; //Add this line.
}
When you do that the table view will call the numberOfSectionsInTableView method, then numberOfRowsInSection (once or more) and then cellForRowAtIndexPath
check out your tableview datasource and delegate may solve it
#interface contactsTableViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSArray* usersId;
NSMutableArray* usersData;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.groupSelectedId = #"";
[self reloadData];
}

TableViewController not updating after editing data and saving. Only updates upon application restart

I'm currently making an application that can edit the information stored in certain table view cells by clicking on them and then going to a separate view controller.
Once the changes are made, a save button is pressed, and the fields are updated on parse.com
My issue is that once I go back to my main tableViewController, the table cells aren't updating unless I completely restart the application, at which point everything shows as updated.
I've called the [self.tableView reloadData] method like in the iOS tutorials, but it just won't seem to update.
Here's the .m file for the controller that I'm trying to update.
#interface ProspectsTableViewController ()
#end
#implementation ProspectsTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Prospect"];
[query orderByAscending:#"prospectName"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error in prospect cells");
}
else {
self.allProspects = objects;
NSLog(#"%#", self.allProspects);
[self.tableView reloadData];
}
}];
}
#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.allProspects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProspectCell";
UITableViewCell *prospectCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *prospect = [self.allProspects objectAtIndex:indexPath.row];
prospectCell.textLabel.text = prospect[#"prospectName"];
return prospectCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"cell pressed");
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:#"showProspectInfo" sender:cell];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showProspectInfo"]) {
ProspectInfoViewController *transferViewController = segue.destinationViewController; //prepares an instance of the prospectsTableViewController so we can pass our information over to the next view controller
PFObject *prospect = self.allProspects[self.tableView.indexPathForSelectedRow.row]; //selects the information from the prospect at the cell tapped on
transferViewController.name = prospect[#"prospectName"];
transferViewController.phone = prospect[#"phoneNumber"];
transferViewController.email = prospect[#"email"];
transferViewController.objectId = [prospect objectId];
}
}
#end
any ideas? I've been trying to figure this one out for quite some time now
Thanks in advance!
It doesn't look like you're calling [self.tableView reloadData]; after coming back to the table view controller. Try adding this to your table view controller's implementation to refresh your table view whenever your view controller is about to appear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
try reloadData in viewDidAppear
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView reloadData]
}

Inserting items in UITableView crashes application

I'm making an application with a tableview list of contacts that can be reached via a tab-controller at the bottom.
I copied (literally copy/pasted) from the example Master Detail Application and tried to make sure all storyboard references lined up.
#import "ContactsTableViewController.h"
#import "ContactViewController.h"
#interface ContactsTableViewController () {
NSMutableArray *_objects;
}
#end
#implementation ContactsTableViewController
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
// [self.tableView setDelegate:self]; // From (unsuccesfully) trying https://stackoverflow.com/questions/16311393/how-to-insert-items-to-a-uitableview-when-a-uibutton-is-clicked-in-ios
// [self.tableView setDataSource:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.contactViewController = (ContactViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // Crashes here
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath]; // Crashes here
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSDate *object = _objects[indexPath.row];
self.contactViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
It crashes on the first line in CellRowAtIndexPath. Since I was having trouble I also took the advice in How to insert items to a UITableView when a UIButton is clicked in iOS but it didn't solve my problem.
This is just incredibly frustrating, because as far as I can tell my code is (except for names) exactly the same as the example application.
edit: Exception message is
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Alex - When using storyboards and the new prototype cell feature in xCode, you have to set an identifier value in Interface Builder whose value should match what is in your code.
So notice you have this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
In this case, your cell identifier is "Cell"
Can you confirm that in Interface Builder/Storyboard, your table view cell's identifier is set to the same?
As an example, here's a screenshot from a sample app I was building (Notice my Indentifier field on the right):
I would recommend this
-(void)ViewDidLoad{
[tableView registerClass:[MyCell class] forCellReuseIdentifier:#"Cell"];
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
[tableView reloadData]; //this will trigger cellForRowAtIndexPath again with the updated array
}
and would you please post your error.
You should not use [self.tableView insertRowsAtIndexPaths: withRowAnimation:];. Instead of this you should call [talbeView reloadData] method.

UITableView cell empty when called recursively

I'm trying to use recursive table view but when I click to any cell and second UITableView appears (created by tableView:didSelectRowAtIndexPath:), there are just empty rows without any text.
Can somebody help please ?
#implementation RSTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
[self.tableView registerClass:[RSCell class] forCellReuseIdentifier:#"bbaCell"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.array = [NSMutableArray arrayWithObjects:#"1",#"2",#"3", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RSTableViewController *rsTableView = [[RSTableViewController alloc] initWithStyle:UITableViewStylePlain];
rsTableView.tableView.delegate = self;
[self.navigationController pushViewController:rsTableView animated:TRUE];
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"bbaCell";
RSCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.label1.text = [self.array objectAtIndex:indexPath.row];
cell.label2.text = [self.array objectAtIndex:indexPath.row];
return cell;
}
#end
A. [self.navigationController pushViewController:rsTableView animated:TRUE] is wrong, there's no such thing as TRUE, there's only YES.
B. I feel like you're doing something very wrong but anyway, you method should be the following:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RSTableViewController *rsTableView = [[RSTableViewController alloc] initWithStyle:UITableViewStylePlain];
// each rs table view instance is it's own delegate and datasource
rsTableView.tableView.delegate = rsTableView;
// you forgot to add this line:
rsTableView.tableView.dataSource = rsTableView;
[self.navigationController pushViewController:rsTableView animated:YES];
// what's this line for ?? the new ViewController will automatically call all the necessary methods. please remove it.
[tableView reloadData];
}
please check the syntax, and secondly, what's RSTableViewController base ViewController ?? is it UITableViewController ??
make sure you're setting the delegate and datasource properly.

Resources