moveRowAtIndexPath doesn't get called - ios

My code doesn't enter the moveRowAtIndexPath part. But it enters into canMoveRowAtIndexPath.
When I click minus, "delete" doesn't appear. How can I solve this? Thank you.
ps:I try to make it work for not the tableview with the SearchDisplayController.
In .h file : UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate
`/*viewdidload*/`
self.editbtn addTarget:self action:#selector(toggleEditMode:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)toggleEditMode:(id)sender {
if(self.alerjitableview.isEditing){
[self.alerjitableview setEditing:NO animated:YES];
[self.editbtn setTitle:#"Düzenle" forState:UIControlStateNormal];
}else{
[self.alerjitableview setEditing:YES animated:YES];
[self.editbtn setTitle:#"Bitti" forState:UIControlStateNormal];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if(tableView== self.alerjitableview)
return[hastaninalerjileri count];
else{
if(tableView == self.searchDisplayController.searchResultsTableView)
if (isSearching) {
return [tanilist count];
} else {
return [list count];
}
else
return [favTanilar count];
}
}
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)targetIndexPath
{
//if(tableView == self.searchDisplayController.searchResultsTableView)
if(tableView== self.alerjitableview){
NSUInteger sourceIndex = [sourceIndexPath row];
NSUInteger targetIndex = [targetIndexPath row];
if (sourceIndex != targetIndex)
{
[hastaninalerjileri exchangeObjectAtIndex:sourceIndex
withObjectAtIndex:targetIndex];
}
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if(tableView== self.alerjitableview){
[hastaninalerjileri removeObjectAtIndex:[indexPath row]];
// Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.alerjitableview deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
[self.alerjitableview reloadData];
}
else {
if (tableView == self.taniView)
{
[hastanintanilari removeObjectAtIndex:[indexPath row]];
// Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.taniTableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.0;
}
- (UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
TaniModel *taniInfo =[[TaniModel alloc]init];
if (isSearching && [tanilist count]) {
taniInfo = [tanilist objectAtIndex:indexPath.row];
} else {
taniInfo = [list objectAtIndex:indexPath.row];
}
if(tableView == self.alerjitableview){
OrderAlerjiModel *alerjimodel =[hastaninalerjileri objectAtIndex:indexPath.row];
cell.textLabel.text = alerjimodel.Ad;
if((indexPath.row % 2) ==0)
cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];
return cell;
}
else{
if(tableView == self.searchDisplayController.searchResultsTableView){
cell.textLabel.text = taniInfo.Name;
cell.textLabel.tag=[NSString stringWithFormat:#"%d",taniInfo.Id];
if((indexPath.row % 2) ==0)
cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];
NSAttributedString *attributedText =[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:#"%# ",cell.textLabel.text ] attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:14] }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){242/2, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize nameSize = rect.size;
if (nameSize.height <=25)
cell.textLabel.numberOfLines=1;
else if(nameSize.height<=50)
cell.textLabel.numberOfLines=2;
else if(nameSize.height<=100)
cell.textLabel.numberOfLines=3;
return cell;
}else{
FavoriTaniModel *d = [favTanilar objectAtIndex:indexPath.row];
cell.textLabel.text =d.TaniName;
cell.tag = [NSString stringWithFormat:#"%d",d.TaniId];
if((indexPath.row % 2) ==0)
cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];
NSAttributedString *attributedText =[[NSAttributedString alloc]initWithString:cell.textLabel.text attributes:#{
NSFontAttributeName: [UIFont systemFontOfSize:14]
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){242/2, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize nameSize = rect.size;
cell.textLabel.frame =CGRectMake(30, 0, 242, nameSize.height);
if (nameSize.height <=25)
cell.textLabel.numberOfLines=1;
else if(nameSize.height<=50)
cell.textLabel.numberOfLines=2;
else if(nameSize.height<=100)
cell.textLabel.numberOfLines=3;
return cell;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.taniTableView){
if(tableView == self.searchDisplayController.searchResultsTableView){
TaniModel *model=[tanilist objectAtIndex:indexPath.row];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"0",#"Id",#"0",#"OrderId",[NSString stringWithFormat:#"%d",model.Id],#"TaniId",model.Name,#"Ad",#"1",#"OrderBy", nil];
OrderTaniModel *orderalerji = [[OrderTaniModel alloc]initWithDictionary:dict];
[hastanintanilari addObject:orderalerji];
[self.taniTableView reloadData];
[self.searchDisplayController.searchResultsTableView setHidden:TRUE];
}
}
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
Minus appears when return YES in upper method
In method
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
you allow move cell from 1 indexPath to another indexPath.
And you ought to be delegate and datasource to this tableView

Related

Checkmark multiple rows by loading NSUserDefaults

I'm asking the user to select options in a UITableView and then saving the selection in NSUserDefaults. I'm making sure that if the user selects an option twice, it gets added only once..i.e no duplicates.
I'm loading the NSUserDefaults and then trying to show the rows as "checked"..so that the user remembers what he/she had selected when they had run the app earlier.
Now, I know that only the cellForRowAtIndexPath method can set a checkmark.
The below code only checkmarks the last object. But how do I get multiple checkmarks?
This is how far I've gotten.
- (void)viewDidLoad {
[super viewDidLoad];
appSettings = [NSUserDefaults standardUserDefaults];
self.title = #"Select News Categories";
_selectedCategoriesArray = [[NSMutableArray alloc] init];
[self initNewsCategories];
[self loadNewsSelectedCategories];
}
#pragma mark UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_newsCategoriesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellNewsCategory" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:#"%#",[_newsCategoriesArray objectAtIndex:indexPath.row]];
NSString * cellText = [[cell textLabel] text];
for (NSString * lbl in _selectedNewsCategories)
{
if ([cellText isEqualToString:lbl])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
return cell;
}
#pragma mark UITableViewDelegate methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString * selectedCategory = [[selectedCell textLabel] text];
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone)
{
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(#"%#", selectedCategory);
[_selectedCategoriesArray addObject:selectedCategory];
NSLog(#"_selectedCategoriesArray:%#", _selectedCategoriesArray);
[appSettings setObject:_selectedCategoriesArray forKey:#"selectedNewsCategories"];
}
else if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
NSLog(#"%#", selectedCategory);
[_selectedCategoriesArray removeObject:selectedCategory];
NSLog(#"_selectedCategoriesArray:%#", _selectedCategoriesArray);
[appSettings setObject:_selectedCategoriesArray forKey:#"selectedNewsCategories"];
}
else
{
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
[appSettings synchronize];
}
#pragma mark Helper methods
-(void) initNewsCategories
{
_newsCategoriesArray = [NSArray arrayWithObjects:#"Arts",#"Business",#"Company",
#"Entertainment",#"Environment",#"Health",#"Lifestyle",
#"Media",#"Money",#"Most Read",#"Trending",#"World",nil];
}
-(void) loadNewsSelectedCategories
{
_selectedNewsCategories = [[NSMutableArray alloc] init];
_selectedNewsCategories = [appSettings objectForKey:#"selectedNewsCategories"];
}
You have a problem in this code:
for (NSString * lbl in _selectedNewsCategories)
{
if ([cellText isEqualToString:lbl])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
You first checkmark cell but then you continue search and unmark cell.
You need to use this:
for (NSString * lbl in _selectedNewsCategories)
{
if ([cellText isEqualToString:lbl])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
break;
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
Or this
if([_selectedNewsCategories indexOfObject: cellText] != NSNotFound)
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}

Q:Get rows count by indexPath of tableView comes `Trying to put the stack in unreadable memory at:` error

In my tableView delegate methods:
I get numberOfRows and numberOfSections in my tableView delegate methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ,
but in the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method, I tried this method:
NSString *key = [NSString stringWithFormat:#"%ld", section];
In this line , it shows the error:Thread 1:EXC_BAD_ACCESS(code=2,address=0x)
and I taken the (lldb):
(lldb) po [NSString stringWithFormat:#"%ld", section]
error: Trying to put the stack in unreadable memory at: 0x7fff50739eb0.
If I don't get numberOfRows and numberOfSections in my tableView delegate methods:
the error will not appear.
The below is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [tableView numberOfSections];
if (indexPath.row == 0 && numberOfRows != 1) {
cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [tableView numberOfSections];
if (indexPath.row == 0 && numberOfRows != 1) {
return 40;
}else {
return 5;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSString *key = [NSString stringWithFormat:#"%d", (int)section];
NSString *key = [NSString stringWithFormat:#"%ld", section]; // this line shows the error.
BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];
if (section == 0) {
return folded?_arr.count:1;
} else if (section == 1) {
return folded?_arr.count:1;
} else if (section == 2) {
return folded?_arr.count:1;
} else {
return folded?_arr.count:0;
}
}
This is not a error caused by this code:
NSString *key = [NSString stringWithFormat:#"%ld", section];
Your code is run in an endless loop and end by the system. Look at the below:
When you call:
NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [tableView numberOfSections];
in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, this will case an endless loop.
For Example(call either of them will cause an endless loop):
- (void)func1 {
// Code
[self func2];
}
- (void)func2 {
// Code
[self func1];
}
The Simplest solution is implement you code as below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];
if (indexPath.row == 0 && numberOfRows != 1) {
cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger numberOfRows = [self tableView:self numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];
if (indexPath.row == 0 && numberOfRows != 1) {
return 40;
}else {
return 5;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSString *key = [NSString stringWithFormat:#"%d", (int)section];
NSString *key = [NSString stringWithFormat:#"%ld", section]; // this line shows the error.
BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];
if (section == 0) {
return folded?_arr.count:1;
} else if (section == 1) {
return folded?_arr.count:1;
} else if (section == 2) {
return folded?_arr.count:1;
} else {
return folded?_arr.count:0;
}
}

How to move row to another Section while clicking on row?

I have Implemented multi select row but what i have to do is when we click on row it will move on another section , Here's my code what i have tried is ?
can anyone help me out from this ? Thanks in advance ...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return self.firstSectionDataSource.count;
else if (section == 1)
return self.dataArray.count;
else
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, Have to move row to another section while clcking ..
// other multiselect code
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure a cell to show the corresponding string from the array.
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
#try {
NSMutableArray *sourceArray = nil;
if (sourceIndexPath.section == 0) {
sourceArray = self.firstSectionDataSource;
}
else if (sourceIndexPath.section == 1)
{
sourceArray = self.dataArray;
}
NSMutableArray *destinationArray = nil;
if (destinationIndexPath.section == 0) {
destinationArray = self.firstSectionDataSource;
}
else if (destinationIndexPath.section == 1)
{
destinationArray = self.dataArray;
}
NSString *stringToMov = [sourceArray objectAtIndex:sourceIndexPath.row];
[sourceArray removeObject:stringToMov];
[destinationArray insertObject:stringToMov atIndex:destinationIndexPath.row];
}
#catch (NSException *exception) {
NSLog(#"exception = %# \n reason = %#",exception,exception.reason);
}
}
In cellForRowAtIndexPath
[cell setShowsReorderControl:YES];
And you need to implement the following UITableView delegate and data source methods properly
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return firstSectionDataSource.count;
else if (section == 1)
return secondSectionDataSource.count;
else
return 0;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
#try {
NSMutableArray *sourceArray = nil;
if (sourceIndexPath.section == 0) {
sourceArray = firstSectionDataSource;
}
else if (sourceIndexPath.section == 1)
{
sourceArray = secondSectionDataSource;
}
NSMutableArray *destinationArray = nil;
if (destinationIndexPath.section == 0) {
destinationArray = firstSectionDataSource;
}
else if (destinationIndexPath.section == 1)
{
destinationArray = secondSectionDataSource;
}
NSString *stringToMov = [[sourceArray objectAtIndex:sourceIndexPath.row] retain];
[sourceArray removeObject:stringToMov];
[destinationArray insertObject:stringToMov atIndex:destinationIndexPath.row];
[stringToMov release];
}
#catch (NSException *exception) {
NSLog(#"exception = %# \n reason = %#",exception,exception.reason);
}
}
You can do the next:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
// other multiselect code
}
Try this -
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:#"a",#"b",#"c", nil],[NSArray arrayWithObjects:#"d",#"e",#"f", nil], nil] forKeys:[NSArray arrayWithObjects:#"firstSection",#"secondsection", nil]];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[dict allKeys] count]; //assuming you have only two sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[dict objectForKey:[dict allKeys][section]] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
NSString *str = arr[indexPath.row];
if (indexPath.row!=0) {
[arr removeObject:str];
}
NSMutableArray *arr1 = [dict objectForKey:[dict allKeys][0]];
[arr1 addObject:str];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
NSString *str = arr[indexPath.row];
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
cell.textLabel.text = str;
return cell;
}

Retain selected checkbox in uitableview from searchbar filter

I have used UITableViewCellEditingStyle checkbox in my tableview for multiple selection. I use the uisearchbar to filter and then mark the cells..but when searchbar is closed and back to the main unfiltered table.. the previous selections disappear.
- (void)viewDidLoad {
[super viewDidLoad];
[tableview setEditing:YES animated:YES];
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(isfiltered==YES){
return [filteredarr count];
}
else{
if(arr==nil)return 0;
return [arr count] ;}
}
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID=#"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if(isfiltered==YES){
cell.textLabel.text=[filteredarr objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
}
cell.tintColor=[UIColor grayColor];
return cell;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchText.length==0){
isfiltered=NO;
}
else{
isfiltered=YES;
filteredarr=[NSMutableArray array];
for(NSString *str in arr){
NSRange textrange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(textrange.location != NSNotFound){
[filteredarr addObject:str];
}
}
}
[tableview reloadData];
}
Is there anyway to keep the selections?

UISearchDisplayController display custom UITableViewCell when No Results

I have one UITableView consisting of some custom UITableViewCells and one UISearchDisplayController connected to it. How can I show another custom Table View Cell if there are no results when you search for something?
Here's how the search and table view functions look like:
- (void)searchTableList:(UISearchBar*)searchBar {
NSString *searchString = searchBar.text;
for (NSArray *section in _celebs)
for (NSDictionary *row in section) {
if ([[row[#"name"] lowercaseString] rangeOfString:[searchString lowercaseString]].location!=NSNotFound)
[filteredContentList addObject:row];
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[filteredContentList removeAllObjects];
if (searchText.length) {
isSearching = YES;
[self searchTableList:searchBar];
} else isSearching = NO;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
isSearching = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self searchTableList:searchBar];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?1:_objects.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?filteredContentList.count:[_objects[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?0:25;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
return [self tableView:tableView heightForHeaderInSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 55;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}
- (NSDictionary *)getObjectFromIndexPath:(NSIndexPath*)indexPath tableView:(UITableView*)tableView {
BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
if (search)
return indexPath.row<filteredContentList.count?filteredContentList[indexPath.row]:nil;
else return indexPath.section<_objects.count?
(indexPath.row<[_objects[indexPath.section] count]?_objects[indexPath.section][indexPath.row]:nil)
:nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
FirstTableViewCell *cell;
NoResultsTableViewCell *cell1;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = (FirstTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:#"FirstTableViewCell"];
} else {
cell = (FirstTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:#"FirstTableViewCell" forIndexPath:indexPath];
}
NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
if (!object) return cell;
cell.object = celeb;
cell.objectName.text = [object objectForKey:#"name"];
}
}
return cell;
}
Make some changes like below:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
if (filteredContentList.count > 0){
return filteredContentList.count;
}else{
return 1; // the number of custom cells when there're no results
}
}else{
return [_objects[section] count];
}
}
- (NSDictionary *)getObjectFromIndexPath:(NSIndexPath*)indexPath tableView:(UITableView*)tableView {
BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
if (search)
return indexPath.row < filteredContentList.count? filteredContentList[indexPath.row]: theCustomCellDict; // use theCustomCellDict instead of nil
else return indexPath.section < _objects.count?
(indexPath.row < [_objects[indexPath.section] count]? _objects[indexPath.section][indexPath.row]: nil)
:nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isSearch = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
FirstTableViewCell *cell;
NoResultsTableViewCell *noReultsCell;
if ((isSearch && filteredContentList.count > 0) || !isSearch){
cell = (FirstTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:#"FirstTableViewCell" forIndexPath:indexPath];
NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
if (!object) return cell;
cell.object = celeb;
cell.objectName.text = [object objectForKey:#"name"];
return cell;
}else{
noResultsCell = (NoResultsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:#"NoResultsTableViewCell" forIndexPath:indexPath];
NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
if (!object) return noResultsCell;
noResultsCell.object = celeb;
noResultsCell.objectName.text = [object objectForKey:#"name"];
return noResultsCell
}
}
If you want to just show information (for example there is no result found 'something' ) You don't need to use UITableViewCells. I will reccomend you to look DZNEmptyDataSet library.
GitHub link https://github.com/dzenbot/DZNEmptyDataSet

Resources