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];
}
}
}
}
}
`
Related
I am adding a filter function to my app, but every time a filter is applied a previously opened cell does not close when I change the filter, is there a way to implement this?
I am currently using:
[self.tableview reloadData];
to reload the table every time the filter is applied
To open and close:
- (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];
NSLog(#"Section:%d", rows);
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
NSLog(#"INDEX: %#", tmpIndexPath);
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
});
}
}
}
Before tableview reloadData clear the expandedSections array.
[expandedSections removeAllIndexes];
[self.tableView reloadData];
expandedSections Array maintains the status of the expand section details.Due to that its not collapse the view. So clear the array before reload the table view.
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];
Good day! My default tableview sectionHeaderHeight is 56. I want to update it when sectionOpened method gets called.This is the following code:
- (void) sectionOpened : (NSInteger) section
{
sectionHeight = #"YES";
getsection = section;
NSLog(#"Section Clicked: %ld",(long)getsection);
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
array.open = YES;
NSInteger count = [array.category.menulist count];
NSMutableArray *indexPathToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i<count;i++)
{
[indexPathToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenIndex = self.openSectionIndex;
if (previousOpenIndex != NSNotFound)
{
SectionInfo *sectionArray = [self.sectionInfoArray objectAtIndex:previousOpenIndex];
sectionArray.open = NO;
NSInteger counts = [sectionArray.category.menulist count];
[sectionArray.sectionView toggleButtonPressed:FALSE];
for (NSInteger i = 0; i<counts; i++)
{
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenIndex]];
}
}
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (previousOpenIndex == NSNotFound || section < previousOpenIndex)
{
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
else
{
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
[menulistTable beginUpdates];
if(section==3 || section==4 || section==5) // this section has cells like submenu of a menu
{
menulistTable.sectionHeaderHeight = 20.0;
[menulistTable reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
//[menulistTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}
[menulistTable insertRowsAtIndexPaths:indexPathToInsert withRowAnimation:insertAnimation];
[menulistTable deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[menulistTable endUpdates];
self.openSectionIndex = section;
}
But nothing happening. I also used indexSetWithIndex:0. Whats wrong in the code?
Try like this to change Height:
[menulistTable beginUpdates];
menulistTable.sectionHeaderHeight = 20.0;
[menulistTable endUpdates];
Edited:
Take variable to set SectionHeaderHeight. Then place this code:
[menulistTable beginUpdates];
secHeaderHeight = 20.0;
[menulistTable endUpdates];
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return secHeaderHeight;
}
Hope it will work for you.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
when i click first section getting follwing error:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1076
2014-02-26 14:12:38.935 ExpandableTableCells[1865:a0b]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 8 from section 0 which only contains 1 rows before the update'
give me solution...
thanks...
code:
#pragma mark - Expanding
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
if (section>=0) return YES;
return NO;
//return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"------->%d",section);
if (section==0)
{
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(#"%d",[self.alpha count]);
return [self.alpha count];
}
return 1;
}
if (section==1) {
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(#"vvv%d",[self.numbers count]);
return [self.numbers count];
}
return 1;
}
if (section==2)
{
if ([expandedSections containsIndex:section])
{
// return 5; // return rows when expanded
NSLog(#"%d",[self.colors count]);
return [self.colors count];
}
return 1;
}
return YES;
}
- (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];
}
// Configure the cell...
if (indexPath.section==0) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = #"Alaphabet"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.alpha objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
if (indexPath.section==1) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = #"numbers"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
if (indexPath.section==2) {
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// first row
cell.textLabel.text = #"colors"; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
[self.tableView beginUpdates];
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
NSLog(#"section------%d",section);
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSLog(#"current expand------%d",currentlyExpanded);
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];
NSLog(#"rows------%d",rows);
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
NSLog(#"temp array----->%#",tmpArray);
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
NSLog(#"open index------%d",openSectionIndex);
if (openSectionIndex != -1)
{
[tmpArray removeAllObjects];
rows = [self tableView:tableView numberOfRowsInSection:openSectionIndex];
[expandedSections removeIndex:openSectionIndex];
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:openSectionIndex];
[tmpArray addObject:tmpIndexPath];
}
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
openSectionIndex=section;
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
[self.tableView endUpdates];
}
}
}
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
int selectedIndex;
UITableView *menuTableView;
NSMutableArray *colorsArray, *numberArray, *alphaArray ;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
selectedIndex=50;
colorsArray =[[NSMutableArray alloc]initWithObjects:#"red",#"yellow",#"pink",#"none", nil];
numberArray =[[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
alphaArray =[[NSMutableArray alloc]initWithObjects:#"a",#"b",#"c",#"d",#"e", nil];
menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStyleGrouped];
menuTableView.delegate=self;
menuTableView.dataSource=self;
menuTableView.separatorColor=[UIColor grayColor];
[self.view addSubview:menuTableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count=0;
if (section ==selectedIndex) {
switch (section) {
case 0:
count= colorsArray.count;
break;
case 1:
count= numberArray.count;
break;
case 2:
count= alphaArray.count;
break;
default:
break;
}
}
else
{
count= 1;
}
return count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text =colorsArray[indexPath.row];
break;
case 1:
cell.textLabel.text =numberArray[indexPath.row];
break;
case 2:
cell.textLabel.text =alphaArray[indexPath.row];
break;
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *titleButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
titleButton.frame = CGRectMake(0, 0, 320, 30);
[titleButton setBackgroundColor:[UIColor redColor]];
[titleButton addTarget:self action:#selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
titleButton.tag = section;
NSString *sectionName;
switch (section)
{
case 0: sectionName = NSLocalizedString(#"Colors", #"Colors");
break;
case 1: sectionName = NSLocalizedString(#"Numbers", #"Numbers");
break;
case 2: sectionName = NSLocalizedString(#"Alabaphet", #"Alabaphet");
break;
default: sectionName = #"";
break;
}
[titleButton setTitle:sectionName forState:UIControlStateNormal];
return titleButton;
}
-(void)handleTouchUpInside:(UIButton *)sender
{
if(selectedIndex== sender.tag)
{
selectedIndex=50;
}
else
{
selectedIndex= sender.tag;
}
[menuTableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// secondviewcontroller *sec =[[secondviewcontroller alloc]init];
switch (indexPath.section) {
case 0:
NSLog(#"%#",colorsArray[indexPath.row]);
//sec.arrayobjectInsecondClass =colorsArray[indexPath.row];
break;
case 1:
NSLog(#"%#",numberArray[indexPath.row]);
//sec.arrayobjectInsecondClass =numberArray[indexPath.row];
break;
case 2:
NSLog(#"%#",alphaArray[indexPath.row]);
//sec.arrayobjectInsecondClass =alphaArray[indexPath.row];
break;
default:
break;
}
// [self.navigationController pushViewController:sec animated:NO];
}
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"