pass data from tableview controller to details table view controller - ios

I have the following code. I want to display the data that is selected. However, below code is display every post in database. I would like it to display just the one that is selected. How can i do that?
Post Table View Controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (![self objectAtIndexPath:indexPath]) {
// Load More Cell
[self loadNextPage];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
UserFeedTableViewCell *cell = (UserFeedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UserFeedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (object) {
cell.postView.text = [object objectForKey:#"post"];
// PFQTVC will take care of asynchronously downloading files, but will only load them when the tableview is not moving. If the data is there, let's load it right away.
}
return cell;
}
Details View Controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell1";
CommentsTableViewCell *cell = (CommentsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CommentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (object) {
cell.detailsPost.text = [object objectForKey:#"post"];
}
return cell;
}

You should select a row in tableView:didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// store relevant value in your text object
[self performSegueWithIdentifier:#"showSegue" sender:self];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
and the perform segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DetailViewController *controller= [segue destinationViewController];
// set your text object here
controller.text = // your text object
}
HTH

Create an object of the detail table view controller (on which you have to display the data) in the table view controller (from which data is sent), in the following way,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailTableViewController *objNext = (DetailTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"detailVC"];
objNext.strLink=[[feeds objectAtIndex:indexPath.row] valueForKey:#"link"];
[self.navigationController pushViewController:objNext animated:YES];
}
Pass the data which you want to pass on row select,
Here are some links which show how to pass data from one view controller to another, some of them are from stack overflow itself.
Passing Data between View Controllers
ios Pass data between navigation controller
https://teamtreehouse.com/community/how-to-pass-data-between-two-view-controllers-when-you-have-tab-bar-and-navigation-controller-in-between

Related

Tableview data shown on label from table view

I am new in iOS development and i want to know when i click on row of table, my data which are shown in table view should be select on label. My label has name City.
- (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 = [arrayname objectAtIndex:indexPath.row];
return cell;
}
In viewDidLoad add this array
dataArr=[[NSMutableArray alloc]initWithObjects:#"Hyderabad",#"Bangalore",#"Chennai",#"Pune",#"Mumbai", nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reUseid=#"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reUseid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseid];
}
cell.textLabel.text = dataArr[indexPath.row];
return cell;
}
Here is TableView didSelectRowAtIndexPath method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"selected is %#",[dataArr objectAtIndex:indexPath.row]);
[self performSegueWithIdentifier:#"viewToDescriptionSegue" sender:self];
}
in this method you got indexpath from that you can get selected row like indexpath.row and from that you can get object from array.
There is a UITableViewDelegate method named didSelectRowAtIndexPath which get called up when user select any row. Get the value from your array at indexPath.row and set the text on your label.
Implement the below code on your project:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *name = [arrayname objectAtIndex:indexPath.row];
[yourLabelInstance setText:name];
}
This is a delegate method of UITableView which is called when you tap on cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
city.text = [arrayname objectAtIndex:indexPath.row];
}
Below is a delegate method of UITableView which is called when you touch/select any row in table.
Here, using indexPath parameter you can access currently selected row index. You can access data from datasource array having selected row index.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *strCity = [arrCity objectAtIndex:indexPath.row];
[lblCity setText:strCity];
}

How to pass selected index id to another TableViewControl and Compare using Parse in iOS

For Example:= According to Perticular State all City display in another view Controller's TableView.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
PFObject *tempobj=[list objectAtIndex:indexPath.row];
cell.textLabel.text=[tempobj objectForKey:#"stateName"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Tapped");
CityController *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"CityController"];
[self.navigationController pushViewController:obj animated:YES];
}
Try using Notify, it will keep the controllers from needing to know about each other.

Swipe to delete not working properly with custom XIB cell

