UITableview keeps old cells after reloadData - uitableview

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

Related

Tableview Rows delete ios

I used two(canEditRowAtIndexPath and commitEditingStyle ) methods for delete rows based on index, whenever I went to next view and coming back to the table view deleted rows are again appeared.
How can I delete the rows permanently in table view? it would be very helpful to me.
Seems like you just delete the data from UI, so you got the problem.
You must delete the data from your source and UI at the same time.
Just reference this code:
#interface ViewController ()
#property UITableView* tableView;
#property NSMutableArray* dataArray;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dataArray = [[NSMutableArray alloc] init];
for (int i = 0; i<10; i++) {
[_dataArray addObject:[NSString stringWithFormat:#"Test%d",i]];
}
_tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
_tableView.editing = true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"MyTableCell"];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyTableCell"];
}
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return true;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Delete data from source
[_dataArray removeObjectAtIndex:indexPath.row];
//Delete row from UI
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#end
Hope it can help you.
Use NSMutableArray as your UITableView's datasource.
In commitEditingStyle Delegate....
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[datasourceArray removeObjectAtIndex:indexPath.row];
}
}

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

swipe to delete option breaking the tableviewcell functionality

So I have been creating a tableviewcontroller that handles my tableview and its tableviewcells..
Here's the code––
ItemsViewController.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#interface ItemsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
-(IBAction)addNewItem:(id)sender;
#end
ItemsViewController.m
#implementation ItemsViewController
-(id) init{
self= [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UINavigationItem *n= [self navigationItem];
[n setTitle:#"Homepwner"];
for (int i=0; i<5; i++) {
[[BNRItemStore sharedStore] createItem];
}
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
NSLog(#"ItemsView loaded");
[super viewDidLoad];
UIBarButtonItem *bbi= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNewItem:)];//Target-Action pair..
self.navigationItem.rightBarButtonItem= bbi;
// 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.leftBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated{
NSLog(#"ItemsView appearing");
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
-(IBAction)addNewItem:(id)sender{
BNRItem *newItem= [[BNRItemStore sharedStore] createItem];
NSLog(#"%d",[[[BNRItemStore sharedStore] allItems] count]);
int newRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *ip= [NSIndexPath indexPathForRow:newRow inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];
}
//Row Selection
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row<[[BNRItemStore sharedStore] allItems].count){
NSLog(#"Row# %d selected",indexPath.row);
DetailViewController *detailViewController= [[DetailViewController alloc] init];
NSArray *items= [[BNRItemStore sharedStore] allItems];
BNRItem *item= [items objectAtIndex:indexPath.row];
detailViewController.item=item;//BNRItem at the selected indexPath.
//Push it onto the top of the navigation controller's stack
[[self navigationController] pushViewController:detailViewController animated:YES];
}
else{
return;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Test1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
NSLog(#"Current section- %d",section);
return [[[BNRItemStore sharedStore] allItems] count]+1 ;//no. of rows (5)+'No more items' row
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"hello");
// NSLog(#"%#",headerView);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (!cell) {
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row<[[BNRItemStore sharedStore] allItems].count) {
[[cell textLabel] setText:[[[[BNRItemStore sharedStore] allItems] objectAt Index:indexPath.row] description]];
}
else if (indexPath.row== [[BNRItemStore sharedStore] allItems].count)
{
[[cell textLabel] setText:#"No more items"];
}
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.
if (self.editing) {
NSLog(#"Editing is on");
self.navigationItem.rightBarButtonItem.enabled=NO;
if (indexPath.row==[[BNRItemStore sharedStore] allItems].count) {
[tableView cellForRowAtIndexPath:indexPath].hidden=YES;
return NO;// Do not make 'No more items' editable.
}
}
else{//When the user is out of editing mode
NSLog(#"editing done");
[self.tableView cellForRowAtIndexPath:indexPath].hidden=NO;
self.navigationItem.rightBarButtonItem.enabled=YES;
}
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",NSStringFromSelector(_cmd));
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Deletion is on");
// Delete the row from the data source
NSArray *items= [[BNRItemStore sharedStore] allItems];
BNRItem *p= [items objectAtIndex:[indexPath row]];
[[BNRItemStore sharedStore] removeItem:p];
[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
{
[[BNRItemStore sharedStore] moveItemAtIndex:fromIndexPath.row toIndex:toIndexPath.row];
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath{
if (proposedIndexPath.row==[[BNRItemStore sharedStore] allItems].count) {// 'No more items' row
return [NSIndexPath indexPathForRow:proposedIndexPath.row-1 inSection:0];
}
return proposedIndexPath;
}
#end
When the editing mode is on i.e when i tap Edit button on the NavBar the Add button grays out and the 'No more items' rows becomes hidden. After exiting Edit mode (by tapping 'Done' button) the Add button becomes selectable and the 'No more items' row unhides.. This is all fine when i perform editing by first entering the Edit mode and then exiting the Edit mode. But when instead of using the above way, i use swipe to delete feature, the view doesn't realize the above functionality (i.e. Ungraying/Re-enabling the Add button once i'm done doing a deletion using the swipe to delete thing. When i swipe across a cell (so as to bring up the delete button on the right side of the cell and graying out the Add button in the process) and choose not to press the 'Delete' button by just retapping on the cell (so that the Delete button goes away) the Add button does not update itself so as to get enabled.. What to do about. Pls carefully examine my code first and give recommendations accordingly..
use this tableView Delegate mathod
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Please Utilize the Apple Library- UItableView

How do I change an array and make it show the changes in the UITableView?

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

Going through Apple's ToDoList app tutorial, item tapping doesn't add "completed" checkmark properly.

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

Resources