how to load the items in array at run time - iphone

I have three tableView one containg cateogry one containg type and other product name i want that when user selct any category the type array should be loaded at run time getting the same type of the selected category
In my example if i select category Herbicide then it should insert the contents of the
typeHerbicides in arrayType but when i load table second it does not show any values
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView ==tblSimpleTable )
return [categoryArray count];
else if(tableView==tblSimpleTableType)
return [typeArray count];
else if(tableView==tblSimpleTableProduct)
return [productArray count];
else if(tableView==tblSimpleTableOneSection)
return [categorySectionOneArray count];
else if(tableView==tblSimpleTableTypeSection)
return [typeArraySection count];
else
return [productArraySection count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (tableView ==tblSimpleTable ) {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [categoryArray objectAtIndex:indexPath.row];
return cell;
}
else if (tableView==tblSimpleTableType){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [typeArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableProduct) {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [productArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableOneSection){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [categorySectionOneArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableTypeSection){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [typeArraySection objectAtIndex:indexPath.row];
return cell;
}
else {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [productArraySection objectAtIndex:indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView ==tblSimpleTable ) {
titleCategory=[categoryArray objectAtIndex:indexPath.row];
NSLog(#"This is titleCategory Selected %#",titleCategory);
NSString*test=#"Herbicides";
if([titleCategory isEqualToString:test]){
typeHerbicides=[[NSArray alloc] initWithObjects:#"ACCLAIM EXTRA",#"ILLOXAN",#"REVOLVER",nil];
typeArray=[[NSArray alloc] initWithArray:typeHerbicides copyItems:YES];
}
else {
typeArray=[[NSArray alloc] initWithObjects:#"ILLOXAN",#"REVOLVER",nil];
typeArray=[[NSArray alloc] initWithArray:typeHerbicides copyItems:YES];
}
}
else if(tableView==tblSimpleTableType)
titleType=[typeArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableProduct)
titleProduct=[productArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableOneSection)
titleSectionOneCategory=[categorySectionOneArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableTypeSection)
titleTypeSection=[typeArraySection objectAtIndex:indexPath.row];
else
titleProductSection=[productArraySection objectAtIndex:indexPath.row];
}

When user selects any category then in didSelectRowAtIndexPath you can initialize your typeArray based on category selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == tblSimpleTableType)
{
//initialize typeArray;
//Once typeArray is initialized just reload the tableview as follows:
[tblSimpleTableType reloadData];
}
}

Just try by returning the cell at last of the function only and see.

Related

assigning datasource to two tableviews in one view controller

Whats wrong in the following code?
here i am using two table views and assigning them with different data source, but its not working
-(void)viewDidLoad
{
[super viewDidLoad];
arryData = [[NSArray alloc] initWithObjects:#"MCA",#"MBA",#"BTech",#"MTech",nil];
arryData2 = [[NSArray alloc] initWithObjects:#"Objective C",#"C++",#"C#",#".net",nil];
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
btnOne.layer.cornerRadius=8;
btnTwo.layer.cornerRadius=8;
myTable1.layer.cornerRadius=8;
myTable2.layer.cornerRadius=8;
myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1=tableView)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==self.myTable1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
return cell;
}
else
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}
}
-(IBAction)btnCategory:(id)sender
{
if (flag==1) {
flag=0;
myTable1.hidden=NO;
myTable2.hidden=YES;
}
else{
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myTable1.hidden=YES;
myTable2.hidden=YES;
}
-(IBAction)btnTopic:(id)sender
{
if (flag==1) {
flag=0;
myTable2.hidden=NO;
myTable1.hidden=YES;
}
else{
flag=1;
myTable2.hidden=YES;
myTable1.hidden=YES;
}
}
</code>
when i remove datasource of mytable2 all data gets added to table1 else nothing gets loaded in none of the table.
You did mistake. See my code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1==tableView) // ----- Change here you need to write == symbol instead of = symbol
{
return [arryData count];
} else {
return [arryData2 count];
}
}
Try this,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==myTable1)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if(myTable1==tableView)
{
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
}
else
{
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
}
return cell;
}

TableView isn't updating data even though it recognizes input?

So I have 5 rows and on selection they pass an integer with the row number to my second view controller.
Each number has its own array with items and should then return the amount of items for the row specified.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (radInt == 0) {
cell.textLabel.text = #"LM";
NSLog(#"My return should have been LM");
}
if (radInt == 1) {
cell.textLabel.text = #"Restauranger";
NSLog(#"My return should have been Rest");
}
if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:#"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
This is my code just to test it out and it doesn't work. NSLOG works correctly but the data simply wount update... What have I done wrong? It Nslogs LM every time but it also has 1 in the log which is the selected row (radInt).
New approach
static NSString *CellIdentifier = #"MasterCell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell = [[MasterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (radInt == 0) {
cell.textLabel.text = #"LM";
NSLog(#"My return should have been LM");
}
else if (radInt == 1) {
cell.textLabel.text = #"Restauranger";
NSLog(#"My return should have been Rest");
}
else if (radInt == 2) {
cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
}
else if (radInt == 3) {
cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = #"Noes";
}
//cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:#"tab-icon1.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
The view before ----
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *enjoy = [[SecondViewController alloc] init];
if ([[array objectAtIndex:indexPath.row] isEqual:#"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 0;
NSLog(#"0");
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 1;
NSLog(#"1");
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:indexPath.row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:indexPath.row] isEqual:#"Annat"]) {
enjoy.radInt = 3;
}
[self performSegueWithIdentifier:#"main" sender:self];
}
You don't actually need to implement didSelectRowAtIndexPath: if you connect the segue in the storyboard from the cell to the next controller. What you need to implement is prepareForSegue:. Your code should look something like this:
- (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender
{
NSInteger row = [self.tableView indexPathForSelectedRow].row;
SecondViewController *enjoy = segue.destinationViewController;
if ([[array objectAtIndex:row] isEqual:#"Livsmedel"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 0;
NSLog(#"0");
}
if ([[array objectAtIndex:row] isEqual:#"Restauranger"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 1;
NSLog(#"1");
}
if ([[array objectAtIndex:row] isEqual:#"Shopping"]) {
[enjoy setTitle:[array objectAtIndex:row]];
enjoy.radInt = 2;
}
if ([[array objectAtIndex:row] isEqual:#"Annat"]) {
enjoy.radInt = 3;
}
}

showing data in table view sections from different NSMutableArrays

I am fetching data form JSON and storing that data in 10 differnet NSMutable Arrays .I want to show that data in 10 sections of the tableview so how to do this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//return categories.count ;
if(section == 0)
{
return [appDelegate.books1 count];
}
else if(section == 1)
{
return [appDelegate.books2 count];
}
else if(section == 2)
{
return [appDelegate.books3 count];
}
else if(section == 3)
{
return [appDelegate.books4 count];
}
else if(section == 4)
{
return [appDelegate.books5 count];
}
else if(section == 5)
{
return [appDelegate.books6 count];
}
else if(section == 6)
{
return [appDelegate.books7 count];
}
else if(section == 7)
{
return [appDelegate.books8 count];
}
else if(section == 8)
{
return [appDelegate.books9 count];
}
else if(section == 9)
{
return [appDelegate.books10 count];
}
}
if i am doing below it shows only first section
Book1*aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;
In my view (if you don't want to change much in your code) do it like this
make an another NSMutableArray object lets say allData
add all your array into allData according to section, somthing like this
[allDatad addObjects:appDelegate.books1,appDelegate.books2,appDelegate.books3...,appDelegate.books10,nil];
now in cellForRowAtIndexPath just change this
Book1* aBook=[[allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[allData objectAtIndex:indexPath.section] count];
and in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[allData objectAtIndex:indexPath.section] count];
}
Use indexPath.section to determine for which section you are preparing your cell.
For e.g., if indexPath.section is 3 then use appDelegate.books4.
Hope this helps.
A better more scalable implementation would be to add these arrays to another array, say arrayOfArrays. Now your number of sections are
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [arrayOfArrays count];
}
number of rows are:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[arrayOfArrays objectAtIndex:section] count];
}
and your cellForRowAtIndexPath is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//all the dequeueing and initialization and stuff
Book1 *aBook=[[arrayOfArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[arrayOfArrays objectAtIndex:indexPath.section] count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
//do other stuff
return cell;
}
You should store all those arrays in NSMutableDictionary like,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:appDelegate.books1 forKey:#"1"];
[dict setObject:appDelegate.books2 forKey:#"2"];
[dict setObject:appDelegate.books3 forKey:#"3"];
and access with the reference of indexPath.section while creating cell like,
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", indexPath.section + 1]];
and at time when you want to define numberOfRowsInSection you can get count from the NSMutableDictionary.
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", section + 1]];
[aBook count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
Book1*aBook;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier] autorelease];
}
if(indexPath.section == 0)
{
aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1)
{
aBook=[appDelegate.books2 objectAtIndex:indexPath.row];
}
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;
}

UITableView crashes on scrolling

UITableView is crashing on scrolling . Here is the sample code..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Total Count: %d",count);
return [ObjectArray count] ;
}
- (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] autorelease];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:12.0];
cell.detailTextLabel.font = [UIFont fontWithName:#"Arial" size:10.0];
[cell.textLabel sizeToFit];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
// Configure the cell.
NSLog(#"Row Number %d", indexPath.row);
cell.textLabel.text = [ObjectArray name];
return cell;
}
In console I can see Total count = 22.
Row Number 0
Row NUmber 1
.
.
Row Number 21 (This row number will pull the last object from ObjectArray)
Now if I try to scroll it crashes...why? Can anyone help me with this ...
I don't really understand this line of code
cell.textLabel.text = [ObjectArray name];
If ObjectArray is a NSArray instance, then it would not respond to that selector. And anyway you probably want to return smth like
cell.textLabel.text = [ObjectArray objectAtIndex: [indexPath row]];

iphone table view check mark accessory problem

I have a table with 50 rows. I want to select particular rows with checkmark accessory but when i select some rows and scroll down the table then i see pre checked rows. I know that table cell are reused but i want to emit this problem what can i do about this?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// return [array count];
return 50;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.
You have to store the state of each cell (ie in an array) to recreate the cell with the right value.
Something like that:
BOOL values[50]; //Not the best way, but easy for the example...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
values[indexPath.row] = !value[indexPath.row];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can do it with the help of array ,take one array and each array hold a dict do this condition in array
NSMutableArray *arr = [[NSMutableArray alloc]init];
for(int i=1; i<=50;i++)
{
NSmutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setobject:[nsstring stringwithformat:#"%d",i] valueForkey:#"ID"];
[arr addobject:dict];
[dict release];
}
now rows is [arr count];
and put this check in cell appearance
NSMutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"]==nil])
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}else
{
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
now put below code in didselect methood
nsmutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
[dict setobject:[nsstring stringwithformate:#"2"] valueforkey:#"Check"];
}
else
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}