I have a problem with swipe delete function in UITableView with cell loaded from an XIB. As you can see below, when I swipe the cell to left image moves left but label doesn’t. So label covers delete button. Below you can find the code I have implemented:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MostKadroCell";
MostKadroCell *cell = (MostKadroCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO]; //
cell.lblKadroName.text = [arrKadroName objectAtIndex:indexPath.row];
[cell.imageView setImage:[UIImage imageNamed:strImage]];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"MostKadroCell" bundle:nil];
[self.tableMostKadro registerNib:nib forCellReuseIdentifier:#"MostKadroCell"];
....
Keep label under(inside) the content view of the UITableView cell. Please check if its not out side the content view.
and add
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
View should seems likes

Using RestKit and JSON to populate more than one UITable ViewController (second Table is crashing app)

I'm using a website API to populate my tableViews, using Restkit and JSON for request/receive/parse data into my model. Already set up models and mapping which seem to be working fine.
The problem is after the initial table view that loads Section & Row, I need the second tableView to use the cell/row selected from the tableView, to show the team associated with that specific League that was chosen in the second tableView.
I believe it's the ...
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
that I don't have or have correct, because it's:
Populating data correctly in initial screen:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sports.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.leagues.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Sport *sport = [sports objectAtIndex:indexPath.section];
League *league = [sport.leagues objectAtIndex:indexPath.row];
cell.textLabel.text = league.name;
return cell;
}
But if I select a cell, the second ViewController is a blank tableView (note: no prepareForSegue or didSelectRowAtIndexPath code in previous ViewController tableView code, so I think thats why I might be getting this blank tableview):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return leagues.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.teams.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
League *league = [leagues objectAtIndex:indexPath.section];
Team *team = [league.teams objectAtIndex:indexPath.row];
cell.textLabel.text = team.name;
return cell;
}
Or if I add prepareForSegue and didSelectRowAtIndexPath code in previous ViewController, select a cell which would move us to the next ViewController, the app crashes (prepareForSegue and didSelectRowAtIndexPath code aren't correct for sure, I was trying to just piece something together even though I did at least some of it is wrong):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TeamsViewController *teamsViewController = [[TeamsViewController alloc] initWithNibName:#"TeamsViewController" bundle:nil];
teamsViewController.title = [[sports objectAtIndex:indexPath.row] objectForKey:#"sports"];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"leagueDetail"]) {
TeamsViewController *tvc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
tvc.data = [self.navigationController objectInListAtIndex:indexPath.row]
}
I think what I'm running into is needing for the selected cell to pass its info to the next tableView somehow, and I don't know if it needs to somehow append that info to the baseURL I'm retrieving the data from for that selected row, and then also somehow show the data in the teams tableView. But I'm not sure if thats the problem, and/or how exactly to implement it.
Would appreciate any help, and just let me know if more code/details are needed, I'd be happy to get them quickly for anyone.
EDIT: Here is my updated prepareForSegue code, still have error though-
Error: Property 'leagues' not found on object of type 'TeamsViewController'
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"leagueDetail"]) {
// note that "sender" will be the tableView cell that was selected
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
TeamsViewController *tvc = (TeamsViewController *)[segue destinationViewController];
tvc.leagues = [leagues objectAtIndex:indexPath.section];
}
}
You need to link your Prototype Row in storyboard with the next view controller, name the segue and use prepareForSegue to pass the necessary data. No need for didSelectRow....
To reference the row, use
[self.tableView indexPathForCell:(UITableViewCell*)sender]
(I would not rely on the selected state of the row. You would be mixing View and Model domains, violating MVC principles.)
Also, I noticed a certain logic mistake. If in VC2 you want to show leagues as the sections and teams on the table view rows, you should really be selecting a Sport not a league. A league does not have many leagues, right?

Not able to populate second UITableView Controller

I'm unable to populate the second UITableView Controller, wondering if anyone could help.
I'm using a websites API, JSON, and RestKit for the data. I believe that part is working fine because my first VC loads fine.
But I'm not sure if I need to use prepareForSegue and/or didSelectRowAtIndexPath so that I can identify the cell/row selected in the first VC, so that the second VC populates with the (correct) data.
1st VC populates correct:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sports.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.leagues.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Sport *sport = [sports objectAtIndex:indexPath.section];
League *league = [sport.leagues objectAtIndex:indexPath.row];
cell.textLabel.text = league.name;
return cell;
}
2nd VC populates blank table:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return leagues.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.teams.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
League *league = [leagues objectAtIndex:indexPath.section];
Team *team = [league.teams objectAtIndex:indexPath.row];
cell.textLabel.text = team.name;
return cell;
}
Or if I try to add extra code for 1st VC, app crashes before getting to 2nd VC:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TeamsViewController *teamsViewController = [[TeamsViewController alloc] initWithNibName:#"TeamsViewController" bundle:nil];
teamsViewController.title = [[sports objectAtIndex:indexPath.row] objectForKey:#"sports"];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"leagueDetail"]) {
TeamsViewController *tvc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
tvc.data = [self.navigationController objectInListAtIndex:indexPath.row]
}
Would really appreciate any help!
I am a little bit confused with excepting of creation UITableViewCell object. When you ask table view for dequeuing cell it returns one which it does not need at the moment, if there are no unused cell you have to create a new one. Try code like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create cell
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [NSString stringWithFormat:#"cell %d", indexPath.row];
return cell;
}

Resources