How UITableView show two different arrays one at a time? - iphone

The below Code works but not as i wish.i want that when i click UIbutton its automaically update the new value in UITableview instead of old value.Below Code works only when i press the UIbuttons and after that when i scroll the UITableview then it update the UItableview with new values.
In my application i using UITableview as Subclass of my mainclass.as image show below
I add Tableview in my Mainclass which is "testingViewController" like this way
In testingViewController.h
#import "Inputtableview.h"
#interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
Inputtableview *inputview;
IBOutlet UITableView *inputtbl;
}
#end
In testingViewController.m
- (void)viewDidLoad {
btn1bool=FALSE;
if (inputview == nil) {
inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
}
Now in Button action method
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
}
my Subclass code "inputtableview.m" is show below
- (void)viewDidLoad {
[super viewDidLoad];
listOfItems=[[NSMutableArray alloc] initWithObjects:#"Iceland",#"Greenland",#"Switzerland",
#"Norway",#"New Zealand",#"Greece",#"Italy",#"Ireland",nil];
array1 = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H", nil] ;
}
#pragma mark -
#pragma mark Table View datasource methods
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
}
else {
return [listOfItems count];
}
[self.tableView reloadData];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"Row: %i", indexPath.row);
if (btn1bool) {
NSString *cellValue = [array1 objectAtIndex:indexPath.row];
cell.text = cellValue;
}
else {
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
}
return cell;
}
Any help will be appriated.

Just put the following code:
[inputtbl reloadData];
There are a few things you need to change in your project, but I assume this project is just for testing stuff.
You want the date to reload after you pressed the button, so you call the method in the IBAction.
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
[inputview.tableView reloadData];
}
To switch between the 2 data sources when the button is pressed you can change to this line of code: btn1bool=!btn1bool;
(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
} else {
return [listOfItems count];
}
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is correct

Related

UITableViewCell keeps getting deselected

I have a strange problem with UITableView:
My tableview shows the names of objects from which the user can select one. The value is saved. When the tableview comes up the row is selected (in cellForRowAtIndexPath). But in viewWillAppear the row is first selected and then deselected. You can see that it is first selected because of a the blue background occurs for a short time.
I have set
self.clearsSelectionOnViewWillAppear = NO;
but that does not work ether. Can someone help me out please! Thanks in advance.
Here some sample code:
#import "ECTESTTableViewController.h"
#interface ECTESTTableViewController ()
#end
#implementation ECTESTTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
#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 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.backgroundColor = [UIColor redColor];
}
if (indexPath.row == 1)
{
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(#"select %d", indexPath.row);
cell.selected = TRUE;
}
else {
// cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = FALSE;
}
cell.textLabel.text = [NSString stringWithFormat:#"row %d", indexPath.row];
return cell;
}
#end
If you want to programatically select a row in a UITableView use:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Don't be setting the selected states of the cells.
Write following code
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];

display data in UITableView on view navigation

