Related
I developed expandable and collapsed table view for dynamic data comes from the server. I'm displaying state names in header view successfully, but I can't displaying child data that is districts related to state names.
I followed Link for this http://www.iostute.com/2015/04/expandable-and-collapsable-tableview.html
My data is
_response = #{#"Response":#{#"status":#"SUCCESS",#"error_code":#"0",#"message":#"SUCCESS",#"Array":#[
#{#"state_id":#"0",#"state_name":#"null",#"district_id":#"0",#"district_name":#"null"},
#{#"state_id":#"01",#"state_name":#"State1",#"district_id":#"001",#"district_name":#"State1District1"},
#{#"state_id":#"02",#"state_name":#"State2",#"district_id":#"004",#"district_name":#"State2District1"},
#{#"state_id":#"02",#"state_name":#"State2",#"district_id":#"005",#"district_name":#"State3District1"},
#{#"state_id":#"01",#"state_name":#"State1",#"district_id":#"002",#"district_name":#"State1District2"},
#{#"state_id":#"01",#"state_name":#"State1",#"district_id":#"003",#"district_name":#"State1District3"},
#{#"state_id":#"03",#"state_name":#"State3",#"district_id":#"006",#"district_name":#"State3District1"},
#{#"state_id":#"04",#"state_name":#"State4",#"district_id":#"008",#"district_name":#"State4District1"},
#{#"state_id":#"04",#"state_name":#"State4",#"district_id":#"009",#"district_name":#"State4District2"},
#{#"state_id":#"04",#"state_name":#"State4",#"district_id":#"010",#"district_name":#"State4District3"},
#{#"state_id":#"05",#"state_name":#"State5",#"district_id":#"011",#"district_name":#"State5District1"},
#{#"state_id":#"05",#"state_name":#"State5",#"district_id":#"012",#"district_name":#"State5District2"},
#{#"state_id":#"03",#"state_name":#"State3",#"district_id":#"007",#"district_name":#"State3District2"}]}, #"count":#"6"};
My code is
if ([[[_response objectForKey:#"Response"] objectForKey:#"status"] isEqualToString:#"SUCCESS"] && (!(_integer == 0))) {
_stateID = [[NSMutableArray alloc] init];
_stateName = [[NSMutableArray alloc] init];
_districtID = [[NSMutableArray alloc] init];
_districtName = [[NSMutableArray alloc] init];
_stateIdStateNameDic = [[NSMutableDictionary alloc]init];
//Add arrays to array to remove null values dynamically
NSArray *arr = [[NSArray alloc]initWithObjects:_stateID, _stateName, _districtID, _districtName, nil];
for (int i=0; i<_integer; i++) {
[_stateID addObject:[[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"state_id"]];
[_stateName addObject:[[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"state_name"]];
[_districtID addObject:[[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"district_id"]];
[_districtName addObject:[[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"district_name"]];
//Remove null values
for (int j=0; j<arr.count; j++) {
for (NSMutableArray *ar in arr) {
if ([[ar objectAtIndex:i] isKindOfClass:[NSNull class]] || [[ar objectAtIndex:i] isEqualToString:#"null"] || [[ar objectAtIndex:i] isEqualToString:#"0"]) {
[ar addObject:#""];
[ar removeObjectAtIndex:i];
}
}
}
}
//Add arrays to mutable array to remove empty objects
NSMutableArray *marr = [[NSMutableArray alloc]initWithObjects:_stateID, _stateName, _districtID, _districtName, nil];
//Remove empty objects from all arrays
for (int j=0; j<marr.count; j++) {
for (int i=0; i<[[marr objectAtIndex:j] count]; i++) {
if ([[[marr objectAtIndex:j] objectAtIndex:i] isEqualToString:#""]) {
[[marr objectAtIndex:j] removeObjectAtIndex:i];
}
}
}
//Remove duplicates from state names array
_stateName = [_stateName valueForKeyPath:#"#distinctUnionOfObjects.self"];
NSString *districtName = #"";
NSString * superater = #"&&";
_mdic = [[NSMutableDictionary alloc]init];
for (int j=0; j<_stateName.count; j++) {
for (int i=0; i<_integer; i++) {
if ([[_stateName objectAtIndex:j] isEqualToString:[[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"state_name"]]) {
//Remove district name if empty or null
if ([districtName isEqualToString:#""] || [districtName isEqual:[NSNull null]]) {
districtName = [[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"district_name"];
if ([districtName isEqual:[NSNull null]] || [districtName isEqualToString:#"null"]) {
districtName = #"";
}
} else {
//Add all districts with superater &&
districtName = [districtName stringByAppendingString:[NSString stringWithFormat:#"%#%#", superater, [[[[_response objectForKey:#"Response"] objectForKey:#"Array"] objectAtIndex:i] objectForKey:#"district_name"]]];
}
}
}
//Create district names dictionary with state name keys
[_mdic setValue:districtName forKey:[_stateName objectAtIndex:j]];
districtName = #"";
}
NSLog(#"_mdic %#", _mdic);
_arrayForBool=[[NSMutableArray alloc]init];
//Save bool value " NO " based on sectionTitleArray count.
for (int i=0; i<[_stateName count]; i++) {
[_arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[_availableOrdersTableView reloadData];
});
} else {
}
// TableView delegates
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_mdic count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows in each section
if ([[_arrayForBool objectAtIndex:section] boolValue]) {
NSLog(#"%#", [_stateName objectAtIndex:section]);
NSLog(#"%#", _mdic);
NSArray *mdicKeys = [_mdic allKeys];
for (int i=0; i<_mdic.count; i++) {
if ([[mdicKeys objectAtIndex:i] isEqualToString:[_stateName objectAtIndex:section]]) {
NSString *str = [_mdic objectForKey:[_stateName objectAtIndex:section]];
NSLog(#"%#", str);
_subDistrictArr = [str componentsSeparatedByString:#"&&"];
}
}
NSLog(#"_subDistrictIDArr %#", _subDistrictArr);
return _subDistrictArr.count;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Create cell
static NSString *cellid=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
BOOL manyCells = [[_arrayForBool objectAtIndex:indexPath.section] boolValue];
/********** If the section supposed to be closed *******************/
if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=#"";
}
/********** If the section supposed to be Opened *******************/
else {
cell.textLabel.text=[_subDistrictArr objectAtIndex:indexPath.row];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
cell.textLabel.font=[UIFont systemFontOfSize:20.0f];
} else {
cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
}
cell.backgroundColor=[UIColor whiteColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone ;
}
cell.textLabel.textColor=[UIColor blackColor];
/********** Add a custom Separator with cell *******************/
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(15, 48, _availableOrdersTableView.frame.size.width-15, 1)];
separatorLineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:separatorLineView];
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _availableOrdersTableView.frame.size.width, 50)];
sectionView.backgroundColor = [UIColor clearColor];
sectionView.tag=section;
UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, _availableOrdersTableView.frame.size.width, sectionView.frame.size.height)];
viewLabel.backgroundColor=[UIColor clearColor];
viewLabel.textColor=[UIColor blackColor];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
viewLabel.font=[UIFont systemFontOfSize:25];
} else {
viewLabel.font=[UIFont systemFontOfSize:15];
}
viewLabel.text=[NSString stringWithFormat:#"%#", [_stateName objectAtIndex:section]];
_stateIDString = [_stateID objectAtIndex:section];
NSLog(#"stateIDString %#", _stateIDString);
[sectionView addSubview:viewLabel];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(sectionView.frame.size.width-45, (sectionView.frame.size.height-25)/2, 18, 17)];
imgView.tag = section;
imgView.image = [UIImage imageNamed:#"DA"];
[sectionView addSubview:imgView];
} else {
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(sectionView.frame.size.width-35, (sectionView.frame.size.height-25)/2, 18, 17)];
imgView.tag = section;
imgView.image = [UIImage imageNamed:#"DA"];
[sectionView addSubview:imgView];
}
/********** Add a custom Separator with Section view *******************/
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, sectionView.frame.size.height, _availableOrdersTableView.frame.size.width, 1)];
separatorLineView.backgroundColor = [UIColor blackColor];
[sectionView addSubview:separatorLineView];
/********** Add UITapGestureRecognizer to SectionView **************/
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];
return sectionView;
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
// _gestureInteger = gestureRecognizer.view.tag;
if (indexPath.row == 0) {
BOOL collapsed = [[_arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[_stateName count]; i++) {
if (indexPath.section==i) {
[_arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
}
}
[_availableOrdersTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
// [_availableOrdersTableView reloadData];
}
}
I have two solution for this
(1) initially you put numberofrow for any section is zero and after click on any section you can add row for for clicked section.
(2) number of section will be one and you need to use two cell "cellwithheaderonly" and "cellwithheaderandsubpart" initially you will use "cellwithheaderonly" and when user will click on any cell you need to use "cellwithheaderandsubpart"
You can take reference from following URL
Expanding and Collapsing table view cells in ios
https://www.anexinet.com/blog/expandable-collapsible-uitableview-sections/
You have to maintain an array given below : -
lat arr = [[“name”:”firstRow” , “subRowArray”:[1,2,3,4]],[“name”:”secondRow” , “subRowArray”:[1,2,3,4]],[“name”:”thirdRow” , “subRowArray”:[1,2,3,4]]]
then expand and collapse according to your array .
if would you like to use library then go for expendable Tableview
When I launch my app, tap on a cell in my RootViewController (AufnahmeIstTableViewController), it opens up my DetailViewController (AufnahmeISTDetailTableViewController).When I then tap on cell, it should open a third ViewController (AufnahmeIstDetailDetailViewController). My app works like in this tutorial: http://www.youtube.com/watch?v=99Ssk1-HUq4
But when I tap on a cell and open my third view, the labels on my third view controller don't display any text as declared in the code.
What do I have to change? Here are all my files:
Here is my AufnahmeIstTableViewController.h file:
#import <UIKit/UIKit.h>
#interface AufnahmeIstTableViewController : UITableViewController
#property (strong, nonatomic) NSMutableArray *categoryArray;
#end
Here's my AufnahmeIstTableViewController.m file:
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstTableViewController ()
#end
#implementation AufnahmeIstTableViewController
#synthesize categoryArray;
-(NSMutableArray *)categoryArray
{
if (!categoryArray)
{
categoryArray = [[NSMutableArray alloc]init];
}
return categoryArray;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.categoryArray addObject:#"Gesetze"];
[self.categoryArray addObject:#"Verordnungen"];
[self.categoryArray addObject:#"Technische Regeln"];
[self.categoryArray addObject:#"Berufsgenossenschaft"];
[self.categoryArray addObject:#"Management"];
[self.categoryArray addObject:#"Personal"];
[self.categoryArray addObject:#"Vertrieb"];
[self.categoryArray addObject:#"Kunden"];
[self.categoryArray addObject:#"Lieferanten"];
[self.categoryArray addObject:#"Arbeitsumgebung"];
[self.categoryArray addObject:#"Produktion"];
[self.categoryArray addObject:#"Produkte"];
[self.categoryArray addObject:#"Messmittel"];
[self.categoryArray addObject:#"Informationssicherheit"];
[self.categoryArray addObject:#"Rechnungswesen"];
[self.categoryArray addObject:#"Dritte"];
[self setTitle:#"Ist-Aufnahme"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (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 self.categoryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = self.categoryArray [indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AufnahmeISTDetailTableViewController *categories = [[AufnahmeISTDetailTableViewController alloc]init];
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Gesetze"])
categories.istAufnahmeInt = 0;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Verordnungen"])
categories.istAufnahmeInt = 1;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Technische Regeln"])
categories.istAufnahmeInt = 2;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Berufsgenossenschaft"])
categories.istAufnahmeInt = 3;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Management"])
categories.istAufnahmeInt = 4;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Personal"])
categories.istAufnahmeInt = 5;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Vertrieb"])
categories.istAufnahmeInt = 6;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Kunden"])
categories.istAufnahmeInt = 7;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Lieferanten"])
categories.istAufnahmeInt = 8;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Arbeitsumgebung"])
categories.istAufnahmeInt = 9;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Produktion"])
categories.istAufnahmeInt = 10;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Produkte"])
categories.istAufnahmeInt = 11;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Messmittel"])
categories.istAufnahmeInt = 12;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Informationssicherheit"])
categories.istAufnahmeInt = 13;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Rechnungswesen"])
categories.istAufnahmeInt = 14;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Dritte"])
categories.istAufnahmeInt = 15;
[categories setTitle:[self.categoryArray objectAtIndex:indexPath.row]];
//[self.navigationController pushViewController:categories animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"DetailView"])
{
AufnahmeISTDetailTableViewController *controller = (AufnahmeISTDetailTableViewController *)segue.destinationViewController;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Gesetze"])
controller.istAufnahmeInt = 0;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Verordnungen"])
controller.istAufnahmeInt = 1;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Technische Regeln"])
controller.istAufnahmeInt = 2;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Berufsgenossenschaft"])
controller.istAufnahmeInt = 3;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Management"])
controller.istAufnahmeInt = 4;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Personal"])
controller.istAufnahmeInt = 5;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Vertrieb"])
controller.istAufnahmeInt = 6;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Kunden"])
controller.istAufnahmeInt = 7;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Lieferanten"])
controller.istAufnahmeInt = 8;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Arbeitsumgebung"])
controller.istAufnahmeInt = 9;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Produktion"])
controller.istAufnahmeInt = 10;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Produkte"])
controller.istAufnahmeInt = 11;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Messmittel"])
controller.istAufnahmeInt = 12;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Informationssicherheit"])
controller.istAufnahmeInt = 13;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Rechnungswesen"])
controller.istAufnahmeInt = 14;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Dritte"])
controller.istAufnahmeInt = 15;
[controller setTitle:[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row]];
}
}
#end
Here's my AufnahmeISTDetailTableViewController.h file:
#import <UIKit/UIKit.h>
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeIstDetailDetailViewController.h"
#interface AufnahmeISTDetailTableViewController : UITableViewController {
NSMutableArray *gesetzeArray;
NSMutableArray *verordnungenArray;
NSMutableArray *technischeregelnArray;
NSMutableArray *berufsgenossenschaftArray;
NSMutableArray *managementArray;
NSMutableArray *personalArray;
NSMutableArray *vertriebArray;
NSMutableArray *kundenArray;
NSMutableArray *lieferantenArray;
NSMutableArray *arbeitsumgebungArray;
NSMutableArray *produktionArray;
NSMutableArray *produkteArray;
NSMutableArray *messmittelArray;
NSMutableArray *informationssicherheitArray;
NSMutableArray *rechnungswesenArray;
NSMutableArray *dritteArray;
}
#property int istAufnahmeInt;
#property AufnahmeIstTableViewController *categories;
-(void)makeData;
#end
And here's my AufnahmeISTDetailTableViewController.m file:
#import "AufnahmeISTDetailTableViewController.h"
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeIstDetailDetailViewController.h"
#interface AufnahmeISTDetailTableViewController ()
#end
#implementation AufnahmeISTDetailTableViewController
#synthesize istAufnahmeInt;
#synthesize categories;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self makeData];
}
-(void)makeData
{
gesetzeArray = [[NSMutableArray alloc]init];
verordnungenArray = [[NSMutableArray alloc]init];
technischeregelnArray = [[NSMutableArray alloc]init];
berufsgenossenschaftArray= [[NSMutableArray alloc]init];
managementArray= [[NSMutableArray alloc]init];
personalArray= [[NSMutableArray alloc]init];
vertriebArray= [[NSMutableArray alloc]init];
kundenArray= [[NSMutableArray alloc]init];
lieferantenArray= [[NSMutableArray alloc]init];
arbeitsumgebungArray= [[NSMutableArray alloc]init];
produktionArray= [[NSMutableArray alloc]init];
produkteArray= [[NSMutableArray alloc]init];
messmittelArray= [[NSMutableArray alloc]init];
informationssicherheitArray= [[NSMutableArray alloc]init];
rechnungswesenArray= [[NSMutableArray alloc]init];
dritteArray= [[NSMutableArray alloc]init];
//Gesetze
[gesetzeArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Verordnungen
[verordnungenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Verordnung zur arbeitsmedizinischen Vorsorge – ArbMedVV",#"name",#"Verordnung zur arbeitsmedizinischen Vorsorge – ArbMedVV wurde gedrückt",#"description", nil]];
//Technische Regeln
[technischeregelnArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Technische Regeln",#"name",#"Technische Regeln wurde gedrückt", #"description", nil]];
//Berufsgenossenschaft
[berufsgenossenschaftArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Berufsgenossenschaft",#"name",#"Berufsgenossenschaft wurde gedrückt", #"description", nil]];
//Management
[managementArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Personal
[personalArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Vertrieb
[vertriebArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Kunden
[kundenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Lieferanten
[lieferantenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Arbeitsumgebung
[arbeitsumgebungArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Produktion
[produktionArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Produkte
[produkteArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Messmittel
[messmittelArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Informationssicherheit
[informationssicherheitArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Rechnungswesen
[rechnungswesenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Dritte
[dritteArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", 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.
if (self.istAufnahmeInt == 0)
{
return [gesetzeArray count];
}
if (self.istAufnahmeInt == 1)
{
return [verordnungenArray count];
}
if (self.istAufnahmeInt == 2)
{
return [technischeregelnArray count];
}
if (self.istAufnahmeInt == 3)
{
return [berufsgenossenschaftArray count];
}
if (self.istAufnahmeInt == 4)
{
return [managementArray count];
}
if (self.istAufnahmeInt == 5)
{
return [personalArray count];
}
if (self.istAufnahmeInt == 6)
{
return [vertriebArray count];
}
if (self.istAufnahmeInt == 7)
{
return [kundenArray count];
}
if (self.istAufnahmeInt == 8)
{
return [lieferantenArray count];
}
if (self.istAufnahmeInt == 9)
{
return [arbeitsumgebungArray count];
}
if (self.istAufnahmeInt == 10)
{
return [produktionArray count];
}
if (self.istAufnahmeInt == 11)
{
return [produkteArray count];
}
if (self.istAufnahmeInt == 12)
{
return [messmittelArray count];
}
if (self.istAufnahmeInt == 13)
{
return [informationssicherheitArray count];
}
if (self.istAufnahmeInt == 14)
{
return [rechnungswesenArray count];
}
if (self.istAufnahmeInt == 15)
{
return [dritteArray count];
}
else
{
return 1;
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"DetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
// Configure the cell...
if (self.istAufnahmeInt == 0) cell.textLabel.text = [[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 1) cell.textLabel.text = [[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name" ];
if (self.istAufnahmeInt == 2) cell.textLabel.text = [[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 3) cell.textLabel.text = [[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 4) cell.textLabel.text = [[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 5) cell.textLabel.text = [[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 6) cell.textLabel.text = [[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 7) cell.textLabel.text = [[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 8) cell.textLabel.text = [[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 9) cell.textLabel.text = [[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 10) cell.textLabel.text = [[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 11) cell.textLabel.text = [[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 12) cell.textLabel.text = [[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 13) cell.textLabel.text = [[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 14) cell.textLabel.text = [[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 15) cell.textLabel.text = [[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AufnahmeIstDetailDetailViewController *aufnahmeistDetail =[[AufnahmeIstDetailDetailViewController alloc]initWithNibName:#"AufnahmeIstDetailDetailViewController" bundle:nil];
if (istAufnahmeInt == 0)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 1)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 2)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 3)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 4)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 5)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 6)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 7)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 8)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 9)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 10)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 11)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 12)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 13)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 14)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 15)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
}
#end
Here's my AufnahmeISTDetailTableViewController.h file:
#import <UIKit/UIKit.h>
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstDetailDetailViewController : UIViewController
{
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *textLabel;
NSString *titelString;
NSString *textString;
}
#property (nonatomic, retain) NSString *titelString;
#property (nonatomic, retain) NSString *textString;
#end
And finally, here's my AufnahmeISTDetailTableViewController.m file:
#import "AufnahmeIstDetailDetailViewController.h"
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstDetailDetailViewController ()
#end
#implementation AufnahmeIstDetailDetailViewController
#synthesize textString;
#synthesize titelString;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titelString;
textLabel.text = textString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (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
My suggestion is to just set breakpoints and ensure that your code that sets the labels is actually setting the correct string values. If you are sure about that, then there must be something wrong with your IB files (clearColor text, maybe?).
On another note, you have a horrendous amount of hardcoding in your view controllers. You should decouple your data from your views.
It sounds like it is either an in-house App or one for OBI given the Messmittel and Lieferanten arrays, but this is out of the scope and with that aside.
tilo's suggestion is correct, if you would like to get a hold of the controller through storyboard and segue to it while passing objects to it too.
Here is what I have noticed in your code, right from the very first AufnahmeIstTableViewController.h:
In your didSelectRowAtIndexPath you don't actually need to check the values against a string, because your array does in fact contains those elements namely "Gesetze", "Verordnungen", "Technische Regeln", "Berufsgenossenschaft" etc.
So basically you could just to:
controller.istAufnahmeInt = [[self.tableView indexPathForSelectedRow] row];
Or safer:
if([self.categoriesArray count] >= [[self.tableView indexPathForSelectedRow] row])
{
controller.istAufnahmeInt = [[self.tableView indexPathForSelectedRow] row];
}
Now in your prepareForSegue:
You're basically doing the same thing there. So what you really need to do is just:
controller.istAufnahmeInt = categories.istAufnahmeInt;
So apply the same logic for your second view controller to your third using till's suggestion to pass the strings to it.
Hope this helps.
in your second view controller, you are setting titelString and textString on a viewController that you don't actually show.
You need to implement prepareForSegue:sender: in your second view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AufnahmeIstDetailDetailViewController *aufnahmeistDetail = [segue destinationViewController];
int selectedRow = [self.tableView indexPathForSelectedRow].row;
aufnahmeistDetail.titleString = ...; // get the appropriate string based on the selected row
aufnahmeistDetail.textString = ...;
}
i'm using UiScrollSlidingPages (https://github.com/TomThorpe/UIScrollSlidingPages) plugin but i have a problem. The displayed view for every page is the same and it is the last one view allocated.
This is the method of datasource delegate that i'm using
-(TTSlidingPage *)pageForSlidingPagesViewController:(TTScrollSlidingPagesController*)source atIndex:(int)index{
variabiliGlobali.paginaFormazione = index + 1;
UIViewController *viewController;
viewController = [[foaPaginaFormazioneVC alloc] init];
return [[TTSlidingPage alloc] initWithContentViewController:viewController];
}
This is the foaPaginaFormazioneVC code.
#import "foaPaginaFormazioneVC.h"
#import "foaAppDelegate.h"
#import "foaVotoCalciatoreCell.h"
#import "foaBonusTotaliCell.h"
#import "foaRiepilogoCell.h"
#implementation foaPaginaFormazioneVC
foaAppDelegate *variabiliGlobali;
NSDictionary *formazione;
NSDictionary *elencoCalciatori;
NSDictionary *logFormazione;
NSDictionary *logCalcolo;
NSMutableArray *titolari;
NSMutableArray *panchinari;
NSMutableArray *logForTipo;
NSMutableArray *logForValore;
NSMutableArray *logCalTipo;
NSMutableArray *logCalValore;
bool calcolata;
- (void)viewDidLoad
{
[super viewDidLoad];
self.cellZoomInitialAlpha = [NSNumber numberWithFloat:1.0];
self.cellZoomAnimationDuration = [NSNumber numberWithFloat:0.5];
self.cellZoomXScaleFactor = [NSNumber numberWithFloat:0.9];
self.cellZoomYScaleFactor = [NSNumber numberWithFloat:0.9];
variabiliGlobali = (foaAppDelegate *)[[UIApplication sharedApplication] delegate];
formazione = [variabiliGlobali.formazioniA objectAtIndex: variabiliGlobali.paginaFormazione - 1];
elencoCalciatori = [[NSDictionary alloc] init];
logFormazione = [[NSDictionary alloc] init];
logCalcolo = [[NSDictionary alloc] init];
if ([formazione objectForKey:#"FORMAZIONE"] != [NSNull null]) {
elencoCalciatori = [formazione objectForKey:#"FORMAZIONE"];
}
if ([formazione objectForKey:#"LOG_FORMAZIONE"] != [NSNull null]) {
logFormazione = [formazione objectForKey:#"LOG_FORMAZIONE"];
}
if ([formazione objectForKey:#"LOG_CALCOLO"] != [NSNull null]) {
logCalcolo = [formazione objectForKey:#"LOG_CALCOLO"];
}
titolari = [[NSMutableArray alloc] init];
panchinari = [[NSMutableArray alloc] init];
for (NSDictionary *dict in elencoCalciatori) {
if ([[dict objectForKey:#"ORDINE"] intValue] <= 11) {
[titolari addObject: dict];
}
else {
[panchinari addObject: dict];
}
}
logForTipo = [[NSMutableArray alloc] init];
logForValore = [[NSMutableArray alloc] init];
logCalTipo = [[NSMutableArray alloc] init];
logCalValore = [[NSMutableArray alloc] init];
NSString *appString;
bool appBool;
float appNumber;
if ([logFormazione count] != 0) {
//AMMINISTRATORE
appBool = [[logFormazione objectForKey:#"AMMINISTRATORE"] boolValue];
if (appBool) {
[logForTipo addObject: #"Formazione inserita dall'amministratore"];
[logForValore addObject: #""];
}
//MODULO INIZIALE
appString = [logFormazione objectForKey:#"MODULO_INIZIALE"];
[logForTipo addObject: #"Modulo Iniziale"];
[logForValore addObject: appString];
//DATA
appString = [logFormazione objectForKey:#"DATA"];
[logForTipo addObject: #"Data Ultima Modifica"];
[logForValore addObject: appString];
}
if ([logCalcolo count] != 0) {
//MODULO FINALE
appString = [logCalcolo objectForKey:#"MODULO_FINALE"];
[logForTipo addObject: #"Modulo Finale"];
[logForValore addObject: appString];
//PORTIERE IMBATTUTO
appNumber = [[logCalcolo objectForKey:#"PORTIERE_IMBATTUTO"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Portiere Imbattuto"];
[logCalValore addObject: [doubleNumber stringValue]];
}
//MOD_P
appNumber = [[logCalcolo objectForKey:#"MOD_P"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Modificatore Portiere"];
[logCalValore addObject: [doubleNumber stringValue]];
}
//MOD_D
appNumber = [[logCalcolo objectForKey:#"MOD_D"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Modificatore Difesa"];
[logCalValore addObject: [doubleNumber stringValue]];
}
//MOD_C
appNumber = [[logCalcolo objectForKey:#"MOD_C"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Modificatore Centrocampo"];
[logCalValore addObject: [doubleNumber stringValue]];
}
//MOD_A
appNumber = [[logCalcolo objectForKey:#"MOD_A"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Modificatore Attacco"];
[logCalValore addObject: [doubleNumber stringValue]];
}
//PUNTEGGIO
appNumber = [[logCalcolo objectForKey:#"PUNTEGGIO"] doubleValue];
if (appNumber != 0) {
NSNumber *doubleNumber = [NSNumber numberWithDouble:appNumber];
[logCalTipo addObject: #"Punteggio Finale"];
[logCalValore addObject: [doubleNumber stringValue]];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (variabiliGlobali.gioCalcolata) {
return 4;
}
else {
return 3;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger *numero;
//La giornata è calcolata. Sezione 2 = BONUS e TOTALI
if (variabiliGlobali.gioCalcolata && section == 2) {
numero = [logCalTipo count];
}
//La giornata è calcolata. Sezione 3 = RIEPILOGO
//La giornata non è calcolata. Sezione 2 = RIEPILOGO
if ((variabiliGlobali.gioCalcolata && section == 3) || (variabiliGlobali.gioCalcolata == false && section == 2)) {
numero = [logForTipo count];
}
if (section == 1)
{numero = [panchinari count];}
if ( section == 0)
{numero = [titolari count];}
return numero;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *idDettCal = #"cellaVotoCalciatore";
NSString *idRiepilogo = #"riepilogoCell";
NSString *idTotali = #"bonusTotaliCell";
//Dictionary per Titolari e Panchinari
NSDictionary *calciatore;
if (indexPath.section == 0 || indexPath.section == 1) {
foaVotoCalciatoreCell *cell = (foaVotoCalciatoreCell *)[tableView dequeueReusableCellWithIdentifier:idDettCal];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idDettCal owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//TITOLARI
if (indexPath.section == 0) {
calciatore = [titolari objectAtIndex:indexPath.row];
}
//PANCHINARI
else if (indexPath.section == 1) {
calciatore = [panchinari objectAtIndex:indexPath.row];
}
NSString *calciatoreStr =
[NSString stringWithFormat:#"%#%#%#%#",
[calciatore objectForKey:#"CALCIATORE"],
#" (",
[[calciatore objectForKey:#"SQUADRA"] substringToIndex:3],
#")"];
NSString *imgRuolo =
[NSString stringWithFormat:#"%#%#",
[calciatore objectForKey:#"RUOLO"],
#".jpg"];
cell.ruolo.image = [UIImage imageNamed:imgRuolo];
NSDictionary *dettaglioVoto;
UIImageView *imageIcona;
CGFloat offset;
dettaglioVoto = [calciatore objectForKey:#"VOTO_STM"];
offset = 0;
for (NSString *icona in dettaglioVoto) {
if ([icona isEqualToString: #"AMMONITO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"amm.png"]];
}
else if ([icona isEqualToString: #"GOL_FATTO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"golfatto.png"]];
}
else if ([icona isEqualToString: #"GOL_SUBITO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"golsubito.png"]];
}
else if ([icona isEqualToString: #"ENTRATO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"entrato.png"]];
}
else if ([icona isEqualToString: #"USCITO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"uscito.png"]];
}
else if ([icona isEqualToString: #"ESPULSO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"esp.png"]];
}
else if ([icona isEqualToString: #"ASSIST"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"assist.png"]];
}
else if ([icona isEqualToString: #"ASSIST_FERMO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"assistf.png"]];
}
else if ([icona isEqualToString: #"RIGORE_SEGNATO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rigoresegnato.png"]];
}
else if ([icona isEqualToString: #"RIGORE_SBAGLIATO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rigoresbagliato.png"]];
}
else if ([icona isEqualToString: #"RIGORE_PARATO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rigoreparato.png"]];
}
else if ([icona isEqualToString: #"AUTOGOL"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"autogol.png"]];
}
else if ([icona isEqualToString: #"GOL_PARTITA"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"golvittoria.png"]];
}
else if ([icona isEqualToString: #"GOL_PAREGGIO"]) {
imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"golpareggio.png"]];
}
imageIcona.frame = CGRectMake(offset, 0, imageIcona.frame.size.width, imageIcona.frame.size.height);
//imageIcona.frame = CGRectMake(offset, 0, 20, 20);
offset = imageIcona.frame.size.width + offset + 2;
[cell.iconeDettaglio addSubview: imageIcona];
}
if (offset == 0) {
//cell.calciatore.frame = CGRectMake(cell.calciatore.frame.origin.x, 14, cell.calciatore.frame.size.width, cell.calciatore.frame.size.height);
}
cell.calciatore.text = calciatoreStr;
cell.votoIniziale.text = [calciatore objectForKey:#"VOTO_PRNT"];
cell.votoFinale.text = [calciatore objectForKey:#"VOTO_PRNT_TOT"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
//La giornata è calcolata. Sezione 2 = BONUS e TOTALI
else if (variabiliGlobali.gioCalcolata && indexPath.section == 2) {
NSDictionary *totaliVal;
NSDictionary *totaliTip;
totaliVal = [logCalValore objectAtIndex:indexPath.row];
totaliTip = [logCalTipo objectAtIndex:indexPath.row];
foaBonusTotaliCell *cell = (foaBonusTotaliCell *)[tableView dequeueReusableCellWithIdentifier:idTotali];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idTotali owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.tipoBonusLabel.text = totaliTip;
cell.totaleLabel.text = totaliVal;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
//La giornata è calcolata. Sezione 3 = RIEPILOGO
//La giornata non è calcolata. Sezione 2 = RIEPILOGO
//else ((variabiliGlobali.gioCalcolata && indexPath.section == 3) ||
// (variabiliGlobali.gioCalcolata == false && indexPath.section == 2)) {
//}
else {
NSDictionary *riepilogoVal;
NSDictionary *riepilogoTip;
riepilogoVal = [logForValore objectAtIndex:indexPath.row];
riepilogoTip = [logForTipo objectAtIndex:indexPath.row];
foaRiepilogoCell *cell = (foaRiepilogoCell *)[tableView dequeueReusableCellWithIdentifier:idRiepilogo];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idRiepilogo owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.voceRiepilogoLabel.text = riepilogoTip;
cell.valoreRiepilogoLabel.text = riepilogoVal;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *header;
if (variabiliGlobali.gioCalcolata && section == 2) {
header = #"Bonus e Totale";
}
if ((variabiliGlobali.gioCalcolata && section == 3) || (variabiliGlobali.gioCalcolata == false && section == 2)) {
header = #"Riepilogo";
}
if (section == 1)
{header = #"Panchinari";}
if ( section == 0)
{header = #"Titolari";}
return header;
}
#end
If i use another simple view (with only one random label) the plugin works fine.
What is the matter?
Thanks in advance.
I have resolved my question declaring these variable
NSDictionary *formazione;
NSDictionary *elencoCalciatori;
NSDictionary *logFormazione;
NSDictionary *logCalcolo;
NSMutableArray *titolari;
NSMutableArray *panchinari;
NSMutableArray *logForTipo;
NSMutableArray *logForValore;
NSMutableArray *logCalTipo;
NSMutableArray *logCalValore;
bool calcolata;
in .h files of ViewController.
Rows of Level 1 should expand and collapse. Suppose if Row 0 is open - that means its status is 1 - and next row's status is 0, if I click on next row whose status is 0, then the row whose status is 1 should get closed.
How can I do that?
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
dictBtnStatus = [[NSMutableDictionary alloc] init];
arraylist = [[NSMutableArray alloc] init];
array5 = [[NSMutableArray alloc] init];
array1 = [[NSMutableArray alloc] init];
objects = #[#"Medical Services In Chicago", #"Medical Services On Campus", #"ABC", #"California"];
// objects = #[#"Medical Services In Chicago", #"Medical Services On Campus"];
arrObjectValueChicago = #[#"Ronald McDonald® Children's Hospital of Loyola", #"Burn Centers", #"Gottlieb Hospitals"];
arrObjectValueCampus = #[#"Cardinal Bernardin Cancer Center1", #"Center for Heart & Vascular Medicine2", #"ABC"];
for (int i = 0; i < [arrObjectValueCampus count]; i++) {
dictListCampus1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"2", #"level",[arrObjectValueCampus objectAtIndex:i], #"name", nil];
[array5 addObject:dictListCampus1];
}
NSDictionary *dictListCampus = [[NSDictionary alloc] initWithObjectsAndKeys:#"Wellness Centers", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
array3 = [[NSMutableArray alloc] init ];
for (int i = 0; i < [arrObjectValueChicago count]; i++){
dictList3 = [[NSDictionary alloc]initWithObjectsAndKeys:#"2", #"level",[arrObjectValueChicago objectAtIndex:i], #"name", nil];
[array3 addObject:dictList3];
}
NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Hospitals", #"name", #"1", #"level", array3, #"Objects", nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
for (int i = 0; i < [objects count]; i++) {
if (i == 0) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 1) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 2) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
}
}
dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, #"Objects", nil];
arrayOriginal = [dictList valueForKey:#"Objects"];
[arraylist addObjectsFromArray:arrayOriginal];
}
#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arraylist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// static NSString *CellIdentifier = #"CustomCellIdentifier";
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (IndentLevel == 0) {
CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellHeader" owner:self options:nil];
cell = self.headercell;
self.headercell = nil;
}
cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
NSLog(#"indexFor level 0 ::%# %d",cell.lblHeader.text,indexPath.row);
return cell;
} else if (IndentLevel == 1) {
CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!cell) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellSubHeader" owner:self options:nil];
cell = self.subheadercell;
self.subheadercell = nil;
NSLog(#"dicbtn %#", dictBtnStatus);
NSString *strName = [[arraylist objectAtIndex:indexPath.row]valueForKey:#"name"];
NSString *str = [dictBtnStatus objectForKey:strName];
NSLog(#"indexFor level 1 ::%# %d", strName, indexPath.row);
if ([str isEqualToString:#"1"]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
}
cell.lblSubHeader.text = strName;
cell.imgShadow.hidden = YES;
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
}
return cell;
} else if (IndentLevel == 2) {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.cells;
self.cells = nil;
}
cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell.btnCall addTarget:self action:#selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tblist deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
CustomCellSubHeader *cell = ((CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath]);
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
}
NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
if ([d valueForKey:#"Objects"]) {
NSArray *ar = [d valueForKey:#"Objects"];
BOOL isAlreadyInserted = NO;
for (NSDictionary *dInner in ar) {
NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
isAlreadyInserted = (index > 0 && index != NSIntegerMax);
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
} else if (IndentLevel == 0) {
NSString *strStatus = [dictBtnStatus objectForKey:strName];
if ([strStatus isEqualToString:#"1"]) {
// NSDictionary *dict = [arraylist objectAtIndex:previousRow];
// if ([dict valueForKey:#"Objects"]) {
NSArray * array = [[arraylist objectAtIndex:previousRow]valueForKey:#"Objects"];
[self miniMizeThisRows:array];
}
}
if (isAlreadyInserted) break;
}
if (isAlreadyInserted) {
if ([arraylist count] - 1 && IndentLevel == 1) {
cell.imgShadow.hidden = NO;
}
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
} else if (IndentLevel == 0) {
[dictBtnStatus setValue:#"0" forKey:strName];
}
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row + 1;
NSMutableArray *arCells = [NSMutableArray array];
for (NSDictionary *dInner in ar) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
previousRow = [indexPath row];
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"1" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
} else if (IndentLevel == 0) {
[dictBtnStatus setValue:#"1" forKey:strName];
}
[arraylist insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (indentLevel == 0) {
return 40;
} else if (indentLevel == 1) {
return 25;
} else if (indentLevel == 2) {
CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"]);
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Arial" size:11.5];
CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
if (myStringSize.height > 80) {
myStringSize.height = 50;
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
cell.txtAddress.scrollEnabled = YES;
} else {
cell.txtAddress.scrollEnabled = YES;
myStringSize.height = 40;
}
return myStringSize.height+ 50;
} else return 25;
}
#pragma mark - TableAnimation
- (void)miniMizeThisRows:(NSArray *)ar {
for(NSDictionary *dInner in ar) {
NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
NSArray *arInner = [dInner valueForKey:#"Objects"];
if (arInner && [arInner count] > 0) {
[self miniMizeThisRows:arInner];
}
if ([arraylist indexOfObjectIdenticalTo:dInner]!= NSNotFound) {
[arraylist removeObjectIdenticalTo:dInner];
[tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
dictBtnStatus = [[NSMutableDictionary alloc] init];
arraylist = [[NSMutableArray alloc] init];
array5 = [[NSMutableArray alloc] init];
array1 = [[NSMutableArray alloc] init];
objects = #[#"Medical Services In Chicago", #"Medical Services On Campus", #"ABC", #"California"];
// objects = #[#"Medical Services In Chicago", #"Medical Services On Campus"];
arrObjectValueChicago = #[#"Ronald McDonald® Children's Hospital of Loyola", #"Burn Centers", #"Gottlieb Hospitals"];
arrObjectValueCampus = #[#"Cardinal Bernardin Cancer Center1", #"Center for Heart & Vascular Medicine2", #"ABC"];
for (int i = 0; i < [arrObjectValueCampus count]; i++) {
dictListCampus1 = [[NSDictionary alloc]initWithObjectsAndKeys:#"2", #"level",[arrObjectValueCampus objectAtIndex:i], #"name", nil];
[array5 addObject:dictListCampus1];
}
NSDictionary *dictListCampus = [[NSDictionary alloc]initWithObjectsAndKeys:#"Wellness Centers", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
NSDictionary *dictListCampus2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Wellness Centers123", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array61 = [[NSMutableArray alloc] initWithObjects:dictListCampus2, nil];
array3 = [[NSMutableArray alloc] init];
for (int i = 0; i < [arrObjectValueChicago count]; i++) {
dictList3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"2", #"level",[arrObjectValueChicago objectAtIndex:i], #"name", nil];
[array3 addObject:dictList3];
}
NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Hospitals", #"name", #"1", #"level", array3, #"Objects", nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
for (int i = 0; i < [objects count]; i++) {
if (i == 0) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 1) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 2) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], #"name", #"0", #"level", array61, #"Objects", nil];
[array1 addObject:dictionary];
}
// else {
// dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], #"name", #"0", #"level", array2, #"Objects", nil];
// [array1 addObject:dictionary];
//}
}
dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, #"Objects", nil];
NSLog(#"DictList :: %#", dictList);
arrayOriginal = [dictList valueForKey:#"Objects"];
NSLog(#"Array Original :: %#", arrayOriginal);
[arraylist addObjectsFromArray:arrayOriginal];
//[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
}
#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arraylist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//static NSString *CellIdentifier = #"CustomCellIdentifier";
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (IndentLevel == 0) {
CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellHeader" owner:self options:nil];
cell = self.headercell;
self.headercell = nil;
}
cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
NSLog(#"indexFor level 0 ::%# %d",cell.lblHeader.text, indexPath.row);
return cell;
} else if (IndentLevel == 1) {
CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!cell) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellSubHeader" owner:self options:nil];
cell = self.subheadercell;
self.subheadercell = nil;
NSLog(#"dicbtn %#", dictBtnStatus);
}
NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
NSString *str = [dictBtnStatus objectForKey:strName];
NSLog(#"indexFor level 1 ::%# %d", strName, indexPath.row);
if ([str isEqualToString:#"1"]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
}
// else {
// [cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
//}
cell.lblSubHeader.text = strName;
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
if (indexPath.row == arraylist.count - 1) {
cell.imgShadow.hidden = NO;
}
return cell;
} else if (IndentLevel == 2) {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.cells;
self.cells = nil;
}
cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell.btnCall addTarget:self action:#selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCellSubHeader *cell;
if ([[tblist cellForRowAtIndexPath:indexPath] isKindOfClass:[CustomCellSubHeader class]]) {
cell = (CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath];
UIImage *img = [cell.btnarrow imageForState:UIControlStateNormal];
if ([img isEqual:[UIImage imageNamed:#"dwn1_arow.png"]]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[dictBtnStatus setValue:#"0" forKey:cell.lblSubHeader.text];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
cell.imgShadow.hidden = NO;
} else {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
dictBtnStatus = [[NSMutableDictionary alloc] init];
[dictBtnStatus setValue:#"1" forKey:cell.lblSubHeader.text];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
cell.imgShadow.hidden = YES;
}
}
NSMutableArray *arrIndex = [[NSMutableArray alloc] init];
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (IndentLevel == 0) {
arrLast = [[NSMutableArray alloc] init];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
if ([d valueForKey:#"Objects"]) {
NSArray *ar = [d valueForKey:#"Objects"];
BOOL isAlreadyInserted = NO;
for (NSDictionary *dInner in ar) {
NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
isAlreadyInserted = (index > 0 && index != NSIntegerMax);
if (isAlreadyInserted) break;
}
if (isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count = indexPath.row + 1;
NSMutableArray *arCells = [NSMutableArray array];
for (NSDictionary *dInner in ar) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[arrLast addObject:dInner];
[arraylist insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationBottom];
}
}
//NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (IndentLevel == 0) {
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
for (int i = 0 ; i < arraylist.count; i++) {
NSDictionary *dic = [arraylist objectAtIndex:i];
if ([[dic valueForKey:#"level"] intValue] != 0) {
NSInteger index = [arrLast indexOfObjectIdenticalTo:dic];
if ((index >= 0 && index != NSIntegerMax)) {
} else {
[discardedItems addIndex:i];
[arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
}
if (discardedItems.count > 0) {
[arraylist removeObjectsAtIndexes:discardedItems];
[tblist deleteRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (indentLevel == 0) {
return 40;
} else if (indentLevel == 1) {
return 25;
} else if (indentLevel == 2) {
CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"]);
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Arial" size:11.5];
CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
if (myStringSize.height > 80) {
myStringSize.height = 50;
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
cell.txtAddress.scrollEnabled = YES;
} else {
cell.txtAddress.scrollEnabled = YES;
myStringSize.height = 40;
}
return myStringSize.height + 50;
} else return 25;
}
#pragma mark - TableAnimation
- (void)miniMizeThisRows:(NSArray *)ar {
for (NSDictionary *dInner in ar) {
NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:#"Objects"];
if (arInner && [arInner count] > 0) {
[self miniMizeThisRows:arInner];
}
if ([arraylist indexOfObjectIdenticalTo:dInner] != NSNotFound) {
[arraylist removeObjectIdenticalTo:dInner];
[tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
#pragma mark - Buttons
- (IBAction)btnCall:(id)sender {
UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want to call on this number." delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
Notpermitted.delegate = self;
[Notpermitted tag];
[Notpermitted show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *strCall = [NSString stringWithFormat:#"tel://999-999-9999"];
NSURL *url = [NSURL URLWithString:strCall];
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:#"iPhone"] ) {
[[UIApplication sharedApplication] openURL:url];
} else {
UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Your device doesn't support this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[Notpermitted show];
}
}
}
see following tutorial
http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
www.alexandre-gomes.com/?p=482
Expand/collapse section in UITableView in iOS
I need to filter data in UITableview by text entered in UISearchbar. I followed this example but there data in NSMutableArray and I can't alter it under my requirements. My data is NSMutableDictionary. I'm stuck on this for long time.
My data:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:#"data.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];
sectionKeys = [NSMutableArray new];
sectionsTitle = [NSMutableArray new];
NSArray *tableArray = [myDict objectForKey:#"Black"];
[resultArray addObject:#"Black"];
[resultDic setValue:tableArray forKey:#"Black"];
[sectionsTitle addObject:[NSString stringWithFormat:#"%#", [tableData valueForKey:#"Black"]]];
[sectionCord addObject:[NSString stringWithFormat:#"%#", [tableData valueForKey:#"Coordinates"]]];
[sectionKeys addObject:#"Section1"];
self.tableData = resultDic;
self.sectionsTitle = resultArray;
[myTable reloadData];
My table:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionKeys.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionKeys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int rowCount;
if(self.isFiltered)
rowCount = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:section]] count];
else
rowCount = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if(isFiltered){
NSDictionary *dict = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [dict objectForKey:#"Name"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [dict objectForKey:#"Address"]];
}
else{
NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [dict objectForKey:#"Name"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [dict objectForKey:#"Address"]];
}
return cell;
}
My search:
#pragma mark - SearchBar Delegate -
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text{
if(text.length == 0)
{
isFiltered = FALSE;
NSLog(#"false");
}
else
{
isFiltered = true;
NSLog(#"true");
filteredTableData = [[NSMutableDictionary alloc] init];
for (MyAnnotation* ann in tableData)
{
NSRange nameRange = [ann.title rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange descriptionRange = [ann.description rangeOfString:text options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
[filteredTableData addObject:ann];
//error
}
}
}
[myTable reloadData];
}
First of all, you need to create a state/flag for the controller/data source, in order for it to know weather you are in search/filter mode.
Then, if you are in search mode, point the data source methods to the filteredArray.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int numberOfSections = 0;
if (_searchMode)
{
numberOfSections = self.filteredDataDict.allKeys.count;
}
else
{
numberOfSections = self.tableData.allKeys.count;
}
return numberOfSections;
}
Hope it's understood.