Odd animations with swipe to delete action in UITableView - objective-c

I am encountering an issue with my UITableView.
The animations for the delete swipe gesture does not work properly.
The thing is, in the new template project "Master Detailed" it works well. But not in the project I am currently working in.
I had an other issue before with the animations that was not working after finishing animate the first time. I fixed it by replacing this in my code.
/// New code
- (void)gl_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
if (!key || !obj) {
return;
}
[self gl_setObject:obj forKeyedSubscript:key];
}
/// Old code
- (void)gl_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
if (!key) {
return;
}
if (!obj) {
obj = [NSNull null];
}
[self gl_setObject:obj forKeyedSubscript:key];
}
And this is the code of the current TableView that is working in the Xcode base project but not in mine.
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface TempViewController: UIViewController
#end
NS_ASSUME_NONNULL_END
#import "TempViewController.h"
#import "TempTableViewCell.h"
#interface TempViewController () <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property NSMutableArray *objects;
#end
#implementation TempViewController
- (void)viewDidLoad {
[super viewDidLoad];
// TableView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 80;
[self.tableView registerNib:[UINib nibWithNibName:#"TempTableViewCell" bundle:nil] forCellReuseIdentifier:#"TempTableViewCell"];
// Add button to add element when deleting too much
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
// Add some data to make the bug work
for (int i = 0; i < 100; i++) {
[self insertNewObject:0];
}
}
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TempTableViewCell" forIndexPath:indexPath];
NSDate *object = self.objects[indexPath.row];
cell.tempLabel.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) {
[self.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.
}
}
#end
So, I am wondering guys if you have any clue about where to look at? Or already experienced this issue before?
Thank you for your future help!!
Happy building!

Well,
I went deeper in the direction on our swizzling methods.
I removed this code completely. We were using it to avoid inserting nil value in dictionary. Without it, the bug disappeared. I am not sure why, but if I found the answer someday, I will update this post.
Hope it can save some time for some people!
#import "NSDictionary+NilSafe.h"
#import <objc/runtime.h>
#implementation NSObject (Swizzling)
+ (BOOL)gl_swizzleMethod:(SEL)origSel withMethod:(SEL)altSel {
Method origMethod = class_getInstanceMethod(self, origSel);
Method altMethod = class_getInstanceMethod(self, altSel);
if (!origMethod || !altMethod) {
return NO;
}
class_addMethod(self,
origSel,
class_getMethodImplementation(self, origSel),
method_getTypeEncoding(origMethod));
class_addMethod(self,
altSel,
class_getMethodImplementation(self, altSel),
method_getTypeEncoding(altMethod));
method_exchangeImplementations(class_getInstanceMethod(self, origSel),
class_getInstanceMethod(self, altSel));
return YES;
}
+ (BOOL)gl_swizzleClassMethod:(SEL)origSel withMethod:(SEL)altSel {
return [object_getClass((id)self) gl_swizzleMethod:origSel withMethod:altSel];
}
#end
#implementation NSDictionary (NilSafe)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self gl_swizzleMethod:#selector(initWithObjects:forKeys:count:) withMethod:#selector(gl_initWithObjects:forKeys:count:)];
[self gl_swizzleClassMethod:#selector(dictionaryWithObjects:forKeys:count:) withMethod:#selector(gl_dictionaryWithObjects:forKeys:count:)];
});
}
+ (instancetype)gl_dictionaryWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(NSUInteger)cnt {
id safeObjects[cnt];
id safeKeys[cnt];
NSUInteger j = 0;
for (NSUInteger i = 0; i < cnt; i++) {
id key = keys[i];
id obj = objects[i];
if (!key) {
continue;
}
if (!obj) {
obj = [NSNull null];
}
safeKeys[j] = key;
safeObjects[j] = obj;
j++;
}
return [self gl_dictionaryWithObjects:safeObjects forKeys:safeKeys count:j];
}
- (instancetype)gl_initWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(NSUInteger)cnt {
id safeObjects[cnt];
id safeKeys[cnt];
NSUInteger j = 0;
for (NSUInteger i = 0; i < cnt; i++) {
id key = keys[i];
id obj = objects[i];
if (!key) {
continue;
}
if (!obj) {
obj = [NSNull null];
}
safeKeys[j] = key;
safeObjects[j] = obj;
j++;
}
return [self gl_initWithObjects:safeObjects forKeys:safeKeys count:j];
}
#end
#implementation NSMutableDictionary (NilSafe)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = NSClassFromString(#"__NSDictionaryM");
[class gl_swizzleMethod:#selector(setObject:forKey:) withMethod:#selector(gl_setObject:forKey:)];
[class gl_swizzleMethod:#selector(setObject:forKeyedSubscript:) withMethod:#selector(gl_setObject:forKeyedSubscript:)];
});
}
- (void)gl_setObject:(id)anObject forKey:(id<NSCopying>)aKey {
if (!aKey) {
return;
}
if (!anObject) {
anObject = [NSNull null];
}
[self gl_setObject:anObject forKey:aKey];
}
- (void)gl_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
if (!key) {
return;
}
if (!obj) {
obj = [NSNull null];
}
[self gl_setObject:obj forKeyedSubscript:key];
}
#end
#implementation NSNull (NilSafe)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self gl_swizzleMethod:#selector(methodSignatureForSelector:) withMethod:#selector(gl_methodSignatureForSelector:)];
[self gl_swizzleMethod:#selector(forwardInvocation:) withMethod:#selector(gl_forwardInvocation:)];
});
}
- (NSMethodSignature *)gl_methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig = [self gl_methodSignatureForSelector:aSelector];
if (sig) {
return sig;
}
return [NSMethodSignature signatureWithObjCTypes:#encode(void)];
}
- (void)gl_forwardInvocation:(NSInvocation *)anInvocation {
NSUInteger returnLength = [[anInvocation methodSignature] methodReturnLength];
if (!returnLength) {
// nothing to do
return;
}
// set return value to all zero bits
char buffer[returnLength];
memset(buffer, 0, returnLength);
[anInvocation setReturnValue:buffer];
}
#end
EDIT 1: Well, at the end, the problem was this nil check of obj
if (!key || !obj) {
return;
}
/// Replaced by
if (!key) {
return;
}
I kept the rest of the code, it was producing a crash in case of inserting trying to insert nil value in a NSDictionary.

