UIButton in Custom TableViewCell not Changing Value when Selecting - ios

Thanks to #jsetting32 I have a custom UITableViewCell complete with buttons at the bottom of the tableView. However, when I am clicking those buttons, the value for the selectedState is not changing, making it a bit difficult for the end-user to tell if they have clicked it or not.
self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.likeButton addTarget:self action:#selector(didTapLikeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.likeButton setTitle:#"Pray" forState:UIControlStateNormal];
[self.likeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.likeButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[[self.likeButton titleLabel] setFont:[UIFont fontWithName:#"Verdana" size:12.0f]];
[self.cellView addSubview:self.likeButton];

Try UIControlStateHighlighted instead of UIControlStateSelected
[self.likeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.likeButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

So heres how I solve the button issue... There should already be a method within your custom cell that sets the like status... It is set like so
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellIdentifier";
if (indexPath.row == [self.objects count])
return [self tableView:tableView cellForNextPageAtIndexPath:indexPath];
PHChatCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[PHChatCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setDelegate:self];
}
[self setCellAttributesWithCell:cell withObject:[self.object objectAtIndex:indexPath.row] withIndexPath:indexPath];
return cell;
}
- (void)setCellAttributesWithCell:(PHChatCell *)cell withObject:(PFObject *)object withIndexPath:(NSIndexPath *)indexPath
{
if (object) {
[cell setChat:object];
[cell setTag:indexPath.row];
[cell.likeButton setTag:indexPath.row];
if ([[PHCache sharedCache] attributesForMessage:object]) {
[cell setLikeStatus:[[PHCache sharedCache] isMessageLikedByCurrentUser:object]];
NSString *likeCount = [[[PHCache sharedCache] likeCountForMessage:object] description];
cell.likeCount.text = ([likeCount isEqualToString:#"1"]) ?
[NSString stringWithFormat:#"%# like", likeCount] :
[NSString stringWithFormat:#"%# likes", likeCount];
NSString *commentCount = [[[PHCache sharedCache] commentCountForMessage:object] description];
cell.commentCount.text = ([commentCount isEqualToString:#"1"]) ?
[NSString stringWithFormat:#"%# comment", commentCount] :
[NSString stringWithFormat:#"%# comments", commentCount];
return;
}
#synchronized(self) {
// Put this in your init method
// self.outstandingSectionHeadersQueries = [NSMutableDictionary dictionary]
if (![self.outstandingSectionHeaderQueries objectForKey:#(indexPath.row)]) {
PFQuery *query = [PHUtility queryForActivitiesOnMessage:object cachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
#synchronized(self) {
[self.outstandingSectionHeaderQueries removeObjectForKey:#(indexPath.row)];
if (error) return;
NSMutableArray *likers = [NSMutableArray array];
NSMutableArray *commenters = [NSMutableArray array];
BOOL isLikedByCurrentUser = NO;
for (PFObject *activity in objects) {
if ([[activity objectForKey:kPHActivityTypeKey] isEqualToString:kPHActivityTypeLike] && [activity objectForKey:kPHActivityFromUserKey]) {
[likers addObject:[activity objectForKey:kPHActivityFromUserKey]];
} else if ([[activity objectForKey:kPHActivityTypeKey] isEqualToString:kPHActivityTypeComment] && [activity objectForKey:kPHActivityFromUserKey]) {
[commenters addObject:[activity objectForKey:kPHActivityFromUserKey]];
}
if ([[[activity objectForKey:kPHActivityFromUserKey] objectId] isEqualToString:[[PFUser currentUser] objectId]] &&
[[activity objectForKey:kPHActivityTypeKey] isEqualToString:kPHActivityTypeLike]) {
isLikedByCurrentUser = YES;
}
}
[[PHCache sharedCache] setAttributesForMessage:object likers:likers commenters:commenters likedByCurrentUser:isLikedByCurrentUser];
if (cell.tag != indexPath.row) return;
[cell setLikeStatus:[[PHCache sharedCache] isMessageLikedByCurrentUser:object]];
NSString *likeCount = [[[PHCache sharedCache] likeCountForMessage:object] description];
cell.likeCount.text = ([likeCount isEqualToString:#"1"]) ?
[NSString stringWithFormat:#"%# like", likeCount] : [NSString stringWithFormat:#"%# likes", likeCount];
NSString *commentCount = [[[PHCache sharedCache] commentCountForMessage:object] description];
cell.commentCount.text = ([commentCount isEqualToString:#"1"]) ?
[NSString stringWithFormat:#"%# comment", commentCount] : [NSString stringWithFormat:#"%# comments", commentCount];
}
}];
}
}
}
}
- (void)PHChatCell:(PHChatCell *)cell didTapLikeButton:(UIButton *)button chat:(PFObject *)chat
{
// Disable the button so users cannot send duplicate requests
[cell shouldEnableLikeButton:NO];
//These are private interface properties to handle when the user wants to unlike the prayer
//when the UIActionsheet is loaded
self.chat = chat;
self.likeButton = button;
self.cell = cell;
if (button.selected) {
[[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Unlike" otherButtonTitles:nil] showInView:self.view];
return;
}
BOOL liked = !button.selected;
[cell setLikeStatus:liked];
NSString *originalButtonTitle = button.titleLabel.text;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSNumber *likeCount = [numberFormatter numberFromString:button.titleLabel.text];
[button setTitle:#"Liked" forState:UIControlStateNormal];
[UIView animateWithDuration:0.25 animations:^{
[cell.likeImage setImage:[UIImage imageNamed:#"ButtonLikeSelected.png"]];
[cell.likeImage setTransform:CGAffineTransformMakeScale(1.5, 1.5)];
} completion:^(BOOL finished){
[UIView animateWithDuration:0.25 animations:^{
[cell.likeImage setTransform:CGAffineTransformMakeScale(1, 1)];
}];
}];
NSInteger checker = [[cell.likeCount text] integerValue] + 1;
cell.likeCount.text = (checker == 1) ?
[NSString stringWithFormat:#"%ld like", (long)checker] :
[NSString stringWithFormat:#"%ld likes", (long)checker];
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
[[PHCache sharedCache] incrementLikerCountForMessage:chat];
[[PHCache sharedCache] setMessageIsLikedByCurrentUser:chat liked:liked];
[PHUtility likeMessageInBackground:chat block:^(BOOL succeeded, NSError *error) {
PHChatCell *actualCell = (PHChatCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0]];
[actualCell shouldEnableLikeButton:YES];
[actualCell setLikeStatus:succeeded];
if (!succeeded) {
[actualCell.likeButton setTitle:originalButtonTitle forState:UIControlStateNormal];
}
}];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
BOOL liked = !self.likeButton.selected;
[self.cell setLikeStatus:liked];
[self.likeButton setTitle:#"Like" forState:UIControlStateNormal];
[self.cell.likeImage setImage:[UIImage imageNamed:#"ButtonLike.png"]];
NSInteger checker = [[self.cell.likeCount text] integerValue] - 1;
self.cell.likeCount.text = (checker == 1) ?
[NSString stringWithFormat:#"%ld like", (long)checker] :
[NSString stringWithFormat:#"%ld likes", (long)checker];
NSString *originalButtonTitle = self.likeButton.titleLabel.text;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *likeCount = [numberFormatter numberFromString:self.likeButton.titleLabel.text];
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
if ([likeCount intValue] > 0) {
likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1];
}
[[PHCache sharedCache] decrementLikerCountForMessage:self.chat];
[[PHCache sharedCache] setMessageIsLikedByCurrentUser:self.chat liked:NO];
[PHUtility unlikeMessageInBackground:self.chat block:^(BOOL succeeded, NSError *error) {
PHChatCell *actualCell = (PHChatCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.likeButton.tag inSection:0]];
[actualCell shouldEnableLikeButton:YES];
[actualCell setLikeStatus:!succeeded];
if (!succeeded) {
[actualCell.likeButton setTitle:originalButtonTitle forState:UIControlStateNormal];
}
}];
}
}
Here is the query method used in the previous snippet of code (declared in PHUtility class) :
+ (PFQuery *)queryForActivitiesOnMessage:(PFObject *)message cachePolicy:(PFCachePolicy)cachePolicy {
PFQuery *queryLikes = [PFQuery queryWithClassName:kPHActivityClassKey];
[queryLikes whereKey:kPHActivityMessageKey equalTo:message];
[queryLikes whereKey:kPHActivityTypeKey equalTo:kPHActivityTypeLike];
PFQuery *queryComments = [PFQuery queryWithClassName:kPHActivityClassKey];
[queryComments whereKey:kPHActivityMessageKey equalTo:message];
[queryComments whereKey:kPHActivityTypeKey equalTo:kPHActivityTypeComment];
PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryLikes,queryComments,nil]];
[query setCachePolicy:cachePolicy];
[query includeKey:kPHActivityFromUserKey];
[query includeKey:kPHActivityMessageKey];
return query;
}
Here are the PHCache implementation ...
#interface PHCache()
#property (nonatomic, strong) NSCache *cache;
- (void)setAttributes:(NSDictionary *)attributes forMessage:(PFObject *)message;
#end
#implementation PHCache
#synthesize cache;
#pragma mark - Initialization
+ (id)sharedCache {
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init {
self = [super init];
if (self) {
self.cache = [[NSCache alloc] init];
}
return self;
}
- (void)clear {
[self.cache removeAllObjects];
}
- (void)setAttributes:(NSDictionary *)attributes forMessage:(PFObject *)message {
[self.cache setObject:attributes forKey:[self keyForMessage:message]];
}
- (NSString *)keyForMessage:(PFObject *)message {
return [NSString stringWithFormat:#"message_%#", [message objectId]];
}
#pragma mark - Global Chat
- (void)setAttributesForMessage:(PFObject *)message
likers:(NSArray *)likers
commenters:(NSArray *)commenters
likedByCurrentUser:(BOOL)likedByCurrentUser {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:likedByCurrentUser],kPHMessageAttributesIsLikedByCurrentUserKey,
#([likers count]),kPHMessageAttributesLikeCountKey,
likers,kPHMessageAttributesLikersKey,
#([commenters count]),kPHMessageAttributesCommentCountKey,
commenters,kPHMessageAttributesCommentersKey,
nil];
[self setAttributes:attributes forMessage:message];
}
- (NSDictionary *)attributesForMessage:(PFObject *)message {
return [self.cache objectForKey:[self keyForMessage:message]];
}
- (NSNumber *)likeCountForMessage:(PFObject *)message {
NSDictionary *attributes = [self attributesForMessage:message];
if (attributes) {
return [attributes objectForKey:kPHMessageAttributesLikeCountKey];
}
return [NSNumber numberWithInt:0];
}
- (NSNumber *)commentCountForMessage:(PFObject *)message {
NSDictionary *attributes = [self attributesForMessage:message];
if (attributes) {
return [attributes objectForKey:kPHMessageAttributesCommentCountKey];
}
return [NSNumber numberWithInt:0];
}
- (NSArray *)likersForMessage:(PFObject *)message {
NSDictionary *attributes = [self attributesForMessage:message];
if (attributes) {
return [attributes objectForKey:kPHMessageAttributesLikersKey];
}
return [NSArray array];
}
- (NSArray *)commentersForMessage:(PFObject *)message {
NSDictionary *attributes = [self attributesForMessage:message];
if (attributes) {
return [attributes objectForKey:kPHMessageAttributesCommentersKey];
}
return [NSArray array];
}
- (void)setMessageIsLikedByCurrentUser:(PFObject *)message liked:(BOOL)liked {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForMessage:message]];
[attributes setObject:[NSNumber numberWithBool:liked] forKey:kPHMessageAttributesIsLikedByCurrentUserKey];
[self setAttributes:attributes forMessage:message];
}
- (BOOL)isMessageLikedByCurrentUser:(PFObject *)message {
NSDictionary *attributes = [self attributesForMessage:message];
if (attributes) {
return [[attributes objectForKey:kPHMessageAttributesIsLikedByCurrentUserKey] boolValue];
}
return NO;
}
- (void)incrementLikerCountForMessage:(PFObject *)message {
NSNumber *likerCount = [NSNumber numberWithInt:[[self likeCountForMessage:message] intValue] + 1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForMessage:message]];
[attributes setObject:likerCount forKey:kPHMessageAttributesLikeCountKey];
[self setAttributes:attributes forMessage:message];
}
- (void)decrementLikerCountForMessage:(PFObject *)message {
NSNumber *likerCount = [NSNumber numberWithInt:[[self likeCountForMessage:message] intValue] - 1];
if ([likerCount intValue] < 0) {
return;
}
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForMessage:message]];
[attributes setObject:likerCount forKey:kPHMessageAttributesLikeCountKey];
[self setAttributes:attributes forMessage:message];
}
- (void)incrementCommentCountForMessage:(PFObject *)message {
NSNumber *commentCount = [NSNumber numberWithInt:[[self commentCountForMessage:message] intValue] + 1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForMessage:message]];
[attributes setObject:commentCount forKey:kPHMessageAttributesCommentCountKey];
[self setAttributes:attributes forMessage:message];
}
- (void)decrementCommentCountForMessage:(PFObject *)message {
NSNumber *commentCount = [NSNumber numberWithInt:[[self commentCountForMessage:message] intValue] - 1];
if ([commentCount intValue] < 0) {
return;
}
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForMessage:message]];
[attributes setObject:commentCount forKey:kPHMessageAttributesCommentCountKey];
[self setAttributes:attributes forMessage:message];
}
- (NSNumber *)messageCountForUser:(PFUser *)user {
NSDictionary *attributes = [self attributesForUser:user];
if (attributes) {
NSNumber *photoCount = [attributes objectForKey:kPHUserAttributesMessageCountKey];
if (photoCount) {
return photoCount;
}
}
return [NSNumber numberWithInt:0];
}
- (void)setMessageCount:(NSNumber *)count user:(PFUser *)user {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForUser:user]];
[attributes setObject:count forKey:kPHUserAttributesMessageCountKey];
[self setAttributes:attributes forUser:user];
}
Like I said in your previous post... It requires a hefty amount of code to make this feature 'optimal'... Reason for the cache object is to limit api requests to the servers. If the cell has been loaded, we're gonna check the cache to see if the attributes for the cell have been loaded, if it hasn't query to the server to get the attributes and set the cache to ensure we don't need to make an api request in the future...
All the variables starting with kPH are constants that you can declare on your own, or how you feel is sufficient... Like I said before, checking out the Anypic project and integrating the features into your app is best. Just copy over the PAPCache class and PAPUtility class over. Just make sure you read over the code to fully understand what is going on to get a better sense of how to integrate the code with yours, and to extend it to add more features to your app.
If you have any issues please feel free to post a comment.

i. While touching use UIControlStateHighlighted as suggested above.
ii. Keep a different color:
Since you have a custom UITableViewCell, you should implement an IBAction and set your UIButton to this using
[self.likeButton addTarget:self action:#selector(select:) forControlEvents:UIControlEventTouchUpInside];
in the .m file of your cell set:
-(IBAction)onSelectCellClicked:(id)sender {
if(self.yourButton.selected) {
self.yourButton.selected = NO;
} else {
self.yourButton.selected = YES;
}
}
Now set the cell's delegate to self in cell for row after putting this in the cell's .h file:
#protocol CustomCellDelegate <NSObject>
#end
#interface CustomCell : UITableViewCell
#property (strong, nonatomic) NSObject<CustomCellDelegate>* delegate;
#end
And put in the VC that is presenting the UITableView using the custom cell.

you can just add this line into 'didTapLikeButtonAction' :
self.likeButton.selected = !self.likeButton.selected;
once you change the selected state, the button will change its title color.
you may also want to add this line for a smoother effect:
[self.likeButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

Related

iOS UItableviewcell is not updating when row deleted from the backend URL

I have a problem on my objective-C iOS App.
I have a UItableview with multiple row defined with data loaded from my backend URL
This table view is a Job Table. let say that on my backend I have 3 row for this job table (photographer, architect and teacher). If i add a new job (baker), it is correctly download on the tableview when I restart my App (so 4 row on the App).
Now when I am deleting a job on my backend, the job is not deleted from the table on my iPhone. It's causing me trouble as users selecting the deleted job row are facing a crash or an error ... to remove it I have to delete the App and reload it (which is a real mess)
What would you recommend me ?
Many many thanks for your help,
Best, David
// JobsVC.m
#import "JobsVC.h"
#import "DataProvider.h"
#import "JobsTableViewCell.h"
#import "DBJobs.h"
#import "DBLinkUserJob.h"
#import <MagicalRecord/MagicalRecord.h>
#interface JobsVC ()
#property (weak, nonatomic) IBOutlet UITableView *jobsTableView;
#property (weak, nonatomic) IBOutlet UIButton *okButton;
#property (weak, nonatomic) IBOutlet UIView *selectJobsView;
#property (weak, nonatomic) IBOutlet UILabel *selectJobsTitleLabel;
#property (weak, nonatomic) IBOutlet UILabel *selectJobsDetailsLabel;
#property (strong, nonatomic) NSMutableDictionary *jobsDataSource;
#property (strong, nonatomic) NSMutableArray *selectedJobs;
#property (nonatomic, strong) UIRefreshControl *refreshControl;
#property (strong, nonatomic) NSMutableArray *jobsArray;
#end
#implementation JobsVC
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedJobs = [NSMutableArray new];
self.jobsDataSource = [NSMutableDictionary new];
self.jobsArray = [NSMutableArray new];
self.verifiedHyperjobs = [NSMutableArray new];
[self initialSetup];
[self loadJobs];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(refreshData) forControlEvents:UIControlEventValueChanged];
[self.jobsTableView addSubview:self.refreshControl];
}
-(void)viewWillAppear:(BOOL)animated{
//Editing header view according to selection mode
if (self.jobsMode == Jobs_Mode_Lb || self.jobsMode == Jobs_Mode_Ad) {
[self.selectJobsView setFrame:CGRectMake(self.selectJobsView.frame.origin.x, self.selectJobsView.frame.origin.x, self.selectJobsView.frame.size.width, 60.0)];
[self.selectJobsTitleLabel setFrame:CGRectMake(self.selectJobsTitleLabel.frame.origin.x, 19.0, self.selectJobsTitleLabel.frame.size.width, 0.0)];
[self.selectJobsDetailsLabel setFrame:CGRectMake(self.selectJobsDetailsLabel.frame.origin.x, 19.0, self.selectJobsDetailsLabel.frame.size.width, 36.0)];
[self.selectJobsTitleLabel setText:#""];
[self.selectJobsDetailsLabel setText:NSLocalizedString(#"jobs_select_ad_lb_jobs", nil)];
[self.selectJobsDetailsLabel setFont:[UIFont fontWithName:#"DIN Alternate" size:18]];
[self.selectJobsDetailsLabel setMinimumScaleFactor:0.6];
}
else{
[self.selectJobsView setFrame:CGRectMake(self.selectJobsView.frame.origin.x, self.selectJobsView.frame.origin.x, self.selectJobsView.frame.size.width, 180.0)];
[self.selectJobsTitleLabel setFrame:CGRectMake(self.selectJobsTitleLabel.frame.origin.x, 19.0, self.selectJobsTitleLabel.frame.size.width, 36.0)];
[self.selectJobsDetailsLabel setFrame:CGRectMake(self.selectJobsDetailsLabel.frame.origin.x, 63.0, self.selectJobsDetailsLabel.frame.size.width, 117.0)];
[self.selectJobsTitleLabel setText:NSLocalizedString(#"jobs_select_title", nil)];
[self.selectJobsTitleLabel setFont:[UIFont fontWithName:#"AWConquerorDidot-Light" size:24]];
[self.selectJobsTitleLabel setMinimumScaleFactor:0.6];
[self.selectJobsDetailsLabel setText:NSLocalizedString(#"jobs_select_details", nil)];
[self.selectJobsDetailsLabel setFont:[UIFont fontWithName:#"DIN Alternate" size:18]];
[self.selectJobsDetailsLabel setMinimumScaleFactor:0.6];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Helpers
- (void)initialSetup {
[self setupNavigationBar:YES];
if (self.jobsMode == Jobs_Mode_Lb || self.jobsMode == Jobs_Mode_Ad || ![LogicManager sharedInstance].currentUserID) {
[self.navigationItem setTitle:NSLocalizedString(#"jobs_ttl", nil)];
} else {
DBUsers *userDB = [DBUsers MR_findFirstByAttribute:#"userID" withValue:[LogicManager sharedInstance].currentUserID];
[self.navigationItem setTitle:userDB.login];
}
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont fontWithName:#"AWConquerorDidot-Light" size:24]}];
[self.okButton setTitle:NSLocalizedString(#"button_ok", nil) forState:UIControlStateNormal];
[self.okButton.titleLabel setFont:[UIFont fontWithName:#"DIN Alternate" size:16]];
}
#pragma mark - Logic
- (void)loadJobs {
[self.jobsDataSource removeAllObjects];
[self.jobsArray removeAllObjects];
if (!self.selectedJobs.count) {
if (self.jobsMode == Jobs_Mode_Lb) {
self.selectedJobs = [DataProvider sharedInstance].lbJobsArray;
} else if (self.jobsMode == Jobs_Mode_Ad) {
self.selectedJobs = [DataProvider sharedInstance].adJobsArray;
} else {
self.selectedJobs = [DataProvider sharedInstance].userJobsArray;
}
}
// NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(isM == NO)"];
// NSMutableArray *sources = [[DBJobs MR_findAllWithPredicate:predicate] mutableCopy];
// NSMutableArray *sources = [[DBJobs MR_findAll] mutableCopy];
NSMutableArray* sources = [[[[DBJobs MR_findAll] mutableCopy] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
if([[LogicManager sharedInstance].deviceLanguage containsString:#"fr"]){
return [NSLocalizedString([(DBJobs*)a name_fr],nil) caseInsensitiveCompare:NSLocalizedString([(DBJobs*)b name_fr],nil)];
}
else{
return [NSLocalizedString([(DBJobs*)a name],nil) caseInsensitiveCompare:NSLocalizedString([(DBJobs*)b name],nil)];
}
}] mutableCopy];
for (DBJobs *jobDB in sources) {
NSString *jobName = [[LogicManager sharedInstance].deviceLanguage containsString:#"fr"] ? jobDB.name_fr : jobDB.name;
NSString *jobIndex = [[jobName substringToIndex:1] uppercaseString];
if (![[[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)] containsObject:jobIndex]) {
NSMutableArray *mutableArray = [NSMutableArray new];
[mutableArray addObject:jobDB];
[self.jobsDataSource setValue:mutableArray forKey:jobIndex];
} else {
NSMutableArray *mutableArray = [self.jobsDataSource objectForKey:jobIndex];
if (![mutableArray containsObject:jobDB]) {
[mutableArray addObject:jobDB];
[self.jobsDataSource setValue:mutableArray forKey:jobIndex];
}
}
[self.jobsArray addObject:jobDB];
}
sortedKeys = [[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)];
}
- (void)refreshData {
[[ServiceManager sharedInstance] loadJobsWithCompletion:^(NSArray *responce, NSString *errorMessage) {
if ([self.refreshControl isRefreshing]) {
[self.refreshControl endRefreshing];
}
if (errorMessage) {
[[LogicManager sharedInstance] showAlertWithTitleKey:#"api_msg_error" AndMessage:errorMessage];
} else {
[self loadJobs];
}
}];
}
#pragma mark - Actions
- (IBAction)okButtonPressed:(id)sender {
if(self.selectedJobs.count == 0){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"jobs_select_title", nil) message:NSLocalizedString(#"jobs_no_job_msg", nil) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else{
if (self.jobsMode != Jobs_Mode_Lb && self.jobsMode != Jobs_Mode_Ad) {
BOOL hasSelectedHyperJob = FALSE;
NSMutableArray *tempArray = [NSMutableArray new];
NSMutableArray *tempHyperArray = [NSMutableArray new];
//Get selected hyperjobs for request
for(NSString *jobID in self.selectedJobs){
DBJobs *jobDB = [DBJobs MR_findFirstByAttribute:#"jobID" withValue:jobID];
if([jobDB.isM boolValue]){
hasSelectedHyperJob = TRUE;
[tempArray addObject:jobID];
}
}
//Check for verified hyperjobs in selected hyperjobs and remove them from request
for(NSString *jobID in tempArray){
for(NSString* selectedJobID in self.verifiedHyperjobs){
if([selectedJobID isEqualToString:jobID]){
[tempArray removeObject:jobID];
}
}
}
//Check for removed hyperjobs
for(NSString *jobID in self.verifiedHyperjobs){
BOOL isSelected = FALSE;
for(NSString* selectedJobID in self.selectedJobs){
if([selectedJobID isEqualToString:jobID]){
isSelected = TRUE;
}
}
if(isSelected == FALSE){
[tempHyperArray addObject:jobID];
}
}
//Remove deleted hyperjob
for (NSString *jobID in tempHyperArray){
[self.verifiedHyperjobs removeObject:jobID];
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.color = [UIColor whiteColor];
hud.labelColor = [UIColor blackColor];
hud.detailsLabelColor = [UIColor blackColor];
hud.mode = MBProgressHUDModeCustomView;
hud.customView = [LogicManager sharedInstance].HudActivity;
hud.dimBackground = YES;
//Request hyperjob
BOOL shouldReturn = FALSE;
for (NSString *jobID in tempArray){
[self.selectedJobs removeObject:jobID];
if([self.selectedJobs count] == 0){
//adding "Awaiting Status Verification" job
[self.selectedJobs addObject:#"57"];
}
shouldReturn = TRUE;
NSString *lang = #"en";
if([[LogicManager sharedInstance].deviceLanguage containsString:#"fr"]){
lang = #"fr";
}
[[ServiceManager sharedInstance] requestHyperjob:jobID forEmail:self.userEmail userID:self.userID language:lang lastName:self.userLastName andFirstName:self.userFirstName completion:^(id responce, NSString *errorMessage) {
if (!errorMessage && [responce[#"error"] intValue] == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"hint_hyperjobs", nil) message:NSLocalizedString(#"hint_hyperjobs_txt", nil) delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
//show success
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"hint_hyperjobs", nil) message:NSLocalizedString(#"api_msg_error", nil) delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}];
}
if(shouldReturn == TRUE){
return;
}
}
NSLog(#" selected jobs : %#", self.selectedJobs);
if (self.jobsMode == Jobs_Mode_Lb) {
[DataProvider sharedInstance].lbJobsArray = self.selectedJobs;
} else if (self.jobsMode == Jobs_Mode_Ad) {
[DataProvider sharedInstance].adJobsArray = self.selectedJobs;
} else {
[DataProvider sharedInstance].userJobsArray = self.selectedJobs;
}
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark - UITableView Delegate & DataSource1
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
JobsTableViewCell*cell = (JobsTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
// NSString *sectionTitle = [[[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.section];
// NSMutableArray *array = [self.jobsDataSource objectForKey:sectionTitle];
DBJobs *jobDB = [self.jobsArray objectAtIndex:indexPath.row];
[cell.jobNameLabel setFont:[UIFont fontWithName:#"DIN Alternate Bold" size:16]];
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(142.0/255.0) green:(142.0/255.0) blue:(142.0/255.0) alpha:1.0]];
if ([self.selectedJobs containsObject:jobDB.jobID]) {
if([jobDB.isM boolValue]){
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(210.0/255.0) green:(196.0/255.0) blue:(169.0/255.0) alpha:1.0]];
}
[self.selectedJobs removeObject:jobDB.jobID];
} else {
[self.selectedJobs addObject:jobDB.jobID];
[cell.jobNameLabel setFont:[UIFont fontWithName:#"DIN Alternate" size:20]];
if([jobDB.isM boolValue]){
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(168.0/255.0) green:(148.0/255.0) blue:(109.0/255.0) alpha:1.0]];
}
else{
[cell.jobNameLabel setTextColor:[UIColor blackColor]];
}
}
NSLog(#"selected jobs : %#", self.selectedJobs);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return [[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)].count;
return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// return [[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)];
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// NSString *sectionTitle = [[[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)] objectAtIndex:section];
// NSArray *array = [self.jobsDataSource objectForKey:sectionTitle];
return [self.jobsArray count];
}
//- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
// [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
// return index;
//}
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// return [[[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)] objectAtIndex:section];
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
JobsTableViewCell*cell = (JobsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"JobsTableViewCell"];
//NSString *sectionTitle = [sortedKeys objectAtIndex:indexPath.section];
// NSString *sectionTitle = [[[self.jobsDataSource allKeys] sortedArrayUsingSelector: #selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.section];
// NSMutableArray *array = [self.jobsDataSource objectForKey:sectionTitle];
DBJobs *jobDB = [self.jobsArray objectAtIndex:indexPath.row];
cell.width = self.view.width;
cell.jobNameLabel.text = [[LogicManager sharedInstance].deviceLanguage containsString:#"fr"] ? jobDB.name_fr : jobDB.name;
[cell.jobNameLabel setFont:[UIFont fontWithName:#"DIN Alternate Bold" size:16]];
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(142.0/255.0) green:(142.0/255.0) blue:(142.0/255.0) alpha:1.0]];
[cell setUserInteractionEnabled:TRUE];
if ([self.selectedJobs containsObject:jobDB.jobID]) {
[cell.jobNameLabel setFont:[UIFont fontWithName:#"DIN Alternate" size:20]];
if([jobDB.isM boolValue]){
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(168.0/255.0) green:(148.0/255.0) blue:(109.0/255.0) alpha:1.0]];
if(self.jobsMode != Jobs_Mode_Ad && self.jobsMode != Jobs_Mode_Lb){
[cell setUserInteractionEnabled:FALSE];
}
}
else{
[cell.jobNameLabel setTextColor:[UIColor blackColor]];
}
// [cell.contentView setBackgroundColor:[UIColor clearColor]];
} else {
// [cell.contentView setBackgroundColor:[UIColor grayColor]];
if([jobDB.isM boolValue]){
[cell.jobNameLabel setTextColor:[UIColor colorWithRed:(210.0/255.0) green:(196.0/255.0) blue:(169.0/255.0) alpha:1.0]];
}
}
return cell;
}
#pragma mark - UIAlertView Delegate Methods
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
[self okButtonPressed:0];
}
#end
// ServiceManager.m
#import "ServiceManager.h"
#import "LogicManager.h"
#import <MagicalRecord/MagicalRecord.h>
#import "DataProvider.h"
#import "DBJobs.h"
#import "Constants.h"
static NSString * const ApiJobsURLString = #"/api/v1/jobs/";
#pragma mark - JOBS
- (void)loadJobsWithCompletion:(void (^)(NSArray *responce,NSString * error))completion {
if (!self.isConnected) {
completion(nil, NSLocalizedString(#"msg_internet",nil));
return;
}
[self GET:ApiJobsURLString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
if (completion) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSArray *arrayResponce = [responseObject objectForKey:#"jobs"];
for (NSDictionary *dict in arrayResponce) {
[self saveJob:dict];
}
}
completion(responseObject, nil);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
if (!errorData) {
(error.code == -1005 || error.code == -1009) ? completion(nil, NSLocalizedString(#"msg_internet",nil)) : completion(nil, NSLocalizedString(error.description,nil));
return;
}
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
NSString *errorMessage = [serializedData objectForKey:#"message"];
if (completion) {
completion(nil, NSLocalizedString(errorMessage,nil));
}
}];
}

how to add search filter in uitableview

I am working on UITableView where I have a list of contacts. All the contacts are coming form a webservice in JSON format. I already parse it in my tableView, now I want to add a search logic to search contacts.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ChatListCell *cell=[tableView dequeueReusableCellWithIdentifier:#"contactListCell"];
if(!cell){
cell = [[ChatListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"contactListCell"];
}
ChatListData *chatlistData = [chatList objectAtIndex:indexPath.row];
NSString *txtName = chatlistData.name;
NSLog(#"name %#", txtName);
NSData *emojiData = [txtName dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiTxtName = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
cell.txtName.text = emojiTxtName;
cell.txtTime.text = chatlistData.time;
NSString *stringImg = #"image";
NSString *stringVideo = #"video";
if(![chatlistData.mime_type isEqual: [NSNull null]]){
if ([chatlistData.mime_type rangeOfString:stringImg].location == NSNotFound || [chatlistData.mime_type rangeOfString:stringVideo].location == NSNotFound) {
if ([chatlistData.mime_type rangeOfString:stringImg].location == NSNotFound) {
cell.txtMsg.text = #"Image";
}else if([chatlistData.mime_type rangeOfString:stringVideo].location == NSNotFound){
cell.txtMsg.text = #"Video";
}
}else {
NSString *txtMsg = chatlistData.body;
NSData *emojiData = [txtMsg dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiTxtMsg = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
cell.txtMsg.text = emojiTxtMsg;
}
}else {
NSString *txtMsg = chatlistData.body;
NSData *emojiData = [txtMsg dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiTxtMsg = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
cell.txtMsg.text = emojiTxtMsg;
}
return cell;
}
- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//http://app.wazapper.com/api/users/inbox/45/#api_917838828123
ChatListData *chatlistData = [chatList objectAtIndex:indexPath.row];
if(![chatlistData.contact isEqual: [NSNull null]]){
NSString *chatUrl = [NSString stringWithFormat:#"http://app.wazapper.com/api/users/inbox/%#/#api_%#", [[User sharedInstance] userId], chatlistData.contact];
chatViewController.chatUrl = chatUrl;
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangeChatList" object:chatUrl];
}
}
this is my little piece of code, please give me some idea about it, I have already added the search bar in UITableView, I just need a logic for this purpose.
First add UISearchBarDelegate to your ViewController, then add this method to filter your search.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSPredicate *pre0 = [NSPredicate predicateWithFormat:#"Fname contains[cd] %#", searchText];
NSPredicate *pre1 = [NSPredicate predicateWithFormat:#"Lname contains[cd] %#", searchText];
NSPredicate *preAll = [NSCompoundPredicate orPredicateWithSubpredicates:#[pre0, pre1]];
NSArray *filterAry= [jsonAry filteredArrayUsingPredicate:preAll];
}
UI implementation:
- (void)setupSearchBar {
CGFloat ySearchBarPosition = self.navigationController.navigationBar.bounds.size.height;
CGRect viewBounds = self.view.bounds;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetMinX(viewBounds), ySearchBarPosition, CGRectGetWidth(viewBounds), kHeightDefault44)];
self.searchBar.backgroundImage = [[UIImage alloc] init];
self.searchBar.barTintColor = kYourBarTintColor;
self.searchBar.tintColor = kYourTintColor;
self.searchBar.delegate = self;
self.searchBar.placeholder = NSLocalizedString(#"searchBar.placeholder", nil);
self.searchBar.showsCancelButton = YES;
for (UIView *subview in self.searchBar.subviews) {
for (UIView *subSubview in subview.subviews) {
if ([subSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
UITextField *textField = (UITextField *)subSubview;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = kColorUnregisteredDevicesSearchBarBackground;
textField.textColor = kColorUnregisteredDevicesSearchBarText;
}
if ([subSubview isKindOfClass:NSClassFromString(#"UINavigationButton")]) {
UIButton *cancelBarButton = (UIButton *)subSubview;
cancelBarButton.tintColor = kColorUnregisteredDevicesSearchBarCancelButtonTintColor;
}
}
}
self.tableView.tableHeaderView = self.searchBar;
self.tableView.contentOffset = CGPointMake(0.0, self.tableView.tableHeaderView.bounds.size.height);
}
call this method where you create your tableView, before this line of code:
[self.view addSubview:self.yourTableView];
Functionality implementation:
1. You have to add UISearchBarDelegate on your .h class.
2. Use this delegation methods:
// delegation methods
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self reloadModelAndTable];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
self.searchBar.text = #"";
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.searchBar resignFirstResponder];
}
// private methods
- (void)reloadModelAndTable:(NSTimer *)timer {
[self filterModelForSearch:self.searchBar.text];
[self.tableView reloadData];
}
- (void)filterModelForSearch:(NSString *)searchText {
NSMutableArray *records = (NSMutableArray *)[self.unregisteredDevicesList mutableCopy];
NSIndexSet *resultSet = [records indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
YourModel *model = (YourModel *)obj;
BOOL hasObjects = [self resultHasObjects:model withSearchText:searchText];
return hasObjects;
}];
self.yourArrayWhichPopulatesTheTable = [[records objectsAtIndexes:resultSet] mutableCopy];
}
- (BOOL)resultHasObjects:(YourModel *)model withSearchText:(NSString *)searchText{
// I consider you make search for name
return ((model.name.length > 0) && [self containsString:model.name searchText:searchText]);
}
- (BOOL)containsString:(NSString *)haystack searchText:(NSString *)needle {
if (haystack) {
NSRange range = [haystack rangeOfString:needle options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
return range.location != NSNotFound;
}
return NO;
}
This code works perfect. Good luck! If something is not clear, please let me know.:)

not able to search the data?

I have added search bar for collection view using programatically .But when i enter the names to search nothing shows.I have some section also and each section contains some data.
Here is my code:
#interface ViewController ()
{
NSMutableArray *arrayPDFName;
NSMutableArray *titleArray;
}
#property (nonatomic,strong) NSArray *dataSourceForSearchResult;
#property (nonatomic) BOOL searchBarActive;
#property (nonatomic) float searchBarBoundsY;
#property (nonatomic,strong) UISearchBar *searchBar;
#property (nonatomic,strong) UIRefreshControl *refreshControl;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSourceForSearchResult = [NSArray new];
titleArray = [NSMutableArray array];
[self getdata];
self.mycollectionView.dataSource = self;
self.mycollectionView.delegate = self;
[self.mycollectionView reloadData];
}
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:#"%02x",result[i]];
}
return ret;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self prepareUI];
}
-(void)getdata {
NSString *userName = #“username#yahoo.com";
NSString *password = #“password”;
NSData *plainData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
base64String=[self sha256HashFor: base64String];
NSString *urlString = #"https://api.exampleport/user/orderid/files";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"GET"];
// basic authendication
NSString *authStr = [NSString stringWithFormat:#"%#:%#", userName, base64String];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
//header-field
NSString *authValue = [NSString stringWithFormat:#"Basic %#", [authData base64Encoding]];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSError * error;
self->arrayPDFName = [[NSMutableArray alloc]init];
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dictOriginal = jsonResults[#“dark”];
[titleArray addObject:[NSString stringWithFormat:#"dark(%#)”, dictOriginal[#"count"]]];
NSDictionary *dictOriginal2 = jsonResults[#"orange”];
[titleArray addObject:[NSString stringWithFormat:#"orange(%#)”, dictOriginal2[#"count"]]];
NSDictionary *dictOriginal3 = jsonResults[#"pencill”];
[titleArray addObject:[NSString stringWithFormat:#"pencill(%#)”, dictOriginal3[#"count"]]];
NSDictionary *dictOriginal4 = jsonResults[#"smart”];
[titleArray addObject:[NSString stringWithFormat:#"smart(%#)”, dictOriginal4[#"count"]]];
NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];
NSLog(#"str: %#", titleArray);
for (NSDictionary *dict in arrayFiles) {
NSMutableArray *arr = [NSMutableArray array];
NSArray *a = dict[#"files"];
for(int i=0; i < a.count; i ++) {
NSString *strName = [NSString stringWithFormat:#"%#",[[dict[#"files"] objectAtIndex:i] valueForKey:#"name"]];
// NSLog(#"str: %#", strName);
[arr addObject:strName];
}
[arrayPDFName addObject:arr];
}
//Get plist path
NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory1 stringByAppendingPathComponent:#"SampleData.plist"];
//NSLog(#"plistPath : %#",plistPath);
//Save data from json to plist
NSString *error1;
returnData = [NSPropertyListSerialization dataFromPropertyList:jsonResults
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(returnData ) {
if ([returnData writeToFile:plistPath atomically:YES]) {
NSLog(#"Data successfully saved.");
}else {
NSLog(#"Did not managed to save NSData.");
}
}
else {
NSLog(#"%#",errorDesc);
}
// NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];
NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// NSLog(#"str: %#", stringsDictionary);
}
//delegate method for header
-(UICollectionReusableView*) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
HeaderView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"myHeader" forIndexPath:indexPath];
header.myHeaderLabel.text = titleArray[indexPath.section];
return header;
}
#pragma mark - UICollectionView Datasource
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return titleArray.count;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.searchBarActive) {
return self.dataSourceForSearchResult.count;
}
NSArray *a = arrayPDFName[section];
if (a.count == 0)
{
return 1;
}
else
{
return a.count;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(0, 80);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath {
VenueCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"mycell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.imgV.image = [UIImage imageNamed:#"doc1.png"];
if (self.searchBarActive) {
cell.myLabel.text = _dataSourceForSearchResult[indexPath.section][indexPath.row];
}else{
NSArray *a = arrayPDFName[indexPath.section];
if (a.count == 0) {
cell.myLabel.text = #"NO DATA";
return cell;
}
cell.myLabel.text = a[indexPath.row];
}
return cell;
}
//////////////////////////for search bar //////////
#pragma mark - actions
-(void)refreashControlAction{
[self cancelSearching];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// stop refreshing after 2 seconds
[self.mycollectionView reloadData];
[self.refreshControl endRefreshing];
});
}
#pragma mark - search
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"self contains[c] %#", searchText];
self.dataSourceForSearchResult = [self->arrayPDFName filteredArrayUsingPredicate:resultPredicate];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
// user did type something, check our datasource for text that looks the same
if (searchText.length>0) {
// search and reload data source
self.searchBarActive = YES;
[self filterContentForSearchText:searchText
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
[self.mycollectionView reloadData];
}else{
// if text lenght == 0
// we will consider the searchbar is not active
self.searchBarActive = NO;
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self cancelSearching];
[self.mycollectionView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
self.searchBarActive = YES;
[self.view endEditing:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
// we used here to set self.searchBarActive = YES
// but we'll not do that any more... it made problems
// it's better to set self.searchBarActive = YES when user typed something
[self.searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// this method is being called when search btn in the keyboard tapped
// we set searchBarActive = NO
// but no need to reloadCollectionView
self.searchBarActive = NO;
[self.searchBar setShowsCancelButton:NO animated:YES];
}
-(void)cancelSearching{
self.searchBarActive = NO;
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
#pragma mark - prepareVC
-(void)prepareUI{
[self addSearchBar];
[self addRefreshControl];
}
-(void)addSearchBar{
if (!self.searchBar) {
self.searchBarBoundsY = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,self.searchBarBoundsY, [UIScreen mainScreen].bounds.size.width, 50)];
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.tintColor = [UIColor whiteColor];
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Search here";
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
// add KVO observer.. so we will be informed when user scroll colllectionView
[self addObservers];
}
if (![self.searchBar isDescendantOfView:self.view]) {
[self.view addSubview:self.searchBar];
}
}
-(void)addRefreshControl{
if (!self.refreshControl) {
self.refreshControl = [UIRefreshControl new];
self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:#selector(refreashControlAction)
forControlEvents:UIControlEventValueChanged];
}
if (![self.refreshControl isDescendantOfView:self.mycollectionView]) {
[self.mycollectionView addSubview:self.refreshControl];
}
}
-(void)startRefreshControl{
if (!self.refreshControl.refreshing) {
[self.refreshControl beginRefreshing];
}
}
Where i am doing mistake and what i need to change to work.Thanks in advance!
It seems your arrayPDFName have array object and each array object have string object. There are two ways to filter you array.
Way - 1
If you want to get the list of array which has the search string the use SUBQUERY with your predicate. Like below.
NSArray *a1 = #[#"a1", #"a2", #"a3"];
NSArray *a2 = #[#"a1", #"a2", #"a3"];
NSArray *a3 = #[#"a1", #"a2", #"a3"];
NSArray *a4 = #[#"a2", #"a3"];
NSArray *allA = #[a1, a2, a3, a4];
NSPredicate *pre = [NSPredicate predicateWithFormat:#"SUBQUERY(SELF, $a, $a == %#).#count > 0", #"a1"];
NSArray *a = [allA filteredArrayUsingPredicate:pre];
Way - 2
If you want to get the search string alone from each array object then follow the below,
NSMutableArray *fa = [[NSMutableArray alloc] init];
for (NSArray *a in allA) {
NSPredicate *p = [NSPredicate predicateWithFormat:#"self CONTAINS[c] %#", #"a3"];
[fa addObjectsFromArray:[a filteredArrayUsingPredicate:p]];
}
Note: Above code done with same sample data. Replace it as you want.
As per your code replace the below code
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSMutableArray *fa = [[NSMutableArray alloc] init];
for (NSArray *a in arrayPDFName) {
NSPredicate *p = [NSPredicate predicateWithFormat:#"self CONTAINS[c] %#", searchText];
[fa addObjectsFromArray:[a filteredArrayUsingPredicate:p]];
}
self.dataSourceForSearchResult = [NSArray arrayWithArray:fa];
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if(self.searchBarActive)
return 1;
return titleArray.count;
}
In cellForItemAtIndexPath
cell.myLabel.text = _dataSourceForSearchResult[indexPath.row];

UITableview Delegate methods are not executing

i tried the following code for searching in epub ,Its not working the reason is
1.The table delegate methode executing at startup so the default value of search results is 0 ,so i give one dummy array with 4 elements in viewdidload method.So now tableview displaying only 4 thode elements in dummy array and when i scroll the tableview it displaying correct search results but still it showing only 4 elemets in that search results because number of rows methode is not execute while scrolling.
when i click search button it will call this methode in first class
SearchResultsViewController *searchRes=[[SearchResultsViewController alloc]initWithNibName:#"SearchResultsViewController" bundle:nil];
NSString *searchQuery=[search text];
sharedManager=[Mymanager sharedManager];
sharedManager.searchQuery=searchQuery;
// UITextField *textField = [NSString stringWithFormat:#"%#",searchQuery];
// [textField setFont:[UIFont fontWithName:#"BL-Ruthika-Bold" size:15]];
[searchRes searchString:searchQuery];
[self.navigationController pushViewController:searchRes animated:YES];
then it calls following methods in searchresult class
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"%d",[results count]);
if([results count]>0)
{
return [results count];
}
else
{
return [test count];
}
}
//executes only at the startup time ,so the value of results always become zero
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if([results count]>0) {
// cell.textLabel.adjustsFontSizeToFitWidth = YES;
hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"...%#...", hit.neighboringText];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Chapter %d - page %d", hit.chapterIndex, hit.pageIndex+1];
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:13];
cell.textLabel.textColor = [UIColor colorWithRed:25/255.0 green:90/255.0 blue:100/255.0 alpha:1];
cell.detailTextLabel.textColor=[UIColor colorWithRed:25/255.0 green:90/255.0 blue:100/255.0 alpha:1];
cell.detailTextLabel.font= [UIFont fontWithName:#"Trebuchet MS" size:10];
return cell;
}
else
{
cell.textLabel.text=[test objectAtIndex:indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
sharedManager=[Mymanager sharedManager];
hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
sharedManager.searchFlag=YES;
sharedManager.hitPageNumber=hit.pageIndex;
sharedManager.hitChapter=hit.chapterIndex;
sharedManager.hit=hit;
// [fvc loadSpine:hit.chapterIndex atPageIndex:hit.pageIndex highlightSearchResult:hit];
[self.navigationController popViewControllerAnimated:YES];
}
- (void) searchString:(NSString*)query{
currentQuery=sharedManager.searchQuery;
[self searchString:currentQuery inChapterAtIndex:0];
[[self resultsTableView]reloadData];
}
- (void) searchString:(NSString *)query inChapterAtIndex:(int)index{
currentChapterIndex = index;
sharedManager=[Mymanager sharedManager];
Chapter* chapter = [sharedManager.spineArray objectAtIndex:index];
NSRange range = NSMakeRange(0, chapter.text.length);
NSLog(#"%#",sharedManager.searchQuery);
range = [chapter.text rangeOfString:sharedManager.searchQuery options:NSCaseInsensitiveSearch range:range locale:nil];
int hitCount=0;
while (range.location != NSNotFound) {
range = NSMakeRange(range.location+range.length, chapter.text.length-(range.location+range.length));
range = [chapter.text rangeOfString:sharedManager.searchQuery options:NSCaseInsensitiveSearch range:range locale:nil];
hitCount++;
}
if(hitCount!=0){
UIWebView* webView = [[UIWebView alloc] initWithFrame:chapter.windowSize];
[webView setDelegate:self];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:chapter.spinePath]];
[webView loadRequest:urlRequest];
} else {
if((currentChapterIndex+1)<[sharedManager.spineArray count]){
[self searchString:sharedManager.searchQuery inChapterAtIndex:(currentChapterIndex+1)];
} else {
fvc.searching = NO;
}
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
// NSLog(#"%#", error);
[webView release];
}
- (void) webViewDidFinishLoad:(UIWebView*)webView{
sharedManager=[Mymanager sharedManager];
NSString *varMySheet = #"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = #"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 = [NSString stringWithFormat:#"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:#"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",[[sharedManager.spineArray objectAtIndex:currentChapterIndex] fontPercentSize]];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView highlightAllOccurencesOfString:sharedManager.searchQuery];
NSString* foundHits = [webView stringByEvaluatingJavaScriptFromString:#"results"];
NSLog(#"%#", foundHits);
NSMutableArray* objects = [[NSMutableArray alloc] init];
NSArray* stringObjects = [foundHits componentsSeparatedByString:#";"];
for(int i=0; i<[stringObjects count]; i++){
NSArray* strObj = [[stringObjects objectAtIndex:i] componentsSeparatedByString:#","];
if([strObj count]==3){
[objects addObject:strObj];
}
}
NSArray* orderedRes = [objects sortedArrayUsingComparator:^(id obj1, id obj2){
int x1 = [[obj1 objectAtIndex:0] intValue];
int x2 = [[obj2 objectAtIndex:0] intValue];
int y1 = [[obj1 objectAtIndex:1] intValue];
int y2 = [[obj2 objectAtIndex:1] intValue];
if(y1<y2){
return NSOrderedAscending;
} else if(y1>y2){
return NSOrderedDescending;
} else {
if(x1<x2){
return NSOrderedAscending;
} else if (x1>x2){
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
}];
[objects release];
for(int i=0; i<[orderedRes count]; i++){
NSArray* currObj = [orderedRes objectAtIndex:i];
SearchResult* searchRes = [[SearchResult alloc] initWithChapterIndex:currentChapterIndex pageIndex:([[currObj objectAtIndex:1] intValue]/webView.bounds.size.height) hitIndex:0 neighboringText:[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"unescape('%#')", [currObj objectAtIndex:2]]] originatingQuery:sharedManager.searchQuery];
[results addObject:searchRes];
[searchRes release];
}
[[self resultsTableView]reloadData];
//Print results
for(int i=0;i<[results count];i++)
{
hit = (SearchResult*)[results objectAtIndex:i];
}
[resultsTableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
if((currentChapterIndex+1)<[sharedManager.spineArray count]){
[self searchString:sharedManager.searchQuery inChapterAtIndex:(currentChapterIndex+1)];
} else {
fvc.searching= NO;
}
[[self resultsTableView]reloadData];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
resultsTableView=[[UITableView alloc]init];
[resultsTableView setDelegate:self];
[resultsTableView setDataSource:self];
sharedManager=[Mymanager sharedManager];
// Do any additional setup after loading the view from its nib.
results = [[NSMutableArray alloc] init];
test=[[NSArray alloc]initWithObjects:#"one",#"one",#"one",#"one",nil];
self.navigationItem.title=#"Search ";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem=backButton;
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor lightGrayColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Trebuchet MS" size:15.0f]
}];
[self searchString:sharedManager.searchQuery];
noMatchingSearch=[[NSArray alloc]initWithObjects:#"No Element Found", nil];
tableOfContents=[[NSMutableArray alloc]init];
for (id img in search.subviews)
{
if ([img isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[img removeFromSuperview];
}
}
tableOfContents=[sharedManager.List copy];
}
- (void)viewDidUnload
{
search = nil;
[super viewDidUnload];
resultsTableView.delegate=self;
resultsTableView.dataSource=self;
// Release any retained subviews of the main view.
self.resultsTableView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
When you have received your search results call
[self.tableView reloadData];
To tell the table view to update itself. I can see you have tried it a number of times in the large amount of code above. You only need to call it once, when you have your search results ready. Also, ensure your reference to the table view is valid when you call it.
Also, if you're creating a table view in an XIB file, then the second one you're creating in viewDidLoad and not showing (adding as a subview) is just confusing you and you're trying to reload the wrong table view.
If you still have problems, show the class properties and remove all the code tat isn't to do with the table view.
resultsTableView=[[UITableView alloc]init];
Try to use resultsTableView, with a property, and set it's to (nonatomic, strong).

UITableview cellForRowAtIndexPath method is not executing?

When i click search it executes a search class and after finding the results it executes numberOfRows method but then it is showing empty table. It is not executing cellForRowAtIndexPath method .
check my below code
code in viewcontroller.h
when i click search button this method will get executed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
SearchResultsViewController *searchRes=[[SearchResultsViewController alloc]initWithNibName:#"SearchResultsViewController" bundle:nil];
NSString *searchQuery=[search text];
sharedManager=[Mymanager sharedManager];
sharedManager.searchQuery=searchQuery;
[searchRes searchString:searchQuery];
[self.navigationController pushViewController:searchRes animated:YES];
}
in search class
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"%#",results);
return [results count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.adjustsFontSizeToFitWidth = YES;
NSLog(#"indexpath%d",indexPath.row);
NSLog(#"%#",[results objectAtIndex:[indexPath row]]);
SearchResult* hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
NSLog(#"%#",hit.neighboringText);
cell.textLabel.text = [NSString stringWithFormat:#"...%#...", hit.neighboringText];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Chapter %d - page %d", hit.chapterIndex, hit.pageIndex+1];
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:12];
cell.textLabel.textColor = [UIColor colorWithRed:25/255.0 green:90/255.0 blue:100/255.0 alpha:1];
// else
// {
//
// }
// [[self resultsTableView] reloadData];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SearchResult* hit = (SearchResult*)[results objectAtIndex:[indexPath row]];
[fvc loadSpine:hit.chapterIndex atPageIndex:hit.pageIndex highlightSearchResult:hit];
}
- (void) searchString:(NSString*)query{
// if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// {
self.results = [[NSMutableArray alloc] init];
[resultsTableView reloadData];
currentQuery=sharedManager.searchQuery;
//
[self searchString:currentQuery inChapterAtIndex:0];
//
// }else {
// currentQuery=sharedManager.searchQuery;
// [self searchString:currentQuery inChapterAtIndex:0];
//}
}
- (void) searchString:(NSString *)query inChapterAtIndex:(int)index{
currentChapterIndex = index;
sharedManager=[Mymanager sharedManager];
Chapter* chapter = [sharedManager.spineArray objectAtIndex:index];
NSLog(#"%d",chapter.text.length);
NSRange range = NSMakeRange(0, chapter.text.length);
NSLog(#"%#",sharedManager.searchQuery);
range = [chapter.text rangeOfString:sharedManager.searchQuery options:NSCaseInsensitiveSearch range:range locale:nil];
int hitCount=0;
while (range.location != NSNotFound) {
range = NSMakeRange(range.location+range.length, chapter.text.length-(range.location+range.length));
range = [chapter.text rangeOfString:sharedManager.searchQuery options:NSCaseInsensitiveSearch range:range locale:nil];
hitCount++;
}
if(hitCount!=0){
UIWebView* webView = [[UIWebView alloc] initWithFrame:chapter.windowSize];
[webView setDelegate:self];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:chapter.spinePath]];
[webView loadRequest:urlRequest];
} else {
if((currentChapterIndex+1)<[sharedManager.spineArray count]){
[self searchString:sharedManager.searchQuery inChapterAtIndex:(currentChapterIndex+1)];
} else {
fvc.searching = NO;
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"%#", error);
[webView release];
}
- (void) webViewDidFinishLoad:(UIWebView*)webView{
NSString *varMySheet = #"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = #"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 = [NSString stringWithFormat:#"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:#"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",[[sharedManager.spineArray objectAtIndex:currentChapterIndex] fontPercentSize]];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView highlightAllOccurencesOfString:sharedManager.searchQuery];
NSString* foundHits = [webView stringByEvaluatingJavaScriptFromString:#"results"];
NSLog(#"%#", foundHits);
NSMutableArray* objects = [[NSMutableArray alloc] init];
NSArray* stringObjects = [foundHits componentsSeparatedByString:#";"];
for(int i=0; i<[stringObjects count]; i++){
NSArray* strObj = [[stringObjects objectAtIndex:i] componentsSeparatedByString:#","];
if([strObj count]==3){
[objects addObject:strObj];
}
}
NSArray* orderedRes = [objects sortedArrayUsingComparator:^(id obj1, id obj2){
int x1 = [[obj1 objectAtIndex:0] intValue];
int x2 = [[obj2 objectAtIndex:0] intValue];
int y1 = [[obj1 objectAtIndex:1] intValue];
int y2 = [[obj2 objectAtIndex:1] intValue];
if(y1<y2){
return NSOrderedAscending;
} else if(y1>y2){
return NSOrderedDescending;
} else {
if(x1<x2){
return NSOrderedAscending;
} else if (x1>x2){
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
}];
[objects release];
NSLog(#"%#",currentQuery);
for(int i=0; i<[orderedRes count]; i++){
NSArray* currObj = [orderedRes objectAtIndex:i];
SearchResult* searchRes = [[SearchResult alloc] initWithChapterIndex:currentChapterIndex pageIndex:([[currObj objectAtIndex:1] intValue]/webView.bounds.size.height) hitIndex:0 neighboringText:[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"unescape('%#')", [currObj objectAtIndex:2]]] originatingQuery:currentQuery];
[results addObject:searchRes];
NSLog(#"%#",results);
[searchRes release];
}
//Print results
for(int i=0;i<[results count];i++)
{
SearchResult* hit = (SearchResult*)[results objectAtIndex:i];
NSLog(#"%#",hit.neighboringText);
}
[resultsTableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
if((currentChapterIndex+1)<[sharedManager.spineArray count]){
[self searchString:sharedManager.searchQuery inChapterAtIndex:(currentChapterIndex+1)];
} else {
fvc.searching= NO;
}
[[self resultsTableView] reloadData];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//- (void)dealloc
////{
//// self.resultsTableView = nil;
//// //[results release];
//// //[currentQuery release];
//// [super dealloc];
//}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
resultsTableView=[[UITableView alloc]init];
[resultsTableView setDelegate:self];
[resultsTableView setDataSource:self];
sharedManager=[Mymanager sharedManager];
// Do any additional setup after loading the view from its nib.
results = [[NSMutableArray alloc] init];
self.navigationItem.title=#"Search ";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem=backButton;
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor lightGrayColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Trebuchet MS" size:15.0f]
}];
noMatchingSearch=[[NSArray alloc]initWithObjects:#"No Element Found", nil];
tableOfContents=[[NSMutableArray alloc]init];
for (id img in search.subviews)
{
if ([img isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[img removeFromSuperview];
}
}
tableOfContents=[sharedManager.List copy];
}
- (void)viewDidUnload
{
search = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
self.resultsTableView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
MY SEARCH RESULTS
"<SearchResult: 0x113482c0>",
"<SearchResult: 0x11348a20>",
"<SearchResult: 0x88c0a50>"
The problem is with the search method before search the array is reinitialised and hence 0 returned in the resultArray and hence no value in tables
- (void) searchString:(NSString*)query{
self.results = [[NSMutableArray alloc] init];//Here it becomes 0!!
[resultsTableView reloadData];
currentQuery=sharedManager.searchQuery;
}
To make it right ,After the search is performed i believe
[self searchString:currentQuery inChapterAtIndex:0];
load the value in the result array and then reload table.
Make sure
in .h add
<UITableViewDataSource,UITableViewDelegate>
in nib
connect datasource and delegate[if via nib]

Resources