Trying to load UIViewController after click of item in TableViewController - ios

I feel like this is a fairly easy question, but I've tried for hours and can't seem to get it. I'm trying to create an app that calculates different equations that are critical to my job. Essentially, I've grouped the calculations into categories using nested TableViewControllers. This part works.
The part I am trying to figure out is how to push to a UIViewController so I can actually have the user enter data to do the calculations. For some reason I can't seem to get it to work. I am using Xcode 5 and am using Storyboards.
Really sorry for the long post. I've also included a screenshot of the storyboard at the following link: http://imgur.com/0oSxsR1
code below:
Root TableViewController Code (RootTableViewController.m)
#import "RootTableViewController.h"
#import "calculatorTableViewController.h"
#interface RootTableViewController ()
#end
#implementation RootTableViewController{
NSArray *calculatorOptions;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
calculatorOptions = [NSArray arrayWithObjects: #"General", #"Digital", #"Print", #"Television", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [calculatorOptions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CalculatorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [calculatorOptions objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"calculatorDetails"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
calculatorTableViewController *destViewController = segue.destinationViewController;
destViewController.calculationType = [calculatorOptions objectAtIndex:indexPath.row];
destViewController.title = destViewController.calculationType;
}
}
#end
Secondary TableViewController.h file (calculatorTableViewController.h)
#import <UIKit/UIKit.h>
#interface calculatorTableViewController : UITableViewController
#property (nonatomic, strong) NSString *calculationType;
#end
Secondary TableViewController.m file (calculatorTableViewController.m)
#import "calculatorTableViewController.h"
#import "CalculateGrpsHaveRF.h"
#interface calculatorTableViewController ()
#end
#implementation calculatorTableViewController{
NSArray *general;
NSArray *digital;
NSArray *print;
NSArray *television;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
general = [NSArray arrayWithObjects:#"Calculate GRPs (Have R & F)", #"Calculate Reach (Have GRPs & F)", #"Calculate Frequency (Have GRPs & R)", #"CPM to CPP", #"CPP to CPM", nil];
digital = [NSArray arrayWithObjects:#"Calculate Impressions (Have cost & CPM)", #"Calculate CPM (Have cost & impressions)", #"Calculate Cost (Have CPM and Impressions)", #"Calculate ASF", #"Calculate CPC/CPA/CPV", nil];
print = [NSArray arrayWithObjects:#"Calculate CPM", #"Calculate GRPs", #"Calculate CPP", nil];
television = [NSArray arrayWithObjects:#"Universe Estimates", #"Calculate GRPs", #"Calculate Spots", #"Calculate Average Rating", #"Calculate Total Impressions", #"Calculate CPP", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([_calculationType isEqualToString:#"General"]){
return [general count];
}
else if ([_calculationType isEqualToString:#"Digital"]){
return [digital count];
}
else if ([_calculationType isEqualToString:#"Print"]){
return [print count];
}
else if ([_calculationType isEqualToString:#"Television"]){
return [television count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Calculator2Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if ([_calculationType isEqualToString:#"General"]) {
cell.textLabel.text = [general objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Digital"]) {
cell.textLabel.text = [digital objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Print"]) {
cell.textLabel.text = [print objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Television"]) {
cell.textLabel.text = [television objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Calculate GRPs (Have R & F)"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CalculateGrpsHaveRF *destViewController = segue.destinationViewController;
destViewController.Title = [general objectAtIndex:indexPath.row];
#end

If I understand correctly, you want calculatorTableViewController to perform segue to next view.
//when you select a row
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//perform a segue
if (indexPath.row == 1)
{
[self performSegueWithIdentifier:#"your_segue_identifier" sender:your_sender];
}
else if (indexPath.row == 2)
{
[self performSegueWithIdentifier:#"another_segue_identifier" sender:your_sender];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//prepare your segue here
}

Related

showing NSMutabelArray in tableViewController in objective c

I am passing NSMutableArray to another a tableview and I want to show it in the table view. My NSMutableArray is as follows
2017-02-07 18:32:24.086 krib[13753:2978659] (
"Balcony.png",
"Utilities Included.png",
"Air-conditioning.png",
"Stove.png",
"WiFi Included.png",
"Queen Bed.png",
"Dining Table.png",
"Washing Machine.png",
"Dryer.png",
"Sofa.png",
"TV.png",
"Curtains.png",
"Refrigerator.png",
"Water Heater.png",
"Microwave Oven.png"
)
I send data as,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue"]) {
MZFormSheetPresentationViewControllerSegue *presentationSegue = (id)segue;
presentationSegue.formSheetPresentationController.presentationController.shouldApplyBackgroundBlurEffect = YES;
UINavigationController *navigationController = (id)presentationSegue.formSheetPresentationController.contentViewController;
AmennitiesTableTableViewController *presentedViewController = [navigationController.viewControllers firstObject];
// presentedViewController.textFieldBecomeFirstResponder = YES;
presentedViewController.passingString = facilities;
}
and receive data in table view controller,
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
I tried showing the mutable array as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
But I am not understanding what exactly i am missing to Populate data on the tableview.
I think you are missing to set tableview delegate & datasource.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
[self.myTable setDelegate:self];
[self.myTable setDataSource:self];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.passingString.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(self.passingString.count > 0){
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}

TableView and Detail view cell display

I'm learning how to make a contact using TableView and navigation controller. I have done the first view page with some cells, but I have some problem with showing the cells on the detail view page of each cell from first view.
when tap the Group A, Group B, Group C on first view page, it goes to the detail view of each cell, it should show cells named A1 A2 A3, B1 B2 B3 or C1 C2 C3 according to my code on detail view page. but it display nothing.
Hope someone can give me some advises, thank you so much!
FirstViewController.m
#implementation FirstViewController
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSections(NSInteger)section{
return [self.contactArray count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Celldentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Celldentifier];
if (cell == NULL) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Celldentifier];
}
cell.textLabel.text = [self.contactArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.myTableView = [self.contactArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"To Second View Segue" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"To the second view"]){
NSIndexPath *indexpath = [self.myTableView indexPathForSelectedRow];
self.contactArray = [self.contactArray objectAtIndex:indexpath.row];
SecondViewController *vc = segue.destinationViewController;
vc.memberName = [self.contactArray objectAtIndex:indexpath.row];
vc.title = vc.memberName;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.contactArray = [[NSMutableArray alloc]initWithObjects:#"Group A",#"Group B",#"Group C", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
SecondViewController.m
#implementation SecondViewController{
NSArray *groupA;
NSArray *groupB;
NSArray *groupC;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([self.memberName isEqualToString:#"groupA"]){
return [groupA count];
}
else if([self.memberName isEqualToString:#"groupB"]){
return [groupB count];
}
else if([self.memberName isEqualToString:#"groupC"]){
return [groupC count];
}
return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Celldentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Celldentifier];
if (cell == NULL) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Celldentifier];
}
if([self.memberName isEqualToString:#"groupA"]){
cell.textLabel.text = [groupA objectAtIndex:indexPath.row];
}
if([self.memberName isEqualToString:#"groupB"]){
cell.textLabel.text = [groupB objectAtIndex:indexPath.row];
}
if([self.memberName isEqualToString:#"groupC"]){
cell.textLabel.text = [groupC objectAtIndex:indexPath.row];
}
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
groupA = [NSArray arrayWithObjects:#"A1",#"A2",#"A3",nil];
groupB = [NSArray arrayWithObjects:#"B1",#"B2",#"B3",nil];
groupC = [NSArray arrayWithObjects:#"C1",#"C2",#"C3",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
first view
detail view but nothing displayed
Have you set the delegate and datasource of tableview?
If Yes, check if the datasource methods (numberOfRowsInSection:) are called after you do
groupA = [NSArray arrayWithObjects:#"A1",#"A2",#"A3",nil]; in viewDidLoad method.

Load Array in UITableView Cell

I want to make paging swipe left and right using UIScrollView after view detailController.
First, main.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OZDetailViewController *detailViewController = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
detailViewController.arrDetailNews = [_arrNews objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
OZDetailViewController *arrNewsAll = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
arrNewsAll.allNewsArray = _arrNews;
[self.navigationController pushViewController:arrNewsAll animated:YES];
}
When I selected content in tableviewcell, arrDetailNews can loaded in method viewDidLoad() and cellForRowAtIndexPath(). But arrNewsAll cannot loaded in method cellForRowAtIndexPath().
This is my detailViewController.h:
#property (nonatomic, copy) NSArray *allNewsArray;
And detailViewCOntroller.m:
#synthesize allNewsArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"dataArray: %#", allNewsArray);
}
- (int)numberINSectionsInTableView: (UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"dataArrayCell: %#", allNewsArray);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"New Cell"];
}
if(indexPath.row != self.arrDetailNews.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor whiteColor];
[cell addSubview:line];
}
tableView.allowsSelection = NO;
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
if (indexPath.row==0) {
cell.textLabel.text = #"1st";
}
if (indexPath.row==1) {
cell.textLabel.text = #"2nd";
}
if (indexPath.row==2) {
cell.textLabel.text = #"3rd";
}
return cell;
}
If allNewsArray can loaded in cellForRowAtIndexPath() I can continue next step for paging with UIScrollView. Note, numberOfRowsInSection I set to 4 because I need 4 rows (custom view).
Assuming you setup your delegate & dataSource outlets correctly from xib/storyboard, you still need to specify the number of rows per section (or number of sections).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return allNewsArray.count;
}
Alternatively, the method for number of sections is:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

Xcode tableview does not show the text

I've ran into a problem. I have a TableView that I have created programmatically which does not display the TextView that I have added corresponding to the rows of TableView. I cannot find a solution to this on Stack Overflow.
#import "MasterTableViewController.h"
#interface MasterTableViewController ()
#end
#implementation MasterTableViewController
-(NSMutableArray *)numbers {
if(!_numbers)
{
_numbers = [[NSMutableArray alloc] init];
}
return _numbers;
}
-(NSMutableArray *)subtile {
if(!_subtile)
{
_subtile = [[NSMutableArray alloc] init];
}
return _subtile;
}
- (void)viewDidLoad
{
//Adding Titles
[self.champions addObject:#"1"];
[self.champions addObject:#"2"];
//Adding subtitles
[self.subtileChamps addObject:#"One"];
[self.subtileChamps addObject:#"Two"];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.numbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = self.numbers[indexPath.row];
cell.detailTextLabel.text = self.subtile[indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#end
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: reuseIdentifier:]
}
You should init your cell
And... You have a common way for initiate your variables. This one should be suitable for you. That method calls before all others and you can initiate your variables here:
-(id)initWithStyle:(UITableViewStyle)style
{
if (self = [super initWithStyle:style])
{
someArray = [NSMutableArray array]; //init the array
}
return self;
}

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