Error within tableview when cells are switching sections - ios

So in my table view, I have an array called dataArray and it's getting its objects from a server using JSON. I added a button to each cell and when the button is clicked the cell is supposed to move to another section/ or another array called followedArray to be more exact. Now the cells are transferring to the other section but after moving 6 cells I get an error that saids this. All arrays are NSMutableArray. Also, I'm new to iOS so try to work with me, thank you.
2016-10-26 17:27:19.569 CustomCellApp[3212:198103] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 4]'
---------------------
Side note, I can make a separate error post but I have another issue, the images displaying in the cells are being changed to other images when switching sections, using the pod SDWebImage.
---------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!isFiltered) {
if (section == 0) {
return [followedArray count];
}
else if (section == 1) {
return [dataArray count];
}
}
return [filteredArray count];
}
---------------------
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Followed Data";
}
else {
return #"All Data";
}
}
---------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configuring the cell
Data * dataObject;
if (!isFiltered) {
if (indexPath.section == 0) {
dataObject = [followedArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
dataObject = [dataArray objectAtIndex:indexPath.row];
}
}
else {
dataObject = [filteredArray objectAtIndex:indexPath.row];
}
// Loading Images
if (!isFiltered) {
// Exception Breakpoint Here
NSURL * imgURL = [[dataArray objectAtIndex:indexPath.row] valueForKey:#"dataURL"];
[cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
[cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:#"no-image.png"] options:SDWebImageRefreshCached];
}else{
NSURL * imgURL = [[filteredArray objectAtIndex:indexPath.row] valueForKey:#"dataURL"];
[cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
[cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:#"no-image.png"] options:SDWebImageRefreshCached];
}
// Loading Follow Button
cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:#selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;
return cell;
}
---------------------
-(void)followButtonClick:(UIButton *)sender {
// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
// Creating an action per tag
if (indexPath != nil)
{
// Change Follow to Following
[sender setImage:[UIImage imageNamed:#"follow.png"] forState:UIControlStateNormal];
cell.followButton.hidden = YES;
cell.followedButton.hidden = NO;
// ----- ERROR BEGINS HERE ----- //
[self.myTableView beginUpdates];
// ----- Inserting Cell to Section 0 ----- *CAUSING PROBLEMS*
// Exception Breakpoint Here
[followedArray addObject:[dataArray objectAtIndex:indexPath.row]];
[myTableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:followedArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"indexPath.row = %ld", (long)indexPath.row);
// ----- Removing Cell from Section 1 ----- *WORKING*
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
NSLog(#"Array =%#",followedArray);
[self.myTableView endUpdates];
// ----- ERROR ENDS HERE ----- //
}
}
---------------------

This is what helped me fix the errors I was having. Credits to #AliOmari for helping me out.
In cellForRowAtIndexPath
// Configuring the cell
Data * dataObject;
if (!isFiltered) {
if (indexPath.section == 0) {
dataObject = [followedArray objectAtIndex:indexPath.row];
[cell populateCell:dataObject isFollowed:YES indexPath:indexPath parentView:self];
}
else if (indexPath.section == 1) {
dataObject = [dataArray objectAtIndex:indexPath.row];
[cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self];
}
}
else {
dataObject = [filteredArray objectAtIndex:indexPath.row];
[cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self];
}
return cell;
Inside my Button
[self.myTableView beginUpdates];
// ----- Inserting Cell to Section 0 -----
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
[myTableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
//NSLog(#"indexPath.row = %ld", (long)indexPath.row);
// ----- Removing Cell from Section 1 -----
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
//NSLog(#"Array =%#",followedArray);
[self.myTableView endUpdates];

Related

UILabel under a custom tableview cell is not getting updated

My tableview cell is not getting updated on viewDidAppear :
-(void)viewDidAppear:(BOOL)animated
{
[[ApiAccess getSharedInstance] setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket]setDelegate:self];
[[[SocektAccess getSharedInstance]getSocket] reconnect];
_chatSocket =[[SocektAccess getSharedInstance]getSocket];
if(self.updateWill)
{
NSLog(#"viewDidAppear");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.updateId inSection:0];
NSLog(#"Update Value: %d",self.updateValue);
NSLog(#"Update ID: %d",self.updateId);
TimelineTableViewCell *cell = (TimelineTableViewCell *)[self.tableData cellForRowAtIndexPath:indexPath];
WallPost *data = self.myObject[indexPath.row];
data.commentCount = self.updateValue;
[self.myObject replaceObjectAtIndex:indexPath.row withObject:data];
WallPost *data2 = self.myObject[indexPath.row];
NSLog(#": %d",data2.commentCount);
cell.commentLabel.text = #" ";
[self.tableData beginUpdates];
[self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableData reloadData];
[self.tableData endUpdates];
}
self.updateWill = NO;
}
The self.updateWill is a boolean and has been triggered from another class :
if(self.responseAdd.responseStat.status)
{
self.commentTxt.text=#"";
[self getData];
TimelineViewController *t = (TimelineViewController *)self.navigationController.viewControllers[0];
t.updateWill = YES;
t.updateId = self.index;
t.updateValue =(int)self.response.responseData.count+1;
NSLog(#"response %d",(int)self.response.responseData.count);
}
as you can see i have created a object of my Main class which is TimelineViewController and has added the value of updateWill and other needed properties. But the cell is not getting reloaded!! whats i am doing wrong in here ??
update
(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimelineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
self.counter = 0;
WallPost *data = self.myObject[indexPath.row];
cell.image.userInteractionEnabled = YES;
cell.image.tag = indexPath.row;
if(self.updateWill == YES)
{
NSLog(#"YES YES");
data.commentCount= (int)self.updateValue;
[self.tableData beginUpdates];
[self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableData reloadData];
[self.tableData endUpdates];
cell.commentLabel.text = [NSString stringWithFormat:#"%d comments",data.commentCount];
}
else
{
data.commentCount = (int)data.comments.count;
cell.commentLabel.text = [NSString stringWithFormat:#"%d comments",data.commentCount];
}
This is what you need to workaround in objective c
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableData reloadData];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
NSArray *paths = [self.tableData indexPathsForVisibleRows];
for (NSIndexPath *path in paths)
{
//get desired cell here
CustomCell *cell = (CustomCell *)[(UITableView *)self.view cellForRowAtIndexPath: path
];
cell.labeView //now you can update here on your cell items
}
});
});
}
and this is my original logic in Swift
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
tableView.reloadData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//get visible cells indexpaths
if let indices = self.tableView.indexPathsForVisibleRows
{
for index in indices
{
if index.section == 0 //this is specific to me, checking if section 0 is visible
{
//get desired cell here
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0)) as! CustomCell
cell.myImageView.userInteractionEnabled = true //now i can update imageview on table cell
}
}
}
})
}
In viewDidLoad
[self.tableData reloadData];
I hope it will be useful.

Remove Rows From UITableView from Previous Selected Section

Currently I have a UITableView which is expands and collapses with the current code.
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section{
if (section>=0) return YES;
return NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger numberofRows = 0;
numberofRows = nameArray.count;
if (numberofRows != 0){
self.mainTableView.hidden = false;
}
return numberofRows;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [ALCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:ALCustomColoredAccessoryTypeUp];
}
}
else {
NSLog(#"Selected Section is %ld and subrow is %ld ",(long)indexPath.section ,(long)indexPath.row);
}
}
}
This works really well and upon selection of a UITableViews section the rows are expanded and populated with the correct data and when the same section is selected the rows are removed and appear collapsed.
However what i want to do is somehow automatically collapse the previous selected section and remove the rows within that previous section when the user selects a new indexpath.section.
I have tried storing the selected section index path to an array removing rows based on this value but I think I'm going about it the wrong way as I get assertion failures.
So my question is as follows :-
How can i automatically collapse (remove rows) from a uitableviews section upon selection of another section
Thanks for your help in advance
Thomas
i have faced this issue when i was writting code for my app and i founded this post that had no any single comment or answer...
You can use this code that i was written for me to colapse previous expanded rows of section when i clicked on other section and expand rows of that section, this code working for me try it...
`
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
if (section>=0) return YES;
return NO;
}
static NSInteger Value=0;
static NSInteger Value2=0;
static CFIndex indexPre=0;
static NSMutableArray *preArray;
static NSIndexPath *path;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.hideButton.hidden=NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger index=[expandedSections firstIndex];
if ([expandedSections indexGreaterThanIndex:index])
{
NSLog(#"%lu hhhwww", index);
}
NSInteger section = indexPath.section;
static NSInteger rows;
static NSInteger PreRows;
NSLog(#"%lu MY INDEXES1",(unsigned long)[expandedSections count]);
NSMutableArray *tmpArray = [NSMutableArray array];
preArray = [NSMutableArray array];
BOOL currentlyExpanded = [expandedSections containsIndex:section];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
Value=0;
Value2=0;
cell.hideButton.hidden=YES;
}
else
{
if (Value==0)
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
PreRows = [self tableView:tableView numberOfRowsInSection:section];
indexPre=indexPath.section;
Value=1;
}
else if (Value==1)
{
Value2=1;
NSLog(#"indexPre == %ld %# my indexxxxx", indexPre, expandedSections);
[expandedSections removeIndex:indexPre];
NSLog(#"indexPre == %ld %# my indexxxxx", indexPre, expandedSections);
[self.tableView reloadData];
[expandedSections addIndex:section];
NSLog(#"indexPre == %ld %# my indexxxxx", indexPre, expandedSections);
rows=[self tableView:tableView numberOfRowsInSection:section];
}
}
NSLog(#"%# MY INDEXES",expandedSections);
if (Value2==1)
{
for (int i=1; i<PreRows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:indexPre];
[preArray addObject:tmpIndexPath];
}
NSLog(#"my arrray %#", preArray);
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
NSLog(#"my arrray tmparray%#", preArray);
}
else
{
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
}
if (currentlyExpanded )
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:NO];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else if(Value==1)
{
if (Value2==1)
{
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:NO];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
[tableView endUpdates];
[tableView beginUpdates];
[tableView endUpdates];
indexPre=indexPath.section;
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
}
}
}
`

delete last row in TableView and show a "no results" label in new cell

I have a problem with my UITableView where deleting the last row in the section terminates the app with an NSInternalInconsistencyException:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
My UITableView is populated with MPMediaItems from an MPMediaItemCollection (self.detailCollection). When the last one gets deleted I want to show a "No results found" label in a blank cell.
Here is my cellForRowAtIndexPath:
- (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] autorelease];
}
// Configure the cell...
if ([[self.detailCollection items] count] == 0) {
[tableView numberOfRowsInSection:1];
cell.textLabel.text = #"No results found";
//return cell;
} else {
MPMediaItem *song = (MPMediaItem *)[[self.detailCollection items] objectAtIndex:[indexPath row]];
if (song) {
cell.textLabel.text = [song valueForProperty:MPMediaItemPropertyTitle];
MPMediaItemArtwork *art = [song valueForProperty:MPMediaItemPropertyArtwork];
cell.imageView.image = [art imageWithSize:CGSizeMake(64, 64)];
}
}
return cell;
}
Here is my code for deleting the rows:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
PlaylistData *pData = [PlaylistData getInstance];
NSMutableArray *tempArray = [[self.eventDictionary valueForKey:[NSString stringWithFormat:#"%#", pData.selectedEvent]] mutableCopy];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
// Delete the row from the data source
[tempArray removeObjectAtIndex:indexPath.row];
[self.eventDictionary setValue:tempArray forKey:[NSString stringWithFormat:#"%#", pData.selectedEvent]];
[[NSUserDefaults standardUserDefaults] setValue:self.eventDictionary forKey:#"Playlist Items"];
[[NSUserDefaults standardUserDefaults] synchronize];
if ([tempArray count] == 0) {
[tableView numberOfRowsInSection:1];
}
for (int i=0; i<[tempArray count]; i++) {
NSString *pID = [NSString stringWithFormat:#"%#", [tempArray objectAtIndex:i]];
unsigned long long ullvalue = strtoull([pID UTF8String], NULL, 0);
NSNumber *UniqueID = [NSNumber numberWithUnsignedLongLong:ullvalue];
MPMediaQuery *cellQuery = [[MPMediaQuery alloc] init];
[cellQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:UniqueID forProperty:MPMediaItemPropertyPersistentID]];
for (MPMediaItem *item in [cellQuery items]) {
[newArray addObject:item];
}
[cellQuery release];
}
if (![newArray count] == 0) {
self.detailCollection = [[MPMediaItemCollection alloc] initWithItems:newArray];
[tableView numberOfRowsInSection:[self.detailCollection count]];
} else {
[tableView numberOfRowsInSection:1];
[tableView reloadData];
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
And here is my numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if ([[self.detailCollection items] count] == 0 || [self.detailCollection items] == nil || [self.detailCollection items] == NULL) {
return 1;
}
return [[self.detailCollection items] count];
}
My question is: Why isn't it creating the "No results found" cell when self.detailCollection is == 0?
I think you want something to the effect of:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
if ([newArray count] == 0) {
[tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
However, a simpler solution would be to just add a label to your table view. Unless there is some specific reason that you need an actual UITableViewCell to display "No results found".
UILabel *label = [[UILabel alloc] init];
CGRect frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
label.frame = frame;
label.text = #"No results found";
[self.tableView addSubview:label];
One solution that I would recommend is to use a table footer view rather than a new cell. Basically, add a footer to your table that is only visible when the cell count is 0.
You can override the method
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
to get a footer.
When deleting and adding objects, check the new count and then adjust the visibility of the footer from there.
You are calling numberOfRowsInSection in a couple of places. You should never be calling it, it's a call back hook that you implement and the system calls.
The cleanest solution to do this would be to set self.tableView.tableFooterView when rows = 0
#interface UITableView : UIScrollView <NSCoding> {
...
#property(nonatomic,retain) UIView *tableFooterView;
// accessory view below content. default is nil. not to be confused with section footer

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.

CoreData with relationship in the same UItableView

I need to load the "Main Entity" that has a relationship one-to-many with "Child Entity" into the same UITableView, how can i do?
Thanks in advance.
Update
i use the following code to populate the UITableView:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[fetchedResultsController sections] objectAtIndex:section] name];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
if (self.selectedIndexPath) {
return [sectionInfo numberOfObjects] + [self.childArray count];
}else {
return [sectionInfo numberOfObjects];
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];
OrarioMainCell *cell = nil;
if (indexPath == self.selectedIndexPath) {
NSUInteger giorniCount = [selectedObj.giorni count];
NSInteger row = indexPath.row;
if (indexPath.row < giorniCount) {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:11];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"fondoCella"]]autorelease];
cell.backgroundView=bgImage;
[df setDateFormat:#"HH:mm"];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];
} else {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"DetailCell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:13];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
GiornoRecupero *giorno = [childArray objectAtIndex:row];
cell.textLabel.text = [NSString stringWithFormat:#"%i", giorno.oreStraordinario];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%i", giorno.durata];
}
} else {
cell = (OrarioMainCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell=(OrarioMainCell *)[[[NSBundle mainBundle]loadNibNamed:#"myCustomCell" owner:self options:nil]objectAtIndex:11];
radiusLabel=[[LabelRadiusAndText alloc]init];
}
UIImageView *bgImage=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"fondoCella"]]autorelease];
cell.backgroundView=bgImage;
[df setDateFormat:#"HH:mm"];
cell.orariLbl.text = [NSString stringWithFormat:#"%# - %#",[df stringFromDate:selectedObj.oraInizio],[df stringFromDate:selectedObj.oraFine]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
OreRecupero *selectedObj=(OreRecupero*)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1
inSection:indexPath.section];
//if user tapped the same row twice let's start getting rid of the control cell
if (!self.selectedIndexPath) {
// Show Drop Down Cell
for (GiornoRecupero *giorno in selectedObj.giorni) {
[self.childArray addObject:giorno];
}
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
self.selectedIndexPath = indexPath;
} else {
NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1
inSection:self.selectedIndexPath.section];
if (indexPath.row == self.selectedIndexPath.row) {
// Hide Drop Down Cell
[self.childArray removeAllObjects];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
self.selectedIndexPath = nil;
} else if (indexPath.row == currentdropDownCellIndexPath.row) {
// Dropdown Cell Selected - No Action
return;
} else {
// Switch Dropdown Cell Location
[self.myTableView beginUpdates];
// Hide Dropdown Cell
[self.childArray removeAllObjects];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
// Show Dropdown Cell
NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1);
dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow
inSection:indexPath.section];
for (GiornoRecupero *giorno in selectedObj.giorni) {
[self.childArray addObject:giorno];
}
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1
inSection:dropDownCellIndexPath.section];
[self.myTableView endUpdates];
}
}
}
i would like to insert new rows using didSelectRowAtIndexPath method, getting data from "Child Entity"

Resources