How to code PrepareForSeque in UITableView that has sections and searching - ios

I have constructed a nice TableView that contains sections, searching, and the alphabetical strip on the right.
I tested it and all seemed well until I noticed that anything after the "A"s was not working correctly. I discovered the reason - in my PrepareForSegue I was passing the object I found at the row, but not paying any attention to the sections.
I searched stack overflow and found some things like I am trying to do, but it seems they are coding in the didSelectRowAtIndexPath, no prepare for segue. And I was unable to get any of the code to work.
My first question is what is the relationship between the two methods? I am using Storyboards so I think I should stick with the segues. But then how do I find out which employee object to pass?
Any help would be greatly appreciated.
Bryan
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
employee *dtlEmployee = [_employees objectAtIndex:indexPath.row];
self.detailViewController.detailItem = dtlEmployee;
}
else
{
// employee *dtlEmployee;
//
// int row = indexPath.row;
// int section = indexPath.section;
//
// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
//
// [self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
//
// dtlEmployee = [_employees objectAtIndex:newIndexPath];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
employee *dtlEmployee;
// int row = [[self.tableView] indexPath.row];
// int section = [self.tableView indexPath.section];
// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
// dtlEmployee = [_employees objectAtIndex:newIndexPath];
// NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
// NSUInteger selectedRow = selectedRowIndexPath.row;
// NSUInteger selectedSection = selectedRowIndexPath.section;
// NSArray *tmpArray = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow]];
// dtlEmployee = [tmpArray objectAtIndex:selectedRowIndexPath.row];
// dtlEmployee = [tmpArray objectAtIndex:indexPath.row];
//
// dtlEmployee = [_employees objectAtIndex:selectedRowIndexPath.]
//
if (isSearching)
{
dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
}
else
{
dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
[[segue destinationViewController] setDetailItem:dtlEmployee];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self.tableView dequeueReusableCellWithIdentifier: CellIdentifier];
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (isSearching)
{employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;}
else
{employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;}
return cell;
}

Assuming that your segue is connected to the table view cell, your sender in prepareForSegue: is actually a cell. (Use logging or the debugger to be sure.)
That means you can ask your table view for an index path that matches the cell and use the same logic that you would have in tableView:cellForRowAtIndexPath: for obtaining an object from a path.

Your logic for getting the right employee in tableView:cellForRowAtIndexPath: looks like this:
if (isSearching)
{
employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;
}
else
{
employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fullName;
}
But your logic in prepareForSegue:sender: looks like this:
if (isSearching)
{
dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
}
else
{
dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
You need to make the logic when performing the segue match the one when populating the cell, which from the sound of it works correctly. It appears that you're doing it correctly when isSearching is true, so when it's not true, the following line should be used:
employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex: [self.tableView indexPathForSelectedRow].section]] objectAtIndex: [self.tableView indexPathForSelectedRow].row];

Related

Deselect a row of tableview search results

I have a table view which list countries and I can select multiple raws. I've the option of select and deselect raws. The problem comes when I add a Search bar, I can`t deselect a raw of the results list because the method "didDeselectRowAtIndexPath" is never called when I press a results item.
I searched about this problem but nothing,
Thanks in advance,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:indexPath.row];
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSDictionary *country = [self.searchResult objectAtIndex:indexPath.row];
[self.selectedItems addObject: country];
}
else
{
//[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:indexPath.row];
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSDictionary *country = [self.countriesArray objectAtIndex:indexPath.row];
[self.selectedItems addObject: country];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = YES;
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
NSDictionary *country = [self.searchResult objectAtIndex:indexPath.row];
[self.selectedItems removeObject:country];
}else
{
//[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryView.hidden = YES;
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
NSDictionary *country = [self.countriesArray objectAtIndex:indexPath.row];
[self.selectedItems removeObject:country];
}
}

UISearchbarController is returning a wrong indexpath?

