I have table view with rows...after I selecting one item I want to show tableView with a new data source.
I tried to implement:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:YES animated:YES];
Game *game = [self.filteredListContent objectAtIndex:indexPath.row];
//name your class names with capitals
NSMutableArray *arrayToBeAdded= [[NSMutableArray alloc]initWithArray:newgamearray];
[ListContent removeAllObjects];
[ListContent addObject:arrayToBeAdded];
but I getting exeption in the
[ListContent removeAllObjects];
Implementation of the init:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = #"cellSgames";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Gmage *game = [self.ListContent objectAtIndex:indexPath.row];
In the .h file i declare:
NSArray AllList;
NSMutableArray ListContent;
any ideas?
Instead of manipulating the current list, create new UITableViewController with your desired data source. If you are using the UINavigationController it will create a better user experience and you will avoid your current problem.
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InnerTableViewController *innerTVC = [[InnerTableViewController alloc] initWithNibName:#"InnerTableViewController" bundle:nil];
[self.navigationController pushViewController:innerTVC animated:YES];
[innerTVC release];
}
Normally if you are resetting the array in didselect method and reloading your table view with new content it should work.
In your case i think at line:
[ListContent removeAllObjects];
ListContent is not a valid pointer. I don't see a reason to crash it here.
Related
I'm trying to make an upcoming events table in my app but when I select a row indexPathForSelectedRow always returns 0.
NSArray *upcommingTitle;
NSMutableArray *upcommingSubtitle;
NSMutableArray *upcommingTime;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)datesTable numberOfRowsInSection:(NSInteger)section
{
return upcommingTitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)datesTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
DatesCustomCell *Cell = [datesTable dequeueReusableCellWithIdentifier:cellIdentifier];
if(!Cell)
{
Cell = [[DatesCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Cell.DatesTitle.text = [upcommingTitle objectAtIndex:indexPath.row];
Cell.DatesSubtitle.text = [upcommingSubtitle objectAtIndex:indexPath.row];
Cell.DatesTime.text = [upcommingTime objectAtIndex:indexPath.row];
return Cell;
}
self.datesTable.dataSource = self;
self.datesTable.delegate = self;
View Did Load
upcommingTitle = [[NSMutableArray alloc] initWithObjects:#"Title1", #"Title2", nil];
upcommingSubtitle = [[NSMutableArray alloc] initWithObjects:#"Sub1", #"Sub2", nil];
upcommingTime = [[NSMutableArray alloc] initWithObjects:#"Date1", #"Date2", nil];
But the following always returns 0 resulting in the "test" label to be "Title1"
View Did Appear
_test.text = [upcommingTitle objectAtIndex:self.datesTable.indexPathForSelectedRow.row];
Thanks for you help
try to push your another view in your didSelect method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, indexpath returns the selection of cell's indexpath, so put your code here
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
//then push your view
}
UPDATED : then use this code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:#"pushView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PushingViewController *pushView = [segue destinationViewController];
pushView._test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
}
(void)tableView:(UITableView *)datesTable didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
Just put this code in your application.
Try not to deselect cell before presenting your seque
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Call This Line
[self performSegueWithIdentifier:#"ShowMobileShopStore" sender:nil];
//Before This
[tableView deselectRowAtIndexPath:indexPath animated:true];
}
The initial question was: indexPathForSelectedRow return 0 (incorrect value),
Well, this is by-design, from the apple developer notes, the value returns an the IndexPath which can be NULL (is what you are seeing).
So, to do things right:
1) You need to test if the indexPathForSelectedRow is NOT NULL first
2) If it's not NULL then it would contain (.row) which would then give you the selected row-index, this starts from 0.
3) Please note that indexPathForSelectedRow.row would return ZERO in the case of indexPathForSelectedRow is NULL.
Here is the code that I've tested and works:
if (MyTableViewList.indexPathForSelectedRow)
{
long SelectedRow = MyTableViewList.indexPathForSelectedRow.row;
}
I hope this helps,
I have a tableviewcell in the RegisterViewController, when i click on it, it push to the SelectCityViewController where i can pick a city from a uipickerview. and I have defined a delegate in the SelectCityViewController. But when i implement the delegate method, i got the city back from the pickerview and the selected indexpath from didSelectRowAtIndexPath in RegisterViewController. When i check the log, i get the city, and the selected indexpath. but when i call reloadRowsAtIndexPaths, the cell still got the old titlelabel text. the code:
SelectCityViewController.h
#protocol SelectCityDelegate <NSObject>
- (void)didGetCity:(City *)city;
#end
SelectCityViewController.m
- (void)finished:(UIBarButtonItem *)buttonItem
{
if ([self.delegate respondsToSelector:#selector(didGetCity:)]) {
[self.delegate didGetCity:self.city];
}
NSLog(#"%#", self.city.city);
[self.navigationController popViewControllerAnimated:YES];
}
RegisterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
self.selectedIndexPath = indexPath;
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
SelectCityViewController *signUp = [storyBoard instantiateViewControllerWithIdentifier:#"SignVC"];
signUp.delegate = self;
[self.navigationController pushViewController:signUp animated:YES];
}
}
- (void)didGetCity:(City *)city
{
NSLog(#"%#", city.city);
NSLog(#"%#", selectedIndexPath);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.textLabel.text = city.city;
[self.tableView reloadRowsAtIndexPaths:#[selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
I think the problem is in the below code you don't have to set the value here, because when you will reload, it will override the value with what is in the function where you are creating your cells.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.textLabel.text = city.city;
Just reload the cell, and put above code in your
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Cell intialization and other things
cell.textLabel.text = city.city;
}
didGetCity needs to update the model that backs the table, not the table cell itself. How do you answer numberOfRowsInSection:? With the count of some array?
That array at index selectedIndexPath.row needs to change. cellForRowAtIndexPath: should initialize the cell with that same data. Then your didGetCity method updates the array and reloads. It should not refer to cells.
I'm trying to create an iOS app in which I must have a UITableView that every time I press a new entry button, a newcell appears with the time when I pressed that button. My problem is that everytime I press the buttons, not only the cell which is created displays the current time, but the cells above it, which were showing a different time, reload and also show the current time. To try and explain it better, if I press the button at 8:05, 9:01 and 9:10, i want the UITableView to show:
-8:05
-9:01
-9:10
Instead, it's showing:
-9:10
-9:10
-9:10.
What do I do?? Thanks
Here's my code ( newEntry is the button and brain is an object where I have the method to get the current time)
#implementation MarcaPontoViewController{
NSMutableArray *_entryArray;
#synthesize brain=_brain;
- (void)viewDidLoad
{
[super viewDidLoad];
_brain = [[Brain alloc] init];
_entryArray = [[NSMutableArray alloc] init];
//[self updateTime];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_entryArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_entryArray lastObject];
}
return cell;
}
- (IBAction)newEntry:(id)sender {
[_entryArray addObject:[self.brain currentTime]];
[_timeTable reloadData];
}
#end
Your problem is here in this line :
cell.textLabel.text = [_entryArray lastObject];
You need to use :
cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row];
Or,
cell.textLabel.text = _entryArray[indexPath.row];
[_entryArray lastObject] always gives the last returned object.
Use
cell.textLabel.text = [_entryArray objectAtIndex: indexPath.row];
The line cell.textLabel.text = [_entryArray lastObject] will only ever return the last object in the array, which is why you are seeing the same time repeated. Change this to:
// in cellForRowAtIndexPath:
cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row];
This should fix the underlying problem.
i want to pass the properties of data after select a tableviewcell.Please guide me.i already setup the detailviewcontroller.DataArray is my mutablearray in detailviewcontroller.peripheralManager is my NSbject.Peripheral is my device.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
peripheralManager=[[PeripheralManager alloc]init];
self.MeBle.enabled=NO;
device=[[NSMutableArray alloc]init];
}
- (void)peripheralManagerDidConnectPeripheral:(PeripheralManager *)peripheral
{
NSLog(#"%#",peripheral.deviceName);
[device addObject:[NSString stringWithFormat:#" %# ",peripheral.deviceName]];
// [device addObject:peripheral];
[self.deviceTable reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell==nil)
{
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text=[device objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//DetailViewController *detail=[self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
// [self.navigationController pushViewController:detail animated:YES];
//[self performSegueWithIdentifier:#"TableDetails" sender:[device objectAtIndex:indexPath.row]];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"TableDetails"]) {
DetailViewController *detail=segue.destinationViewController;
detail.dataArray=device;
}
}
in the prepareForSegue you should get the current selection index:
NSIndexPath *indexPath = [self.deviceTable indexPathForCell:sender];
now, you may either pass just this index to the destination controller OR specific object from you current array, its up to you.
detail.idx = [indexPath row];
Edit: there you don't need to implement
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
just in
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
change to this:
detailViewObject.objCurrentDevice=[device objectAtIndex:[[self.deviceTable indexPathForCell:sender] row]];
In interface builder, check out the seague runtime attributes section, u can create multiple cell same style but different identifier then cellForRowAtIndexPath return the cell by its row
i can successfuly delete data from tableview , but when i go to main menu and come back then i see all the old data,
how to make data consistent , i.e once deleted it will be gone forever
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate];
{
appDelegate.favDetail = [[NSMutableArray alloc] initWithObjects:#"1", #"2",#"4",nil];
}
}
- (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];
}
appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *cc=[appDelegate.favDetail objectAtIndex:indexPath.row];
NSLog(#" vale is %d",indexPath.row);
// Configure the cell...
NSArray *theParameters = [cc componentsSeparatedByString:#","];
NSLog(#"array is %#",cc);
NSLog(#"arraytheParameters is %#",theParameters);
cell.text=cc;//theParameters;
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ NSLog(#"matrix is her%#",appDelegate.favDetail);
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[appDelegate.favDetail removeObjectAtIndex: indexPath.row];
[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
/*
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
} */
}
- (void)viewWillAppear:(BOOL)animated
{ [tableView reloadData];
[super viewWillAppear:animated]; // no use , it shows old data
}
thanks
If you are looking to save the state of your model beyond the life of your table view controller, there are few things you can do. One is to write the array to a file in the documents directory and read it back on reload. This would work well will small sets of data. But if you are going to use a large data set, you have to use Core Data.
As for this specific case, you are reinitializing the data on every viewDidLoad. I suggest you do this in viewDidLoad –
...
if ( appDelegate.favDetail == nil ) {
appDelegate.favDetail = [[NSMutableArray alloc] initWithObjects:#"1", #"2",#"4",nil];
}
...
This will make sure that you don't reinitialize it.
you have to delete the data in both your view and your model
Check out the place where appDelegate.favDetail is getting data from...
While deleting you are removing from appDelegate.favDetail but When you go to main menu and come back you are reassigning the values to appDelegate.favDetail...
Checkout that appDelegate.favDetail only gets the value once.. or another option is you remove the data from the main source from where the favDetail gets the value
Happy iCoding....