how do i add individual data to individual cells? - iphone

How do make the individual cells go to different View Controller. Right now, using the method im using, if i click the first cell, it takes me to a view controller where i have a pic. now if i go back and click the second cell, it takes me to the same view controller. How do i change this so that all the other individual cells go to individual view controller? what do i add or what do i configure?
here my code:
// 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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [breakdownArray objectAtIndex:row];
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
NSInteger row = [indexPath row];
if (self.bookDetailViewController == nil); {
BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
self.bookDetailViewController = aBookDetail;
[aBookDetail release];
}
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
}

One fairly easy way is in your tableView:didSelectRowAtIndexPath: method. You can check the index path parameter and execute different code paths. Something like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
switch(indexPath.row) {
case 1:
[self.navigationController pushViewController:someViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:someOtherViewController animated:YES];
break;
}
}
Like most design choices, this is just an example of one possible approach. That's the best I can do in this general case.

I would create a new view controller each time instead of using the same one and just changing its properties, more like the example that you have commented out in tableView:didSelectRowAtIndexPath:. This will only work if you don't access your controller from anywhere other than these methods.
First: Remove bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]]; from your tableView:cellForRowAtIndexPath: method (you should do this anyway, since it is just taking more processor time)
Second: Remove the bookDetailController property from your class. You won't need it anymore.
Third: Implement tableView:didSelectRowAtIndexPath: as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
BookDetailViewController *bookDetailViewController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
NSInteger row = path.row;
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
//Any other setup on view controller
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
[bookDetailViewController release];
}

Related

How to display the DetailView after a search

I use predicate to filter a table.
It works very well and also to display the detailView for each cell.
But after a filtering it does not work to display the filtered list in the detailView.
The cell just turns blue when pressing it but does not shift to detailView.
What am I missing here?
The code looks like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}
dvController.title = person.firstName;
dvController.price = person.sjuName;
dvController.link = person.sexName;
dvController.picture = [UIImage imageNamed:person.femName];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Here both the tableView are of different class so the can't be equal thus change your if condition as
if ([tableView class] != [UITableView class]) // if it's is of type UISearchResultsTableView
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}

how to Navigate from one cell to next view?

I want my app to have 5 rows and each row has a particular height. Each row has a title, subtitle and an image. Then i want to be able to navigate to the next page when i tap on either of the rows(for e.g say 3rd row). how do i do this?
Answer to your 2nd part of the question is the initWithStyle:UITableViewCellStyleSubtitle…. this one allows you to have a title and subtitle. For the image, each cell has already built-in.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *title = (#"your title");
NSString *subTitle = (#"your subtitle");
cell.textLabel.text = title; //title
cell.detailTextLabel.text = subTitle; //subtitle
NSString *filePath = //file path to your image
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
cell.imageView.image = [myThumbnailClass image]; //image
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==0)
{
singleProductViewController=[[SingleProductViewController alloc]initWithNibName:#"SingleProductViewController" bundle:nil];
[self.navigationController pushViewController:singleProductViewController animated:YES];
[singleProductViewController release];
}
else if(indexPath.row==1)
{
//Sec view Navigation
}
//Like wise u go on
}
Use method for handle tableview touch
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
You can check indexPath which row taped.
See this tutorial - Navigating a Data Hierarchy With Table Views
Use the tableview delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
singleProductViewController=[[SingleProductViewController alloc]initWithNibName: #"SingleProductViewController" bundle:nil];
[self.navigationController pushViewController:singleProductViewController animated:YES];
[singleProductViewController release];
}
SingleProductViewController is the new view you want to navigate
All the best

how display view when cells select in a tableview

i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method

how to get text of cell from table in to string

i have 2 views and both are table views
1st view
list is there, so when user click on particular cell, i want to take the text of selected cell and store into variable and push 2nd view
e.g. stringVariable = cell.text
2nd view
now i want to set title of view by using stringVariable variable
e.g. self.title = stringVariable;
i hope some one know this issue
thank you in advance
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Save text of the selected cell:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stringVariable = cell.textLabel.text;
// load next view and set title:
MyView *nextView = [[MyView alloc] initWithNibName:#"MyView" bundle:nil];
nextView.title = stringVariable;
[self.navigationController pushViewController:nextView animated:YES];
}
You must be using some array data to fill the table Views.
eg.
- (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];
}
TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
UILabel mainLabel = [[UILable alloc] initWithFrame:()];
mainLabel.text = tableItem.name;
[cell.contentView addSubView:mainLabel];
[mainLabel release];
return cell;
}
Similarly you should use the same array to get the required item, which is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
OtherViewController * otherViewController = [[OtherViewController alloc] init];
otherViewController.name = tableItem.name;
[self.navigationController pushViewController:otherViewController animated:YES];
[otherViewController release];
}

After reloading my tableView with content, it still appears greyed-out until I tap it..why?

After retrieving search results from a UISearchBar, the results appear in my tableview correctly, but the view is 'greyed out' (see image below)..Any help on this is appreciated, I can't find a solution the Apple documentation.
This is my code that is fired upon hitting the Search button:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
isSearchOn = YES;
canSelectRow = YES;
self.tableView.scrollEnabled = YES;
CityLookup *cityLookup = [[CityLookup alloc] findCity:searchBar.text];
if ([cityLookup.citiesList count] > 0) {
tableCities = cityLookup.citiesList;
}
[cityLookup release];
isSearchOn = NO;
self.searchBar.text=#"";
[self.searchBar setShowsCancelButton:NO animated:YES];
[self.searchBar resignFirstResponder];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.tableView reloadData];
}
And this is how the table view is being refreshed:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *cellValue = [tableCities objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
If you are using the full package with a UISearchDisplayController the following line should remove the search interface:
[self.searchDisplayController setActive:NO animated:YES];
If you are not using UISearchDisplayController I would recommend checking it out and see if it doesn't suit your needs.
Remark: There is nothing in your posted code that would remove whatever view you have used to grey out the table view. So if you are not using UISearchDisplayController look in the code that displays the search interface to see what you need to do to remove it.
Your not hiding your search bar when hitting search, try:
[self.navigationController setNavigationBarHidden:YES animated:YES];