Im working on a tableview with a searchBardisplayController but when I type a search this shows the correct results but when I select the cell shows me a different selection (such as if I select a cell when the tableview is normal, the normal indexpath)
So heres my code:
number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSLog(#"%i", [[InfoWebDoctores sharedInstance]numberOfInfo]);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [displayObject count];
} else {
return [allObject count];
}
}
Configure the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"docCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"docCell"];
}
return cell;
}
Make the search:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if([searchString length] == 0)
{
[displayObject removeAllObjects];
[displayObject addObjectsFromArray:allObject];
}
else
{
[displayObject removeAllObjects];
for(NSDictionary *tmpDict in allObject)
{
NSString *val = [tmpDict objectForKey:doctorname];
NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];
NSString *val2 = [tmpDict objectForKey:doctoresp];
NSRange r2 = [val2 rangeOfString:searchString options:NSCaseInsensitiveSearch];
if(r2.location != NSNotFound || r.location != NSNotFound)
{
[displayObject addObject:tmpDict];
}
}
}
return YES;
}
And finally sending the data to the detailViewcontroller (Ive tryied with push connection but when I select a searchresult Cell not even get the push view...)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailTableViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detailController"];
NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
//NSIndexPath *index = [self.tableView indexPathForSelectedRow];
int row = [index row];
EntryJsonDoctores *entry = [[InfoWebDoctores sharedInstance]entryAtIndex:row];
detail.modal = #[entry.nombre,entry.especialidad, entry.especialidad2, entry.especialidad3, entry.cel, entry.mail, entry.tel1, entry.tel2, entry.ext,entry.hospital,entry.direccion];
[self presentViewController:detail animated:YES completion:^{
}];
}
So Ive tryied with the indexpathforselectedrow from the tableview and the indexpath from searchresultscontroller, whats wrong? I need someones help please
If you set the search display controller's resultsTableView's delegate to this View Controller, then the results TableView will be looking for the didSelectRowAtIndexPath.
You can do this in the viewDidLoad and put self.searchDisplayController.searchResultsTableView.delegate = self
Replace NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; with NSIndexPath *index = indexPath;

Search Controller issue

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath**
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];**
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
cell.DescriptionLabel.text = _Description[row];
cell.FeeSchedule.text = _Fschedule[row];
cell.NonFS.text = _Nonfeeschedule[row];
cell.ThumbImage.image = [UIImage imageNamed:_Images[row]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [medicalCodes objectAtIndex:indexPath.row];
return cell;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [medicalCodes objectAtIndex:indexPath.row];
}
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = #[_Title[row],_Description[row],_Fschedule[row],_Nonfeeschedule[row],_Images[row]];
}
}
`enter code here`- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
searchResults = [medicalCodes filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
I have a problem, I am trying to get my search bar to work, however it is stoping at this point right here essentially causing the app to crash. I am open to any suggestions as to any ways to add a search bar better. Any suggestions on what I can do? Any help is appreciated.
if you are update your data in main thread
or
if you are reload your tableview update with UI Main thread
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.tableView reloadData]
});
Looking through your code here, you are configuring cell even before allocating it.
And the codes you've written after following line will go untouched since you are returning the call.
cell.textLabel.text = [medicalCodes objectAtIndex:indexPath.row];
return cell;
I'm not sure what you are trying to achieve in -tableView:cellForRowAtIndexPath:. But I would suggest you try this,
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
//you can also allocate your custom cell here and then configure it as your need.
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
cell.DescriptionLabel.text = _Description[row];
cell.FeeSchedule.text = _Fschedule[row];
cell.NonFS.text = _Nonfeeschedule[row];
cell.ThumbImage.image = [UIImage imageNamed:_Images[row]];
cell.textLabel.text = medicalCodes[indexPath.row];
return cell;
}

How to design custom cell row wise individually in ios?

