Delete selected Items in collection view - ios

I select 5 items in my collection view and i want to delete it. Please help me delete the selected items. I put one button and when i press that button all selected items are delete that i want. My button click code.
- (IBAction)btn_delete:(id)sender {
NSArray *selectedItemsIndexPaths = [self.MyCollectionView indexPathsForSelectedItems];
NSLog(#"Selected images: %#",mySelectedArray);
[self.MyCollectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
[self.MyCollectionView reloadData];
}

First remove Objects for DataSource, then try delete cells from Collectionview
-(void)deleteCellInCollectionViewAtIndex:(int)index{
if (self.collectionView) {
[self.collectionView performBatchUpdates:^{
NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:index inSection:0] ;
[self.collectionView deleteItemsAtIndexPaths:#[cellIndexPath]];
} completion:nil];
}
}

#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#pragma Mark For Collection View Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [patternImageArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
PatternViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSString *myPatternString = [patternImageArray objectAtIndex:indexPath.row];
cell.ImageView.image = [UIImage imageNamed:myPatternString];
cell.ImageLabel.text = myPatternString;
ind = indexPath.row;
return cell; }
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
PatternViewCell *mySelectedCell = (PatternViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
mySelectedCell.ImageLabel.backgroundColor = [UIColor blueColor];
mySelectedCell.ImageLabel.textColor = [UIColor whiteColor];
[mySelectedArray addObject:indexPath];
i = indexPath.row; }
- (void)viewDidLoad {
[super viewDidLoad];
patternImageArray = [[NSMutableArray alloc]initWithObjects:#"Thar.jpg",#"Thar1.jpg",#"Thar2.jpg",#"Thar3.jpg",#"Thar4.jpg",#"Thar5.jpg",#"Thar6.jpg",#"Thar7.jpg",#"Thar8.jpg", nil];
self.MyCollectionView.multipleTouchEnabled = YES;
mySelectedArray = [[NSMutableArray alloc]init]; }
- (IBAction)btn_delete:(id)sender
{
[self.MyCollectionView reloadData];
NSLog(#"Selected images: %#",mySelectedArray);
}
#end

In your btn_delete: method, add this code
NSArray *selectedItemsIndexPaths = [self.MyCollectionView indexPathsForSelectedItems];
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
for (NSInteger counter = 0; counter < [mySelectedArray count]; counter++) {
NSIndexPath *indexPath = mySelectedArray[counter];
[indexSet addIndex:indexPath.row];
}
[patternImageArray removeObjectsAtIndexes:indexSet];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:mySelectedArray];
} completion:^(BOOL finished) {
[mySelectedArray removeAllObjects];
}];
In your collectionView:didSelectItemAtIndexPath: method, you should first check if array already contains that indexpath before adding to array.
You can see it below:
if(![mySelectedArray containsObject:indexPath])
[mySelectedArray addObject:indexPath];

Related

Adding collectionview into uitableviewcell and reload data for particular cell of tableview