Related

UISearchBartextfield -webview called.this method is no longer supported with the new text architecture

I am getting this error and my search function does nothing when the user starts typing a query( XCODE 5.1).
I dont know how the -webView is related to this error as I have not used webView in these files. Any advice would be appreciated. Thank you.
OrdersViewController.h
#import <UIKit/UIKit.h>
#interface OrdersViewController : UITableViewController <UISearchBarDelegate>
//NSmurray is set because we want to edit/move items as well (this will be implemented later)
#property (nonatomic, strong) NSMutableArray *jsonParseArray;
#property (nonatomic, strong) NSMutableArray *ordersArray;
#property (strong, nonatomic) IBOutlet UISearchBar *searchOrders;
#property (nonatomic, strong ) NSMutableArray *orderSearchResults;
#pragma mark -
#pragma mark Class Methods
-(void) retrieveOrderData;
#end
OrdersViewController.m
#import "OrdersViewController.h"
#import "Order.h"
#import "OrderDetailViewController.h"
#define getDataURL #"http://gkhns-macbook-pro.local/gj-3.php"
#interface OrdersViewController ()
#end
#implementation OrdersViewController
#synthesize jsonParseArray, ordersArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
//
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = #"ORDERS";
[self retrieveOrderData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)searchThroughOrders {
self.orderSearchResults = nil;
//gettig the text that is entered in the search bar and it creates nspredicated self containes the search and this text will be replaced by search field
NSPredicate *orderResultsPredicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.searchOrders.text];
//filtering the predicate that is set before
self.orderSearchResults = [[self.ordersArray filteredArrayUsingPredicate:orderResultsPredicate] mutableCopy];
}
//detect when the user typed in the search
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughOrders];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//are we in regular table view or search display view? Lets check that
if (tableView == self.tableView)
{
return ordersArray.count;
}
else{
[self searchThroughOrders];
// Return the number of rows in the section.
return self.orderSearchResults.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"OrderCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellStyleDefault;
}
if (tableView== self.tableView)
// Configure the cell...
{Order *orderObject;
orderObject = [ordersArray objectAtIndex:indexPath.row];
cell.textLabel.text=orderObject.order_id;
cell.detailTextLabel.text=orderObject.firstname;
}
else
{
cell.textLabel.text = self.orderSearchResults [indexPath.row];
}
//accessory
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
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.
if ([[segue identifier] isEqualToString:#"pushOrderDetailView"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Order* object = [ordersArray objectAtIndex:indexPath.row];
[[segue destinationViewController] getOrder:object];
}
}
#pragma mark -
#pragma mark Class Methods
-(void) retrieveOrderData{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonParseArray=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//orders array
ordersArray= [[NSMutableArray alloc]init];
//loop through the jason array
for (int i=0; i<jsonParseArray.count; i++)
{
//create the order object
NSString *oId = [[jsonParseArray objectAtIndex:i] objectForKey:#"order_id"];
NSString *eMail = [[jsonParseArray objectAtIndex:i] objectForKey:#"email"];
NSString *fName = [[jsonParseArray objectAtIndex:i] objectForKey:#"firstname"];
NSString *lName = [[jsonParseArray objectAtIndex:i] objectForKey:#"lastname"];
NSString *sName = [[jsonParseArray objectAtIndex:i] objectForKey:#"store_name"];
NSString *iPrefix = [[jsonParseArray objectAtIndex:i] objectForKey:#"invoice_prefix"];
NSString *pAddress1 = [[jsonParseArray objectAtIndex:i] objectForKey:#"payment_address_1"];
NSString *sPaymentAddress2 = [[jsonParseArray objectAtIndex:i] objectForKey:#"payment_address_2"];
//add order to orders array
[ordersArray addObject:[[Order alloc]initWithOrderId: (NSString *)oId andInvoicePrefix: (NSString*)iPrefix andStoreName:(NSString*)sName andFirstName:(NSString*)fName andLastName:(NSString*)lName andEmail:(NSString*)eMail andPaymentAddress1:(NSString*)pAddress1 andPaymentAddress2:(NSString*)sPaymentAddress2]];
}
//reload the view
[self.tableView reloadData];
}
#end
I was using searchbar only instead of search bar and the search display controller which may have caused this issue.
Also done a few changes to the searchdisplay controller implementation below which fixed the issue.
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self searchThroughOrders:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView{
[tableView registerClass:[CustomOrdersCell class] forCellReuseIdentifier:#"OrderCell"];
}
-(void)searchThroughOrders:(NSString *)searchText scope:(NSString*)scope {
//getting the text that is entered in the search bar and it creates nspredicated self containes the search and this text will be replaced by search field
NSPredicate *orderResultsPredicate = [NSPredicate predicateWithFormat:#"order_id contains[c] %#", searchText];
//filtering the predicate that is set before
_orderSearchResults = [ordersArray filteredArrayUsingPredicate:orderResultsPredicate];
}

Property not found in cellForRowAtIndexPath: method

I am getting an error which I don't understand because I have declared the property/ies. Have commented out the error in the AllListViewController.m file in the cellForRowAtIndexPath: method.
Here are the files:
Checklist.h
#import <Foundation/Foundation.h>
#interface Checklist : NSObject <NSCoding>
#property (nonatomic, copy) NSString *name;
#property (nonatomic, strong) NSMutableArray *items;
#property (nonatomic, copy) NSString *iconName;
-(int)countUncheckedItems;
#end
Checklist.m
#import "Checklist.h"
#import "ChecklistItem.h"
#implementation Checklist
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super init])) {
self.name = [aDecoder decodeObjectForKey:#"Name"];
self.items = [aDecoder decodeObjectForKey:#"Items"];
self.iconName = [aDecoder decodeObjectForKey:#"IconName"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:#"Name"];
[aCoder encodeObject:self.items forKey:#"Items"];
[aCoder encodeObject:self.iconName forKey:#"IconName"];
}
-(id)init
{
if ((self = [super init])) {
self.items = [[NSMutableArray alloc] initWithCapacity:20];
self.iconName = #"Appointments";
}
return self;
}
-(int)countUncheckedItems
{
int count = 0;
for (ChecklistItem *item in self.items) {
if (!item.checked) {
count += 1;
}
}
return count;
}
-(NSComparisonResult)compare:(Checklist *)otherChecklist
{
return [self.name localizedStandardCompare:otherChecklist.name];
}
#end
AllListsViewController.h
#import <UIKit/UIKit.h>
#import "ListDetailViewController.h"
#class DataModel;
#interface AllListsViewController : UITableViewController <ListDetailViewControllerDelegate, UINavigationControllerDelegate>
#property (nonatomic, strong) DataModel *dataModel;
#end
AllListsViewController.m
#import "AllListsViewController.h"
#import "Checklist.h"
#import "ChecklistViewController.h"
#import "ChecklistItem.h"
#import "DataModel.h"
#interface AllListsViewController ()
#end
#implementation AllListsViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 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.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.dataModel setIndexOfSelectedChecklist:indexPath.row];
Checklist *checklist = self.dataModel.lists[indexPath.row];
[self performSegueWithIdentifier:#"ShowChecklist" sender:checklist];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowChecklist"]) {
ChecklistViewController *controller = segue.destinationViewController;
controller.checklist = sender;
}
else if ([segue.identifier isEqualToString:#"AddChecklist"]) {
UINavigationController *navigationController = segue.destinationViewController;
ListDetailViewController *controller = (ListDetailViewController *)navigationController.topViewController;
controller.delegate = self;
controller.checklistToEdit = nil;
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"ListNavigationController"];
ListDetailViewController *controller = (ListDetailViewController *)navigationController.topViewController;
controller.delegate = self;
Checklist *checklist = self.dataModel.lists[indexPath.row];
controller.checklistToEdit = checklist;
[self presentViewController:navigationController animated:YES completion:nil];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.dataModel.lists count];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.dataModel.lists removeObjectAtIndex:indexPath.row];
NSArray *indexPaths = #[indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (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.imageView.image = [UIImage imageNamed:Checklist.iconName]; /* Use of undeclared identifier; did you mean 'Checklist'? or Property 'iconName' not found on object of type 'Checklist'*/
return cell;
}
Checklist *checklist = self.dataModel.lists[indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Remaining", [checklist countUncheckedItems]];
int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
cell.detailTextLabel.text = #"(No Items)";
} else if (count == 0) {
cell.detailTextLabel.text = #"All Done";
} else {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Remaining", count];
}
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.
}
*/
-(void)listDetailViewControllerDidCancel:(ListDetailViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist
{
[self.dataModel.lists addObject:checklist];
[self.dataModel sortChecklists];
[self.tableView reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)listDetailViewController:(ListDetailViewController *)controller didFinishEditingChecklist:(Checklist *)checklist
{
[self.dataModel sortChecklists];
[self.tableView reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if(viewController == self) {
[self.dataModel setIndexOfSelectedChecklist:-1];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.delegate = self;
NSInteger index = [self.dataModel indexOfSelectedChecklist];
if(index >= 0 && index < [self.dataModel.lists count]) {
Checklist *checklist = self.dataModel.lists[index];
[self performSegueWithIdentifier:#"ShowChecklist" sender:checklist];
}
}
#end
You have not declared the variable "Checklist" & trying to access the "iconName". Actually you are trying to access it directly via class name.
I can see you have created an instance if "Checklist" few lines down. So better create that instance before using Checklist.iconName
May be in the beginning of function after CellIdentifier creation.
Checklist *checklist = self.dataModel.lists[indexPath.row];
cell.imageView.image = [UIImage imageNamed:checklist.iconName]; /* Use of undeclared identifier; did you mean 'Checklist'? or Property 'iconName' not found on object of type 'Checklist'*/
return cell;
}
As per your code, "Checklist" is your class name, whereas the instance starts with small "c" as "checklist". So you also might have got confused.
Hope that helps.

Another "reloadData calls numberOfRows, but not cellForRowAtIndexPath"

I know this question has been asked again and again. This post summarized some common causes, but none applies to me:
Every answer I've seen when searching has been a variation of: 1) The tableView is nil 2) numberOfRowsInSection is 0 3) tableView's delegate/data source not set 4) calling reloadTable on the wrong uiTableView.
The answer to that post was the tableView was not displayed before another call to reloadData, which is not my case either. My actual code is a bit lengthy, so I would just paste the parts that I think is related. Feel free to ask me to paste more. Note that competitorsTable has been added to the view in the story board.
#interface CartItemViewController : TrackedUIViewController <UITableViewDataSource, UITableViewDelegate>
//...
#end
#interface CartItemViewController ()
//...
#property (weak, nonatomic) IBOutlet UITableView *competitorsTable;
#end
#implementation CartItemViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
NSAssert(self.competitorsTable, #"Competitor table should not be nil");
self.competitorsTable.dataSource = self;
self.competitorsTable.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self updateCompetitors];
}
- (void)updateCompetitors
{
MBProgressHUD *indicator = [MBProgressHUD showHUDAddedTo:self.hostView animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (![self.product isLoaded]) {
[NSThread sleepForTimeInterval:1];
}
[self.product loadCompetitorsPriceForConditionValue:self.conditionValue];
NSDictionary *competitors = self.product.competitors[#(self.conditionValue)];
dispatch_async(dispatch_get_main_queue(), ^{
if (competitors) {
if (competitors.count > 1) {
self.hostView.hidden = NO;
self.hostView.hostedGraph = [[CompetitorGraph alloc]initWithFrame:self.hostView.bounds Competitors:competitors];
NSAssert(!self.competitorsTable.hidden, #"Competitor table should not be hidden");
[self.competitorsTable reloadData];
} else {
self.hostView.hidden = YES;
}
} else {
self.hostView.hostedGraph = nil;
}
[indicator hide:YES];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *competitors = self.product.competitors[#(self.conditionValue)];
if (competitors.count == 0) {
NSLog(#"WARNING: %s returning 0", __PRETTY_FUNCTION__);
} else {
NSLog(#"Number of rows: %d", competitors.count);
}
return [self.product.competitors[#(self.conditionValue)] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Sort names according to its price
NSDictionary *competitors = self.product.competitors[#(self.conditionValue)];
NSArray *names = [competitors.allKeys sortedArrayUsingComparator:^NSComparisonResult(id name1, id name2) {
if ([competitors[name1] floatValue] > [competitors[name2] floatValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if ([competitors[name1] floatValue] < [competitors[name2] floatValue]) {
return (NSComparisonResult)NSOrderedDescending;
} else {
return (NSComparisonResult)NSOrderedSame;
}
}];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"vendor"];
NSString *vendorName = names[indexPath.row];
cell.textLabel.text = vendorName;
cell.detailTextLabel.text = competitors[vendorName];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Competitors' Offer";
}
#end
When the view is opened, there are two calls to numberOfRowsInSection, one returns 0, which is normal because the competitors information hasn't been loaded, the other returns a number greater than 0. But cellForRowAtIndexPath is called in none of the cases.
After hours of debugging, I finally found the problem: I didn't set the struts and springs right, making the table be squeezed to 0 height. Because it is not shown, cellForRowAtIndexPath was not called

Add Dropbox Files To UITableView

I am trying to load my dropbox files into a UITableView but they are not showing up. I did every step from registering my app on dropbox.com and implementing the session delegate in my app. Here is my code can anyone tell me whats wrong with it. Im pretty sure i declared everything correctly but i cant seem to find the problem. Also i know its not a connection issue because i added an NSLog and it logs the files.
#import <UIKit/UIKit.h>
#import <DropboxSDK/DropboxSDK.h>
#interface testViewController : UITableViewController <DBRestClientDelegate>
{
DBRestClient *restClient;
NSMutableArray *dropboxURLs;
}
#end
#import "testViewController.h"
#interface testViewController ()
#end
#implementation testViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (DBRestClient *)restClient {
if (!restClient) {
restClient =
[[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dropboxURLs = [[NSMutableArray alloc] init];
[[self restClient] loadMetadata:#"/"];
}
- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
if (metadata.isDirectory) {
for (DBMetadata *file in metadata.contents) {
if (!file.isDirectory)
{
NSLog(#"%#", file.filename);
[dropboxURLs addObject:file.filename];
}
}
}
}
- (void)restClient:(DBRestClient *)client
loadMetadataFailedWithError:(NSError *)error {
NSLog(#"Error loading metadata: %#", error);
}
- (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 dropboxURLs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"FileCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [dropboxURLs objectAtIndex:indexPath.row];
return cell;
}
#end
Reload the table view after you add things to the dropboxURLs array.
[self.tableView reloadData];
Try something like this immediately after your addObject call:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
-(void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
if (metadata.isDirectory) {
for (DBMetadata *file in metadata.contents) {
if (!file.isDirectory)
{
NSLog(#"%#", file.filename);
[dropboxURLs addObject:file.filename];
}
}
}
[self. tableView reloadData];
}
Don't forget to set delegate and datasource of UITableView to self.
we have to load data from DropBoxRoot
After MetaData is loaded ,reload the tableview
(void)viewDidLoad{
[super viewDidLoad];
dropboxURLs = [[NSMutableArray alloc] init];
[self loadData];
}
-(void) loadData{
if ([DBSession sharedSession].root == kDBRootDropbox) {
photosRoot = #"/";//can specify any folder like /Photos,/Public etc
}
[self.restClient loadMetadata:photosRoot withHash:photosHash];
}
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
photosHash = metadata.hash;
NSArray* validExtensions = [NSArray arrayWithObjects:#"jpg", #"jpeg",#"png",#"txt",#"pdf",#"doc",#"mp3",#"mp4", nil];
for (DBMetadata* child in metadata.contents) {
NSString* extension = [[child.path pathExtension] lowercaseString];
if (!child.isDirectory && [validExtensions indexOfObject:extension] != NSNotFound) {
[directory addObject:child.filename];
}else{
[allFileAndDirectory addObject:child.filename];
}
[myTableView reloadData];
}

No known class method for selector 'defaultStore'

Am working my through the Big Nerd Ranch iOS Programming 3rd edition, and have run into a problem with the selector 'defaultStore' which is a singleton. The error says that there is no such class method, and I don't know how to fix the problem, which I have commented out.
I believe that there is a part where the book will switch over to Core Data, but I haven't reached that part yet.
ItemsViewController.m
#import "ItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"
#implementation ItemsViewController
- (id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UINavigationItem *n = [self navigationItem];
[n setTitle:#"Homepwner"];
// Create a new bar button item that will send
// addNewItem: to ItemsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
[[self navigationItem] setRightBarButtonItem:bbi];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
- (IBAction)addNewItem:(id)sender
{
// Create a new BNRItem and add it to the store
BNRItem *newItem = [[BNRItemStore defaultStore] createItem];//No known class method for selector 'defaultStore'
// Incompatible pointer types initializing 'BNRItem*__strong' with an expression of 'NSArray'
// Figure out where that item is in the array
int lastRow = [[[BNRItemStore defaultStore] allItems] indexOfObject:newItem]; //No known class method for selector 'defaultStore'
NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];
// Insert this new row into the table.
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip]
withRowAnimation:UITableViewRowAnimationTop];
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
[[BNRItemStore defaultStore] moveItemAtIndex:[fromIndexPath row] //No known class method for selector 'defaultStore'
toIndex:[toIndexPath row]];
}
- (void)tableView:(UITableView *)aTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] init];
NSArray *items = [[BNRItemStore defaultStore] allItems];//No known class method for selector 'defaultStore'
BNRItem *selectedItem = [items objectAtIndex:[indexPath row]];
// Give detail view controller a pointer to the item object in row
[detailViewController setItem:selectedItem];
// Push it onto the top of the navigation controller's stack
[[self navigationController] pushViewController:detailViewController
animated:YES];
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete)
{
BNRItemStore *ps = [BNRItemStore defaultStore];//No known class method for selector 'defaultStore'
NSArray *items = [ps allItems];
BNRItem *p = [items objectAtIndex:[indexPath row]];
[ps removeItem:p];
// We also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore defaultStore] allItems] count];//No known class method for selector 'defaultStore'
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create an instance of UITableViewCell, with default appearance
// Check for a reusable cell first, use that if it exists
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
// If there is no reusable cell of this type, create a new one
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"UITableViewCell"];
}
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
BNRItem *p = [[[BNRItemStore defaultStore] allItems]//No known class method for selector 'defaultStore'
objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
return cell;
}
#end
BNRItemStore.h
#import "BNRItemStore.h"
#import "BNRItem.h"
#implementation BNRItemStore
+ (BNRItemStore *)defaultStore
{
static BNRItemStore *defaultStore = nil;
if(!defaultStore)
defaultStore = [[super allocWithZone:nil] init];
return defaultStore;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
- (id)init
{
self = [super init];
if(self) {
allItems = [[NSMutableArray alloc] init];
}
return self;
}
- (void)removeItem:(BNRItem *)p
{
[allItems removeObjectIdenticalTo:p];
}
- (NSArray *)allItems
{
return allItems;
}
- (void)moveItemAtIndex:(int)from
toIndex:(int)to
{
if (from == to) {
return;
}
// Get pointer to object being moved so we can re-insert it
BNRItem *p = [allItems objectAtIndex:from];
// Remove p from array
[allItems removeObjectAtIndex:from];
// Insert p in array at new location
[allItems insertObject:p atIndex:to];
}
- (BNRItem *)createItem
{
BNRItem *p = [BNRItem randomItem];
[allItems addObject:p];
return p;
}
BNRItemStore.m
#import "BNRItemStore.h"
#import "BNRItem.h"
#implementation BNRItemStore
+ (BNRItemStore *)defaultStore
{
static BNRItemStore *defaultStore = nil;
if(!defaultStore)
defaultStore = [[super allocWithZone:nil] init];
return defaultStore;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
- (id)init
{
self = [super init];
if(self) {
allItems = [[NSMutableArray alloc] init];
}
return self;
}
- (void)removeItem:(BNRItem *)p
{
[allItems removeObjectIdenticalTo:p];
}
- (NSArray *)allItems
{
return allItems;
}
- (void)moveItemAtIndex:(int)from
toIndex:(int)to
{
if (from == to) {
return;
}
// Get pointer to object being moved so we can re-insert it
BNRItem *p = [allItems objectAtIndex:from];
// Remove p from array
[allItems removeObjectAtIndex:from];
// Insert p in array at new location
[allItems insertObject:p atIndex:to];
}
- (BNRItem *)createItem
{
BNRItem *p = [BNRItem randomItem];
[allItems addObject:p];
return p;
}
#end
Make sure that the #interface block for BNRItemStore (in BNRItemStore.h) declares that method:
#interface BNRItemStore : NSObject
+ (BNRItemStore *)defaultStore;
// etc.
#end
The compiler looks at the interface to know what methods are available on that class.