Could any one help me? Am working on expanding cell for the past one week Finally i can able to add sub menu in it. I designed two custom cells and using plist i added menu and sub menu to that. It is working well i added menu and sub menu. Now my problem is i want to add image and button to row 1,2,4,6 only using indexpath.row i assigned but this code assigning image and button to all rows But i only want to add to row 1,2,4,6 only ho i can do this pls some one help me???
interface MyHomeView ()
{
NSMutableArray *LeftPaneList;
NSArray *datalist;
}
#property (assign)BOOL isOpen;
#property (nonatomic,retain)NSIndexPath *selectIndex;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"LeftPaneMenuList" ofType:#"plist"];
LeftPaneList = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.isOpen=NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [LeftPaneList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isOpen) {
if (self.selectIndex.section == section) {
return [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count]+1;;
}
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"MyHomeViewCell2";
MyHomeViewCell2 *cell = (MyHomeViewCell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[LeftPaneList objectAtIndex:self.selectIndex.section] objectForKey:#"SubMenu"];
cell.name.text = [list objectAtIndex:indexPath.row-1];
return cell;
}
else
{
static NSString *CellIdentifier = #"MyHomeViewCell";
MyHomeViewCell *cell = (MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
cell.tag=indexPath.row;
NSString *name = [[LeftPaneList objectAtIndex:indexPath.section] objectForKey:#"MenuName"];
cell.MyHomeMenuLabel.text = name;
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
self.isOpen = firstDoInsert;
[self.MyHomeTableView beginUpdates];
int section = self.selectIndex.section;
int contentCount = [[[LeftPaneList objectAtIndex:section] objectForKey:#"SubMenu"] count];
NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
for (NSUInteger i = 1; i < contentCount + 1; i++) {
NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
[rowToInsert addObject:indexPathToInsert];
}
if (firstDoInsert)
{ [self.MyHomeTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[self.MyHomeTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
[self.MyHomeTableView endUpdates];
if (nextDoInsert) {
self.isOpen = YES;
self.selectIndex = [self.MyHomeTableView indexPathForSelectedRow];
[self didSelectCellRowFirstDo:YES nextDo:NO];
}
if (self.isOpen) [self.MyHomeTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 52;
}
This is the original o/p!
But i want o/p like this
some one help me??
you need to specify IndexPath.section First then you can check with IndexPath.row like Bellow Example:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(indexPath.section==0)
{
if(indexPath.row==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==4)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else if(indexPath.row==6)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
}
}
Th easy way to do this, is to set up 2 different dynamic prototype cells in the storyboard, each with its own identifier. In cellForRowAtIndexPath: dequeue the correct type of cell based on the indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row = 1 || indexPath.row = 2 || indexPath.row = 4 || indexPath.row = 6){
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithButton" forIndexPath:indexPath];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
As you are using dequeueReusableCellWithIdentifier: method, it will just give cell at index path 2, 4, 6 etc to some other cell in tableView. Instead use array to store your cell and then retrieve it from array. And then you can use indexpath.row

Invalid TableView Update

I have 1 empty section and other sections with full of objects.I have a button in all rows.when clicked on the button in a row the row should move to the empty section.But I am getting this error saying invalid tableview update.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.'
I tried the following Code.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
if(section == 0){
return favouritesArray.count;
}else{
NSString* key = [sectionTitleArray objectAtIndex:section - 1];
NSArray *aArray = [sectionContentDict valueForKey:key];
return aArray.count;
}
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"UCPMenuCellIdentifier";
UCPCategoryListCell *cell = (UCPCategoryListCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UCPCategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
}
if(indexPath.section == 0){
cell.textLabel.text = [favouritesArray objectAtIndex:indexPath.row ];
}
else{
NSString* key = [sectionContentDict.allKeys objectAtIndex:indexPath.section - 1];
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
if (!manyCells) {
cell.textLabel.text = #"click to enlarge";
}
else{
cell.textLabel.text = [[sectionContentDict objectForKey:key] objectAtIndex:indexPath.row];
}
}
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deletebtn.frame=CGRectMake(220, 10, 20, 20);
[deletebtn addTarget:self action:#selector(moveRowToFavorites:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
return cell;
}
-(void)moveRowToFavorites:(id)sender{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
[self.aTableView beginUpdates];
[favouritesArray addObject:cell.textLabel.text];
NSIndexPath *newindexPath = [NSIndexPath indexPathForRow:[favouritesArray count] inSection:0];
NSIndexPath *oldindexPath = [self.aTableView indexPathForCell:cell];
[self.aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:oldindexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newindexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.aTableView endUpdates];
}
Try moveRowToFavorites: method like this:
- (void) moveRowToFavorites: (id) sender
{
UIButton* button = (UIButton*) sender;
UITableViewCell* cell = (UITableViewCell*) button.superview.superview;
[self.aTableView beginUpdates];
[favouritesArray addObject: cell.textLabel.text];
NSIndexPath* newindexPath = [NSIndexPath indexPathForRow: [favouritesArray count] - 1
inSection: 0];
[self.aTableView insertRowsAtIndexPaths: [NSArray arrayWithObject: newindexPath]
withRowAnimation: UITableViewRowAnimationRight];
NSIndexPath* oldindexPath = [self.aTableView indexPathForCell: cell];
[self.aTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: oldindexPath]
withRowAnimation: UITableViewRowAnimationFade];
NSInteger oldSection = oldindexPath.section;
NSInteger oldRow = oldindexPath.row;
NSString* key = [sectionTitleArray objectAtIndex: oldSection - 1];
NSMutableArray* anArray = [NSMutableArray arrayWithArray: [sectionContentDict valueForKey: key]];
[anArray removeObjectAtIndex: oldRow];
[sectionContentDict setValue: anArray forKey: key];
[self.aTableView endUpdates];
}
I created project with this code. It is working fine. If you need I can give.
Hope my code helps you.
In moveRowToFavorites method change arrays(favouritesArray and sectionContentDict). You have added in favouritesArray. Delete same object from other array. Then call table reloadData.
Try this. It will add to favorite section.
-(void)moveRowToFavorites:(id)sender{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
[favouritesArray addObject:cell.textLabel.text];
//here you need to delete same object from [sectionContentDict objectForKey:key]
[self.aTableView reloadData];
}
Dont do anything like insertRowsAtIndexPaths or deleteRowsAtIndexPaths
In -(void)moveRowToFavorites:(id)sender after just adding object to favouritesArray, you have to remove objet from array within the sectionContentDict. And you dont have to use [table reloadData] after begin-end Updates.

Resources