how to bind data from server into collectionview which is inside uitableviewcell using hide and unhide functionality.on tap of uibutton i want to make colectionview visible and hide it again on the tap of button.What happens is when i scroll the tableview the data inside the collectionview skips from one cell to another making the collection view blank.
-(void)arrowButtonTapped:(UIButton *)donwbtn{
myob = [NSString stringWithFormat:#"%li", (long)donwbtn.tag];
NSLog(#"%#",myob);
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0
inSection:donwbtn.tag];
OppCell* celll = [_tableView cellForRowAtIndexPath:indexPath];
celll.collView.tag=donwbtn.tag;
if ([checkcoll containsObject:myob]) {
[checkcoll removeObject:myob];
celll.collView.hidden = true;
celll.collvwhght.constant=0;
celll.colltop.constant=0;
celll.collbottom.constant=0;
}
else
{
[checkcoll addObject:myob];
celll.collView.hidden = false;
celll.collvwhght.constant=229;
celll.colltop.constant=9;
celll.collbottom.constant=8;
[celll.collView reloadData] ;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
OppCell *cell = (OppCell *)[tableView
dequeueReusableCellWithIdentifier:#"OppCell"];
NSString *theIndexpath = [NSString stringWithFormat:#"%ld",
(long)indexPath.section];
if ([checkcoll containsObject:theIndexpath])
{
cell.collView.hidden = false;
cell.collvwhght.constant=229;
cell.colltop.constant=9;
cell.collbottom.constant=8;
if((![[[_Activitylistarray
objectAtIndex:indexPath.section]
valueForKey:#"OpportunityContactInfoList"]
isKindOfClass:[NSNull class]])&&([[[_Activitylistarray
objectAtIndex:indexPath.section]
valueForKey:#"OpportunityContactInfoList"]count]>0)){
if(cell.contactdetailsarray == nil) {
cell.contactdetailsarray =[[NSMutableArray alloc]init];
for(NSDictionary * contactdict in
[[_Activitylistarray objectAtIndex:indexPath.section]
valueForKey:#"OpportunityContactInfoList"] ){
[cell.contactdetailsarray addObject:contactdict];
[cell.collView reloadData];
}
}
}
}
else
{
cell.collView.hidden = true;
cell.collvwhght.constant=0;
cell.colltop.constant=0;
cell.collbottom.constant=0;
// [cell.contactdetailsarray removeAllObjects];
// [cell.collView reloadData];
}
////Code in UITableViewCell
#import "OppCell.h"
#import "OppsContactCell.h"
#implementation OppCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self.collView registerNib:[UINib nibWithNibName:#"OppsContactCell"
bundle:nil] forCellWithReuseIdentifier:#"OppsContactCell"];
self.collView.pagingEnabled = YES;
UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout
alloc]init];
layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
self.collView.collectionViewLayout = layout;
self.collView.decelerationRate =
UIScrollViewDecelerationRateNormal;
// _contactdetailsarray=[NSMutableArray array];
// self.collvwhght.constant=0;
//self.collView.hidden=YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OppsContactCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"OppsContactCell"
forIndexPath:indexPath];
cell.contactimgvw.image = [cell.contactimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.contactimgvw setTintColor:[UIColor blackColor]];
cell.numberimgvw.image = [cell.numberimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.numberimgvw setTintColor:[UIColor blackColor]];
cell.emailimgvw.image = [cell.emailimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.emailimgvw setTintColor:[UIColor blackColor]];
if(_contactdetailsarray.count==1){
if(([[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"Designation"]isEqualToString:#""])&&([[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"Department"]isEqualToString:#"" ])){
cell.lbldesignation.text=#"";
}
else{
cell.lbldesignation.text=[NSString stringWithFormat:#"%#,%#",
[[_contactdetailsarray objectAtIndex:0]valueForKey:#"Designation"],
[[_contactdetailsarray objectAtIndex:0]valueForKey:#"Department"]];
}
cell.lblcontatname.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"ContactName"];
cell.lblcontactnumber.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"TelephoneNo"];
cell.lblemail.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"EmailAddress"];
}
else{
if(([[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"Designation"]isEqualToString:#""])&& .
([[[_contactdetailsarray
objectAtIndex:0]valueForKey:#"Department"]isEqualToString:#"" ])){
cell.lbldesignation.text=#"";
}
else{
cell.lbldesignation.text=[NSString stringWithFormat:#"%#,%#",
[[_contactdetailsarray
objectAtIndex:indexPath.section]valueForKey:#"Designation"],
[[_contactdetailsarray
objectAtIndex:indexPath.section]valueForKey:#"Department"]];
}
cell.lblcontatname.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:#"ContactName"];
cell.lblcontactnumber.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:#"TelephoneNo"];
cell.lblemail.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:#"EmailAddress"];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(_collView.bounds.size.width
,_collView.bounds.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section{
// return 5;
if(_contactdetailsarray.count==0){
return 0;
}
else{
return _contactdetailsarray.count;
}
}
Thanks & Regards,
Roshan.k

UITableViewController add item via method

I am using a UISplitviewController and I am trying to add items to the table view.
right now I have two ways
Create a button and add it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
and this code runs when the button is clicked:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
and adds an item to the table view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
this way works.
This is what I am trying to do:
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
but this does not update the table view. I added a breakpoint and when I use the button I added, it goes into the table view, with the method it does not and I am calling the method via [MasterController GetRequest];
What am I doing wrong ?
I am calling GetRequest from another controller.
This is how MasterController is getting defined:
#interface DetailController ()
{
MasterController *MasterController;
}
DetailController.m:
#import "MasterController.h"
#interface DetailController ()
{
MasterController *MasterController;
}
#end
#implementation DetailController
-(void)viewDidLoad {
MasterController = [[MasterController alloc]init];
}
[MasterController GetRequest];
MasterController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
As I suspected, with this line, MasterController = [[MasterController alloc]init], you're creating a new instance that has nothing to do with the one you see on screen. You need to get a reference to the master controller that you already have in the split view controller. From the detail view controller, you can get that like so,
MasterController = self.splitViewController.viewControllers.firstObject;
The split view controller has a viewControllers property, and the one at index 0 is the master, and the one at index 1 is the detail. BTW, you should start your ivars and method names with a lower class letter.
UITableViewController doesn't really work that way.
If you subclass it you need to implement several methods that tells the table what data it has -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
To add new rows you just need to add objects to self.objects and call [self.tableView reloadData];
It'll basically be -
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}

How we can get visible Cell IndexPath when a UICollectionView Loads its data initially

Is there any way that we can get indexPathsForVisibleItems when a UICollectionView loads its data initially . Basically I want all cell indexPaths which are currently visible on the screen. I am getting that in scrollViewDidScroll using below code.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self LoadVisibleItems];
}
-(void)LoadVisibleItems
{
NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;
NSArray *sortedIndexPaths = [visibleCellIndex sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath *path1 = (NSIndexPath *)obj1;
NSIndexPath *path2 = (NSIndexPath *)obj2;
return [path1 compare:path2];
}];
NSIndexPath* newFirstVisibleCell = [sortedIndexPaths firstObject];
NSIndexPath* newLastVisibleCell = [sortedIndexPaths lastObject];
}
Correct me if I misunderstand your question
If you load collectionView from storyboard
- (void) viewDidLoad {
[super viewDidLoad];
NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;
}
If you init collectionView by code after some view addSubview collectionView do
NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;
Try
[self.collectionView layoutIfNeeded];
NSArray *ary = collectionView.visibleCells;
for (UICollectionViewCell *cell in ary) {
NSIndexPath *path = [collectionView indexPathForCell:cell];
NSLog(#"indexPath of cell: %d - %d", (int)path.section, (int)path.row);
}
or use
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
}

UICollectionView insertItemsAtIndexPaths: raising error

I know there are all sorts of questions related to this, but none of the answers answered this.
Anyways, I have a UICollectionView that should be scroll forever, but I can't seem to get insertItemsAtIndexPaths: to work. It raises the error:
*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3688
Here is my code:
#import "ImageSliderViewController.h"
#define ITEM_CELL_IDENTIFIER #"ItemCell"
#interface ImageSliderViewController ()
#end
#implementation ImageSliderViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCollectionView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - UICollectionView methods
-(void)setupCollectionView {
self.items = [[NSMutableArray alloc] init];
loadingGap = 5;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[ImageSliderCell class] forCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
[self loadMorePosts];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.items.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ImageSliderCell *cell = (ImageSliderCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER forIndexPath:indexPath];
cell.imageTitle.text = [NSString stringWithFormat:#"%ld",(long)indexPath.item];
NSLog(#"%d : %d",indexPath.item,self.items.count-1);
if (indexPath.item >= self.items.count-1) {
[self loadMorePosts];
}
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = self.collectionView.frame.size;
size.height = size.height/2;
return size;
}
- (void)loadMorePosts
{
NSLog(#"Loading new items");
NSUInteger loopTill = self.items.count + loadingGap;
NSMutableArray *indexPathsToLoad = [NSMutableArray new];
while (self.items.count < loopTill) {
[indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]];
NSLog(#"Added section %d",self.items.count);
AsyncImageView *img = [[DatabaseFetcher alloc] getPostWithID:[NSString stringWithFormat:#"%lu",(unsigned long)self.items.count]
inSection:#"features"
withFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height)];
[self.items addObject:#[img]];
};
[self.collectionView reloadItemsAtIndexPaths:indexPathsToLoad];
}
#end
Thanks in advance!
Update: I updated the code to use reloadItemsAtIndexPaths and some other improvements base on suggestions. Thanks!
[NSIndexPath indexPathForItem:self.items.count inSection:0]
Should be:
[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0]
update
And you should submit all of your insertions at once by building up an NSMutableArray of index paths in your loop and inserting them at the end (as you've done and commented out). Alternatively, you can put your loop inset of a performBatchUpdates block.
thanks for all the answers.
It looks like I had a miscalculation when trying one of the solutions on the other thread. Here's my new solution that works:
-(void)addNewItems
{
NSLog(#"Loading new items");
NSMutableArray *indexPathsToLoad = [NSMutableArray new];
for (int i = 0; i < LOADING_GAP; i++) {
[indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]];
[self.items addObject:#"Test"];
}
if (self.items.count == LOADING_GAP) {
[self.collectionView reloadData];
} else {
[self.collectionView insertItemsAtIndexPaths:indexPathsToLoad];
}
}
Thank you!

ios Collapsable/expandable tableview on storyboard

i wanted to make collapsable/expandable tableview in which i have two headers product and services and each of them contains 10+ object with custom cell which contains checkmark on left side and label. i check for the tutorials but most of them are using .xib while my project is based on storyboard.
i check these tutorials, can anyone please help me regarding this.
https://www.cocoacontrols.com/controls/collapseclick
https://www.cocoacontrols.com/controls/ratreeview
https://www.cocoacontrols.com/controls/combobox-for-uitableview
I have found a great sample project on this at:
https://github.com/singhson/Expandable-Collapsable-TableView
It is very easy to understand and implement.
//
// ViewController.h
// expandableTV
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSMutableIndexSet *expandedSections;
}
#property (weak, nonatomic) IBOutlet UITableView *tbl_expandablecategory;
#end
//
// ViewController.m
// expandableTV
//
#import "ViewController.h"
#import "expandableTC.h"`
#interface ViewController ()
{
NSArray *jsonArray;
BOOL currentlyExpanded;
}
#end
#implementation ViewController
#synthesize tbl_expandablecategory;
NSMutableArray *arr_categorymenumodel;
- (void)viewDidLoad {
[super viewDidLoad];
arr_categorymenumodel=[[NSMutableArray alloc] init];
if (!expandedSections)
{
expandedSections = [[NSMutableIndexSet alloc] init];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;//[arr_categorymenumodel count];
}
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
return 2;
}
}
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
return 40;
}
return 270;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
if (!indexPath.row)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =#"Room ";// [NSString stringWithFormat:#"%#",[[arr_categorymenumodel objectAtIndex:indexPath.section] valueForKey:#"CategoryName"]];
// cell.backgroundColor=[UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:20.0];
if (currentlyExpanded)
{
}
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:#"rightarrow.png"]];
return cell;
}
else
{
expandableTC *cell = [tableView dequeueReusableCellWithIdentifier:#"Customcell"];
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:#"rightarrow.png"]];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:#"downarrow.png"]];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
#end

Resources