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];
}
Related
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.
I have a search bar in my table view everything works fine when searching initial table datas listed in array but when i dynamically insert a new data into the table i am not able to search the newly inserted data. help i am stuck
#import "contactsTableViewController.h"
#import "loginpageViewController.h"
#import "addcontactsViewController.h"
#import"customcells.h"
#interface contactsTableViewController ()
#end
#implementation contactsTableViewController
#synthesize contactsarray;
#synthesize searchedarray;
- (IBAction)unwindTocontacts:(UIStoryboardSegue *)segue
{
addcontactsViewController *source = [segue sourceViewController];
customcells *item = source.contacts;
if (item != nil)
{
[self.contactsarray addObject:item];
[self.tableView reloadData];
}
}
- (void)viewDidLoad
{
contactsarray=[[NSMutableArray alloc]init];
self.searchedarray = [NSMutableArray arrayWithCapacity:[contactsarray count]];
[self loadInitialData];
[super viewDidLoad];
}
- (void)loadInitialData {
customcells *item1 = [[customcells alloc] init];
item1.name = #"Arun";
[self.contactsarray addObject:item1];
customcells *item2 = [[customcells alloc] init];
item2.name = #"Balaji";
[self.contactsarray addObject:item2];
customcells *item3 = [[customcells alloc] init];
item3.name = #"Chandru";
[self.contactsarray addObject:item3];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchedarray count];
} else {
return [contactsarray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Prototypecells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (tableView == self.searchDisplayController.searchResultsTableView)
{
customcells *newarray = [searchedarray objectAtIndex:indexPath.row];
cell.textLabel.text =newarray.name;
} else {
customcells *myarray= [contactsarray objectAtIndex:indexPath.row];
cell.textLabel.text = myarray.name;
}
}
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
NSMutableArray *tempContent = [self.contactsarray mutableCopy];
[tempContent removeObject:[tempContent objectAtIndex:indexPath.row]];
self.contactsarray = tempContent;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: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 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)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searchedarray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[c] %#",searchText];
searchedarray = [NSMutableArray arrayWithArray:[contactsarray filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex: [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
#end
Looks like you've implemented the UISearchDisplayController delegates - but to actually filter the array every time a new character is entered you also need to implement the UISearchBar delegate method textDidChange. Filter your data in that method.
Here's a demo project I've written - hope it helps: https://github.com/versluis/TableSearch2014
To see items in my tableview, I load them from an array, but when I arrange the table, the changes don't stay. It works at first, but when I click on one of the cells it loads the data from the original order.
This is my tableview controller. I got almost all of the code from this apple developer tutorial:
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html#//apple_ref/doc/uid/TP40011343-CH10-SW1
#import "NoteViewController.h"
#import "Note.h"
#import "NewNoteViewController.h"
#interface NoteViewController ()
#property NSMutableArray *notes;
#end
#implementation NoteViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
NewNoteViewController *source = [segue sourceViewController];
Note *notee = source.note;
if (notee != nil)
{
[self.notes addObject:notee];
[self.tableView reloadData];
}
}
/*
- (void)loadInitialData
{
Note *item1 = [[Note alloc] init];
item1.itemName = #"Note 1";
[self.notes addObject:item1];
}
*/
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.notes = [[NSMutableArray alloc] init];
//[self loadInitialData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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.notes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NotePrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Note *note = [self.notes objectAtIndex:indexPath.row];
cell.textLabel.text = note.itemName;
// 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
[self.notes removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
}
// 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 story board-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.
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//Note *tappedItem = [self.notes objectAtIndex:indexPath.row];
[tableView reloadRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}
#end
By uncommenting - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath You have enabled moving rows. You need to implement that method to swap the items in your notes array. Like:
id *note = [self.notes objectAtIndex:sourceIndexPath.row];
[self.notes removeObjectAtIndex:fromIndexPath.row];
[self.notes insertObject:note atIndex:toIndexPath.row];
When I tap an item, it doesn't seem to register and add a checkmark on the right. When I tap a subsequent item, it shows a checkmark next to the one I tapped previously, but not for the one I just tapped, and so on, always remaining one action behind.
XYZToDoListViewController.m:
//
// XYZToDoListViewController.m
// ToDoList
//
// Created by Andrew Ghobrial on 2/15/14.
//
//
#import "XYZToDoListViewController.h"
#import "XYZToDoItem.h"
#interface XYZToDoListViewController ()
#property NSMutableArray *toDoItems;
#end
#implementation XYZToDoListViewController
- (void)loadInitialData {
XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
item1.itemName = #"Buy milk";
[self.toDoItems addObject:item1];
XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
item2.itemName = #"Buy eggs";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
item3.itemName = #"Read a book";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
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 story board-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.
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}
#end
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
should be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Notice you have Deselect but want Select
I have a view with a tabBar at the bottom and a tableview. When a barButtonItem is pressed, the array that holds the data for the tableview changes.
With NSLogs, it is clear that the array is really changing, as the [NSArray count] displays different values. However, when I use [UITableview reloadData], the cells stay the same.
If I scroll a up a bit however and then scroll back down, whatever went offscreen gets updating. I'm guessing this is because when it goes offscreen it is dequeued and when it comes back it is redrawn with the new data. Is there a way to just have it redraw everything?
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize listBar,selectedTab , linkTableView, barButton5, barButton4, barButton3, barButton2, barButton1, Lists, imageID, titleID, linkID, cells;
- (void)viewDidLoad
{
[super viewDidLoad];
linkTableView = [[UITableView alloc] init];
Lists = [[NSArray alloc] init];
imageID = [[NSMutableArray alloc] init];
titleID = [[NSMutableArray alloc] init];
linkID = [[NSMutableArray alloc] init];
linkTableView.delegate = self;
linkTableView.dataSource = self;
}
-(void)viewWillAppear:(BOOL)animated{
NSArray *barItems = [[NSArray alloc] initWithObjects:barButton1, barButton2, barButton3, barButton4, barButton5, nil];
listBar.selectedItem = [barItems objectAtIndex:selectedTab];
//when view will appear load data dependent on selectedTab
[self performSelector:#selector(updateTable)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
selectedTab = item.tag;
[self performSelector:#selector(updateTable)];
}
-(void)updateTable{
[imageID removeAllObjects];
[titleID removeAllObjects];
[linkID removeAllObjects];
//load from the xml file
RXMLElement *rxml = [RXMLElement elementFromXMLFile:#"Lists.xml"];
//makes an array from the list children
Lists = [rxml children:#"List"];
cells = [[Lists objectAtIndex:selectedTab] children:#"Cell"];
[rxml iterateElements:cells usingBlock:^(RXMLElement *cellElement) {
[imageID addObject:[cellElement child:#"ImageName"]];
[titleID addObject:[cellElement child:#"CellText" ]];
[linkID addObject:[cellElement child:#"Link" ]];
}];
NSLog(#"Count is %i", [cells count]);
[linkTableView 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 [cells count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LinkCell";
linkCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[linkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *imageName = [NSString stringWithFormat:#"%#", [imageID objectAtIndex:indexPath.row]];
cell.image.image = [UIImage imageNamed:imageName];
NSString *labelText = [NSString stringWithFormat:#"%#", [titleID objectAtIndex:indexPath.row]];
cell.linkCellLabel.text = labelText;
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 - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
remove this line from your code:
linkTableView = [[UITableView alloc] init];
or any other initializers for your uitableview
and use this code to update the Sections
[self.tableView beginUpdates];
NSMutableIndexSet* index = [[NSMutableIndexSet alloc]init];
[index addIndex:0];
[self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];