i have 2 views-view1 and view2, one for entering data and another for displaying the entered data.
I am able to display the entered data in view2 in labels. But how to display the data in a UITableView. Following is my code to display the data:
view2.m
#synthesize details; //details is an object of NSMutableArray.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
textLabel.text = self.name;
lblCity.text = self.city;
lblGender.text = self.gender;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [details count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [details objectAtIndex:indexPath.row];
//cell.imageView.image = [UIImage imageNamed:#"creme_brelee.jpg"];
return cell;
i debuuged and found that cellForRowAtIndexPath is never called.
Where am i getting wrong? How do i solve it?
You need to create a NSMutableArray of display Data in to UITableViewCell like below and don't forget to declare UITableView delegates in .h file <UITableViewDataSource,UITableViewDelegate> and also connect your UITableView IBOutlet in your xib and also connect the delegate:
- (void)viewDidLoad
{
[super viewDidLoad];
arrCategorisation=[[NSMutableArray alloc] initWithObjects:#"DataOne",#"DataTwo",#"DataThree", #"DataFour",#"DataFive",nil];
}
#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 arrCategorisation.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text=[arrCategorisation objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
your Table load data like:-

problem while getting selected value from tableview

i have 3 different arrays. 1)for ids 2)for names 3)for emailid.
i am displaying names in a tableview.
And displaying selected name with checkmark in tableview.
Now i need to get selected name id and email from the ids and emailid arrays.
And that retrieved id and emails are need to save in two different arrays.
for that my code is
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.sourceArray 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell.textLabel setText:[self.sourceArray objectAtIndex:indexPath.row]];
if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){
NSLog(#"111111111111");
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
if ([self.selected_agentid_email containsObject:[agentids objectAtIndex:indexPath.row]]){
NSLog(#"222222222222");
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
if(myBoolean){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){
NSLog(#"33333333333333");
[self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:[agentemails objectAtIndex:indexPath.row]]];
}
else
{
[self.selectedArray addObject:[agentemails objectAtIndex:indexPath.row]];
}
if ([self.selected_agentid_email containsObject:[agentids objectAtIndex:indexPath.row]]){
NSLog(#"4444444444444444");
[self.selected_agentid_email removeObjectAtIndex:[self.selected_agentid_email indexOfObject:[agentids objectAtIndex:indexPath.row]]];
}
else
{
[self.selected_agentid_email addObject:[agentids objectAtIndex:indexPath.row]];
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
but the problem i am getting is,
eg: if i select 3 names in a tableview i am able to retrieve only one emailid and 3 ids.
i did n't get what's problem is.
can any one please help me.
Thank u in advance.
This may not be the solution you were looking for but I would recommend creating a class to store those values so you only have to work with an array of all values and an array of selected values. This will greatly decrease the complexity of working with several arrays. I have not actually tested the following code but this will be sort of what it would like.
//Agent.h
#interface Agent : NSObject
{
NSString *_name;
NSString *_aID;
NSString *_email;
}
#property(retain) NSString *name;
#property(retain) NSString *aID;
#property(retain) NSString *email;
#end
//Agent.m
#implementation Agent
#synthesize name = _name;
#synthesize aID = _aID;
#synthesize email = _email;
- (void) dealloc
{
[_name release];
[_aID release];
[_email release];
[super dealloc];
}
#end
//Your selection code will then look like this
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Agent *agent = (Agent*)[self.allAgents objectAtIndex:indexPath.row];
if (UITableViewCellAccessoryCheckmark == cell.accessoryType) {
[self.selectedAgents removeObject:agent];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else {
[self.selectedAgents addObject:agent];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
//All 3 values are guaranteed to be there
NSLog(#"Agent: %# %# %#", agent.name, agent.aID, agent.email);
}

select multiple rows from uitableview and delete

i am displaying a list of items in a tableview.i need to select and delete multiple rows from the table at a time,any resources on how to do this
I'm assuming your table has just one section. You can extend this solution to multiple sections fairly easily.
Add an NSMutableSet member "selectedRows" to your UIViewController subclass that manages your TableView
in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath toggle the indexPath.row's membership in "selectedRows", like this:
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowNsNum] )
[self.selectedRows removeObject:rowNsNum];
else
[self.selectedRows addObject:rowNsNum];
indicate visually that a row is selected (e.g., set the cell's accessoryType property to UITableViewCellAccessoryCheckmark), or modify your cell visually in some other way to indicate that it is a selected row
add a "delete" button to your UI, either in a table section header/footer, your title bar, anywhere, hooked up to a selector called "deleteRows"
in your deleteRows method, iterate through the selectedRows set, building up an array of indexPaths, delete these rows from your data model, then call (with your preferred animation type):
[self.myTableView deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
EDIT:
Here's my full didSelectRowAtIndexPath method. The deselectRowAtIndexPath may be required for correct operation.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( self.editing )
return;
[self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowNsNum] )
[self.selectedRows removeObject:rowNsNum];
else
[self.selectedRows addObject:rowNsNum];
[self.myTableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.2];
}
#import "ViewController.h"
#import "TableViewCell.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tblView;
#property (strong,nonatomic)NSMutableArray *arryData1,*arryData2;
#property (strong,nonatomic)UIBarButtonItem *edit,*delete;
#end
#implementation ViewController
{
BOOL Selected;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.arryData1 = [[NSMutableArray alloc] initWithObjects:#"MCA",#"MBA",#"BTech",#"MTech",nil];
self.arryData2 = [[NSMutableArray alloc] initWithObjects:#"Objective C",#"C++",#"C#",#".net",nil];
self.edit=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(edit:)];
self.navigationItem.leftBarButtonItem=self.edit;
self.delete=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(delete:)];
self.navigationItem.rightBarButtonItem=self.delete;
self.tblView.allowsMultipleSelection=YES;
}
-(void)edit:(id)sender
{
Selected=YES;
[self.tblView reloadData];
}
-(void)delete:(id)sender
{
NSArray *selectedCells = [self.tblView indexPathsForSelectedRows];
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in selectedCells) {
[indicesToDelete addIndex:indexPath.row];
}
//arrayFromPlist is NSMutableArray
[self.arryData1 removeObjectsAtIndexes:indicesToDelete];
[self.tblView beginUpdates];
[self.tblView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblView endUpdates];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove the row from data model
[self.arryData1 removeObjectAtIndex:indexPath.row];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arryData1 count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= #"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
if (Selected==YES) {
cell.imageView.image=[UIImage imageNamed:#"trash.png"];
}
else
{
cell.imageView.image=nil;
}
cell.textLabel.text = [self.arryData1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.arryData2 objectAtIndex:indexPath.row];
return cell;
}
#end

UITableViewCell reloadcellsAtIndexPaths increases the height of the following cell but not the actual one

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
after this I am increasing the height of the 1st cell in heightforRowAtIndexPath. But to my surprise it is increasing the height of the second cell, but when I scroll the table view... the height of the first one is set back to desired.
If I reload the entire table using [tv reloaddata] it works fine.. does not work only if i do the particular cell
Any help on this would be helpful..
As written in the comments above, I only have a vague understanding about what you want to achieve. I assume you want to extend cell by changing their height. You just mention the first cell, but I guess, you want to be able, to do it for any cell when being clicked.
The complete project is available at github
.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
NSIndexPath *selectedIndexPath;
NSDictionary *articles;
}
#end
.m
#import "FirstViewController.h"
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
selectedIndexPath = nil;
articles = [[NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:#"one", #"two", #"three",
#"four", #"five", #"six",
#"seven", #"eight", #"nine",
#"ten", #"eleven", nil]
forKey:#"title"] retain];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[selectedIndexPath release];
[articles release];
[super dealloc];
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[articles allKeys] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[articles allKeys] objectAtIndex : section];
}
- (int)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
id key = [[articles allKeys] objectAtIndex:section];
return [[articles objectForKey : key] count];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row))
return 80.0;
return 40.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * MyIdentifier = #"MyIdentifier";
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
id key = [[articles allKeys] objectAtIndex:indexPath.section];
cell.textLabel.text = [[articles objectForKey:key] objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndexPath == indexPath) {
selectedIndexPath = nil;
} else {
selectedIndexPath = indexPath;
}
[self.tableView deselectRowAtIndexPath : indexPath animated : NO];
[tableView beginUpdates];
[tableView endUpdates];
}
#end
This code is quite rough, as I just extracted it from a project in my very early iPhone-days. i.e. properties are missing.