Multi-select cell in UITableView in sequence - ios

I have a UITableView with numerous cells. I'm able to multi select cells but what I want is that user only can select multiple cells in sequence. ie if user has selected 2nd row then he/she only can select 3rd cell then 4th in sequence, doesn't allow to select random cells.
Here is what I've done:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrPackages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PurchaseListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PurchaseListTableViewCell" forIndexPath:indexPath];
NSDictionary *dict = [arrPackages objectAtIndex:indexPath.row];
cell.lblPackageName.text = [dict valueForKey:#"package_name"];
cell.lblDiscount.text = [NSString stringWithFormat:#"You save: %#%%",[dict valueForKey:#"discount"]];
cell.lblLastAmount.text = [NSString stringWithFormat:#"%#",[dict valueForKey:#"package_price"]];
NSString *strPaymentStatus = [NSString stringWithFormat:#"%#",[dict valueForKey:#"payment_status"]];
if ([strPaymentStatus isEqualToString:#"2"]) {
cell.contentView.userInteractionEnabled = NO;
}
else{
cell.contentView.userInteractionEnabled = YES;
}
/*
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:#2
range:NSMakeRange(0, [attributeString length])];
*/
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSDictionary *dict = [_arrModuleData objectAtIndex:indexPath.row];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

You can implement the tableview delegate method tableView(_:willSelectRowAt:) and return nil if you don't want to select the current index path, else you just return the given index path.
Objective-C:
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray<NSIndexPath *> *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if (selectedIndexPaths == nil || selectedIndexPaths.count == 0) {
return indexPath;
}
NSArray<NSIndexPath *> *sortedIndexPaths = [[selectedIndexPaths sortedArrayUsingComparator:^NSComparisonResult(NSIndexPath * _Nonnull index1, NSIndexPath * _Nonnull index2) {
return index1.row < index2.row;
}] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat: #"section = %d", indexPath.section]];
if (indexPath.row >= sortedIndexPaths[0].row &&
indexPath.row <= sortedIndexPaths[sortedIndexPaths.count-1].row) {
return indexPath;
}
return nil;
}
Swift:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard let selectedIndexPaths = tableView.indexPathsForSelectedRows,
selectedIndexPaths.count > 0 else {
return indexPath
}
let indexes = selectedIndexPaths
.filter { $0.section == indexPath.section }
.sorted()
if let first = indexes.first,
let last = indexes.last,
indexPath.row >= first.row - 1 && indexPath.row <= last.row + 1 {
return indexPath
}
return nil
}
Of course you should code a similar behavior for tableView(_:willDeselectRowAt:)

Related

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

Parse json for sectioned UITableView in iOS

I am trying to display State (in section title) and City (in table rows) from this url. Top Cities will come in first section followed by state and cities. Issue is I am not able to assign respective array of cities for sectioned state title row. Here's what I tried..
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"Json dictionary = %#", json);
self.dictListSateMaster = [json objectForKey:#"RaffleInstanceLocations"];
for (NSDictionary *dict in [self.dictListSateMaster objectForKey:#"listStateMaster"])
{
ViewLocationsObjct *viewLocationsObjct = [[ViewLocationsObjct alloc] init];
viewLocationsObjct.StateNameStr = [dict objectForKey:#"StateName"];
[self.arrayListStateMaster addObject:[dict objectForKey:#"StateName"]];
for (NSDictionary *dictnry in [dict objectForKey:#"listCityMaster"])
{
//*********-- Need corrections here
ViewLocationStateMaster *viewLocNewObjct = [[ViewLocationStateMaster alloc] init];
viewLocNewObjct.CityName = [dictnry objectForKey:#"CityName"];
[self.arrayListCityMaster addObject:viewLocNewObjct];
}
}
for (NSDictionary *dict in [self.dictListSateMaster objectForKey:#"listTopCities"])
{
ViewLocationsObjct *viewLocationsObjct = [[ViewLocationsObjct alloc] init];
viewLocationsObjct.CityName = [dict objectForKey:#"CityName"];
[self.arrayTopCities addObject:viewLocationsObjct];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.arrayListStateMaster.count+1; //+1 to append to top cities section at the top
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
ViewLocationsTblCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (indexPath.section == 0)
{
ViewLocationsObjct *viewLocationObj = [self.arrayTopCities objectAtIndex:indexPath.row];
cell.locationLabl.text = viewLocationObj.CityName;
}
else
{
//*********-- Need corrections here
//NSString *sectionTitle = [self.arrayListStateMaster objectAtIndex:indexPath.section];
//NSMutableArray *arrayCities = [self.dictListSateMaster objectForKey:sectionTitle];
//cell.locationLabl.text = [arrayCities objectAtIndex:indexPath.row];
ViewLocationStateMaster *viewLoc = [self.arrayListCityMaster objectAtIndex:indexPath.row];
cell.locationLabl.text = viewLoc.CityName;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)//return top cities for 1st section
{
return self.arrayTopCities.count;
}
else
{
return self.arrayListCityMaster.count;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return #"Top Cities";
}
else
{
if (self.arrayListStateMaster.count > 0)
{
return [self.arrayListStateMaster objectAtIndex:section-1];//-1 to remove index of top cities section at the top
}
else
{
return #"";
}
}
}
OK so here's how my problem was solved !
NSDictionary *dictRaffleInstLocations = [json objectForKey:#"RaffleInstanceLocations"];
sectionArray = [dictRaffleInstLocations objectForKey:#"listStateMaster"];
topCityArray = [dictRaffleInstLocations objectForKeyedSubscript:#"listTopCities"];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return topCityArray.count;
}else
return [[[sectionArray objectAtIndex:section-1]objectForKey:#"listCityMaster"] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionArray.count+1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return #"Top Cities";
}else
return [[sectionArray objectAtIndex:section-1]objectForKey:#"StateName"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"MyCellLocation";
ViewLocationsTblCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *titleString = #"";
if (indexPath.section == 0) {
titleString = [[topCityArray objectAtIndex:indexPath.row]objectForKey:#"CityName"];
}else{
NSArray *cityArray = [sectionArray[indexPath.section-1]objectForKey:#"listCityMaster"];
titleString = [cityArray[indexPath.row]objectForKey:#"CityName"];
}
cell.locationLabl.text = titleString;
return cell;
}

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

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

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

Resources