UISearchDisplayController display custom UITableViewCell when No Results - ios

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

Related

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?

UIButton doesn't appear on iOS Today Widget

I am creating a today extension for my app, but there are some errors that I can't quite understand:
First:
Everything is Ok on the storyboard, but the button doesn't appear on the widget.
Second: When the tableview has more than one cell, the last cell gets cut.
Third: CellForRow is called, but don't change anything on the cell (Label is still "Label").
http://i.stack.imgur.com/Jp31V.png
Here's my Widget code:
#implementation TodayViewController{
NSMutableArray *listaFavoritos;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.widgetTableView.delegate = self;
self.widgetTableView.dataSource = self;
[self updateTableView];
self.preferredContentSize = self.widgetTableView.frame.size;
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(userDefaultsDidChange:)
name:NSUserDefaultsDidChangeNotification
object:nil];
}
return self;
}
- (void)userDefaultsDidChange:(NSNotification *)notification {
[self updateTableView];
}
- (void)updateTableView {
listaFavoritos = [[NSMutableArray alloc] init];
//listaFavoritos = [[self readArrayWithCustomObjFromUserDefaults:#"listaFavs"] mutableCopy];
[listaFavoritos addObject:#"test1"];
[listaFavoritos addObject:#"test2"];
NSLog(#"%#", listaFavoritos);
}
-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName
{
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.kazoowa.timers"];
NSData *data = [sharedDefaults objectForKey:keyName];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[sharedDefaults synchronize];
return myArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listaFavoritos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WidgetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"WidgetCell" forIndexPath:indexPath];
if (cell == nil)
{
cell.nomeTimer.text = [listaFavoritos objectAtIndex:indexPath.row];
}
NSLog(#"CellForRow");
return cell;
}
1)For your second problem, your cellIdentifier in tableView:cellForRowAtIndexPath will be "static" like this :
static NSString *cellName = #"WidgetCell";
WidgetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName forIndexPath:indexPath];
2)this condition will be never execute because it's never nil :
if (cell == nil)
{
cell.nomeTimer.text = [listaFavoritos objectAtIndex:indexPath.row];
}
replace it by this :
if (cell != nil)
{
cell.nomeTimer.text = [listaFavoritos objectAtIndex:indexPath.row];
}
3)Please use Pragma Mark for separate properly your code like below:
#pragma mark-UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listaFavoritos count];
}
#pragma mark-UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WidgetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"WidgetCell" forIndexPath:indexPath];
if (cell == nil)
{
cell.nomeTimer.text = [listaFavoritos objectAtIndex:indexPath.row];
}
NSLog(#"CellForRow");
return cell;
}
Hope it helps :)
Try it :
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellName = #"WidgetCell";
WidgetTableViewCell *cell = (WidgetTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellName];
if (cell == nil) {
cell = [[WidgetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.textLabel.text = [listaFavoritos objectAtIndex:indexPath.row];;
return cell;
}

moveRowAtIndexPath doesn't get called

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

UITableView cell not display correct detail

I created tableview and searchbar.
Name for cell 1 is iPhone.
When I click on the cell iPhone, on detail view it writes iPhone. When I search iPod it became the first cell in the search list but when I click this ,it writes iPhone instead of iPod. I try insert all code to here.
- (BOOL)shouldAutootateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table View Methods
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
return cell;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:allItems];
} else {
[displayItems removeAllObjects];
for (NSString * string in allItems ){
NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound){
[displayItems addObject:string];
}
}
[tableView reloadData];
}
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)asearchBar {
[searchBar resignFirstResponder];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryDisclosureIndicator;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *DVC = [[DetailViewController alloc] init];
//Pass the row to the DVC
DVC.characterNumber = [indexPath row];
//Push the DetailViewController object onto the stack
[self.navigationController pushViewController:DVC animated:YES];
}
#end
You are passing to detail controller row number for allItems array, but it is not same that in displayItems
DVC.characterNumber = [indexPath row];
There is you should assing correct row from allItems array, not displayItems array
Update:
NSString *item = [displayItems objectAtIndex:indexPath.row;
DVC.characterNumber = [allItems indexOfObject:item];

Resources