Populating UITextView with Specific Info when UITableView Row is Selected - ios

I have a UITableView and another view. The other view has a UITextView that I want populated with different text depending on what row in the table view is selected. I know how to do it if I create different views for all of the different text options. If I did it that way, I could just have that view open when the row is tapped. I'm just wanting to avoid having to create a bunch of different views and just use one that gets loaded with different text depending on row tapped in the table view. I guess I just need to know how the view with the text view can access indexPath.row from the table view. Any suggestions?
EDIT: Here is my code.
LHRRoster.m
#import "LHRRoster.h"
#import "CustomCellBackground.h"
#import "OnMyHonor.h"
#import "AVA.h"
#import "Receivers.h"
#import "BandDetailView.h"
#interface LHRRoster ()
#end
#implementation LHRRoster
{
NSMutableArray *mbTableData;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
mbTableData = [NSMutableArray arrayWithObjects:#"Aesthetics Versus Architecture", #"On My Honor", #"Receivers", #"Skyscraper Stereo", #"California", #"Late Ones", #"Uh-Huh Baby Yeah", #"Danielle Bouchard", #"Chris Karrer", #"Joey Blue", #"The Codas", #"Your Favorite Hero", #"Nixon", #"Skam Impaired", #" SaySomethingHugeOnFire", #"Adore Me Not", #"Common Collective", #"The Royal Tees", #"The Gravetones", #"Bush League", #"Rock & Roll Television", #" Tastyface", #"The Lips", #"Mercy Academy", #"Audiostrobelight", nil];
self.title = #"LHR Roster";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mbTableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *mbTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier];
cell.textLabel.font=[UIFont systemFontOfSize:14.0];
}
cell.backgroundView = [[CustomCellBackground alloc] init];
cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
cell.textLabel.text = [mbTableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BandDetailView *detailView = [[BandDetailView alloc]initWithNibName:#"BandDetailView" bundle:nil];
if (indexPath.row == 0)
{
detailView.title = #"Aesthetics vs Architecture";
UIImage *image = [UIImage imageNamed: #"AVA.png"];
[detailView.bandPic setImage:image];
NSString *bio = #"";
[detailView.bandInfo setText:bio];
[self.navigationController pushViewController:detailView animated:YES];
}
}
#end

You are getting the selected cell object, just pass that object to the TextView and it will dynamically load selected cell data.An important thing to note,first push view controller then pass data to that controller.In your code you have passed data first,then pushed view controller, that doesnt work

MVC - model, view, controller. Your data is model. The table view and the text view are view. Your UIViewController is the controller. It does all the work.
Implement didSelectRowAtIndexPath in the controller, which is the delegate of the table view. Now you know that a row of the table was selected, and you know which row it is. So pull the desired text out of your model and stick it in the UITextView.

Expand your didSelectRowAtIndexPath to also modify your UITextView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
**myUiTextView.text = somethingBasedOnTheRowSelected[indexPath.row];** <-- new code here
// followed by your existing code
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BandDetailView *detailView = [[BandDetailView alloc]initWithNibName:#"BandDetailView" bundle:nil];
if (indexPath.row == 0)
{
detailView.title = #"Aesthetics vs Architecture";
UIImage *image = [UIImage imageNamed: #"AVA.png"];
[detailView.bandPic setImage:image];
NSString *bio = #"";
[detailView.bandInfo setText:bio];
[self.navigationController pushViewController:detailView animated:YES];
}
**myUiTextView.text =[yourarray indexPath.row];
}
follow this code this code might help you.

Just Add your code is
yourtextfieldname.text=[yourarrayname objectAtIndex:indexpath.row];
and get your selected row textfield data in your textfield.
i hope this code is useful for you .

Related

UITableview not showing content/ empty cells

I'm still learning iOS and this is my first major project, so go easy on me. I'm trying to make a gesture-driven task list. I still have a lot of work to do, but I hope you guys can help me get this working. I'm certain I set the delegate and data source properly, but it's still not showing up...anyway, here's my ViewController.m file.
Thank you in advance!!
#import "AGViewController.h"
#import "AGTaskObject.h"
#import "AGTableViewCell.h"
//#import "AGSettingsViewController.h"
#interface AGViewController ()
#end
#implementation AGViewController {
NSMutableArray *taskItems;
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
//Initializing the Array
taskItems = [[NSMutableArray alloc] init];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Tap the + to add a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Tap the Gear Button to check out settings"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Swipe Right to check off a task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Swipe Left to Delete a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"I <3 Bacon"]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Table View Delegates
self.tableView.delegate = self;
self.tableView.dataSource = self;
//set the initial background image
self.tableView.backgroundColor = [UIColor clearColor];
self.backgroundImage.image = [UIImage imageNamed:#"blue.png"];
[self.tableView registerClass:[AGTableViewCell class] forCellReuseIdentifier:#"cell"];
//Get rid of empty cells at bottom of tableview
// self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Long Press Gesture to rearrange cells
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer:longPress];
// // Tap Gesture to add a new cell
// UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(<#selector#>)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Button Press (IB)Actions
- (IBAction)settingsButtonPressed:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toSettingsViewController" sender:nil];
}
- (IBAction)addTaskButtonPressed:(UIButton *)sender
{
}
#pragma mark - UITableView Configuration
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [taskItems count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
//re-Use or create a tableview cell
AGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
/* if (cell == nil) {
cell = [[AGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
*/
//Make the cells clear to show the background image
cell.backgroundColor = [UIColor clearColor];
//default font color will be white
cell.textLabel.textColor = [UIColor whiteColor];
//Set the task for each cell in the tableview
AGTaskObject *item = taskItems[indexPath.row];
//set the text
cell.delegate = self;
cell.taskObject = item;
// Recall final cell
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [self colorForIndex:indexPath.row];
}
#pragma mark - TableItem Delete
-(void)toDoItemDeleted:(id)taskobject {
// use the UITableView to animate the removal of this row
NSUInteger index = [taskItems indexOfObject:taskobject];
[self.tableView beginUpdates];
[taskItems removeObject:taskobject];
[self.tableView deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
Storyboards don't use initWithNibName (even though Xcode creates it pointlessly) so your array is empty. Try moving it to viewDidLoad

Custom table view cells show up blank

I have a regular view controller (not a table view controller) and on that view I have a tableview. My table view cells are custom and I just made them through storyboards (didn't do anything in the code) but when I run my application, the tableview is blank. Any ideas as to why tho is happening to me? I have looked at other things on here but all of these other scenarios have to do with the person using an NSArray to fill out the tableview in the code, but mine is custom so I am not doing that. Thanks for any help. And before you mark this duplicate, please actually read this.
my code is as follows:
#interface TTViewController ()
{
NSArray *messageComponents;
}
#end
#implementation TTViewController
#synthesize dateTimePicker, messageSetupTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Message Setup";
messageComponents = [[NSArray alloc] initWithObjects:#"Recipient",#"Message", #"Date",#"haha", nil];
messageSetupTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
messageSetupTableView.alpha = 0.9;
}
#pragma mark -
#pragma mark Table view data source
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
return [messageComponents count];
}
-(NSInteger)tableView:(UITableView *) tableVew numberOfRowsInSection:(NSInteger)section{
return 1;
}
// 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];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
}
}
// Configure the cell.
cell.textLabel.text = [messageComponents objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[messageComponents objectAtIndex:indexPath.row]];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor whiteColor];
UIColor *selectedColor = [UIColor blueColor];
UIView *myBackgroundColor = [[UIView alloc] init];
[myBackgroundColor setBackgroundColor:selectedColor];
[cell setSelectedBackgroundView:myBackgroundColor];
return cell;
I want my table view to have a date picker in one section, a textview in the other, and a button and a few text fields in the other. Thanks
A couple of suggestions:
First make sure that UITableView's delegate and datasource are mapped or not either from IB or programmatically.
Put a break point on cellForRowAtIndexPath delegate function and see if it comes in this functions, and make sure you are using this function to create your custom cells.
Double check your have at least one row in your tableView using something similar:
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return 5; // for test pass value 5 so tableView should aware to display 5 rows.
}
Hope it helps!

My table view doesn't populate

I'm using this tutorial on how to create a pop out menu.
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
I got it working when going through the tutorial, now i'm trying to implement it into my app.
I'm at the stage where i can press the menu button and the popout menu appears. The problem is, it doesn't populate itself with my table view cells.
I set the identifiers for each table view cell and then in the code reference them to full an array.
I know when woking through the tutorial, if I misspelled one of the identifiers when defining what's in the array, the program would crash. In my app, that's not the case. Hopefully that can help pin down the problem. It doesn't even change the colours which is the first part of the code.
Here's the code.
#import "SidebarViewController.h"
#import "SWRevealViewController.h"
#interface SidebarViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation SidebarViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
_menuItems = #[#"markup",#"tax"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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 [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Any help would be great.
Thanks
All you're doing is creating an array with two string literals. You must set the textlabel's text property in the following method to display the strings in 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.text = [_menuItems objectAtIndexPath:indexPath.row];
return cell;
}
OMG I feel like such a noob. It turns out the ViewControler didn't have a target, hence why it wasn't responding.
I'm so sorry for wasting your time, I really feel bad now.

Tableview with details

I have created a tableview with the data from an array. Now i want the details for all the rows of the tableview. If i select any recipe from the tableview it should show me all the ingredients and procedure to prepare that recipe with a pic of that particular recipe on top. I am new to this field so kindly help.
Here's "ViewController.m"
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:#"Chicken Kabab"];
[RecipeList addObject:#"Chicken Tikka"];
[RecipeList addObject:#"Chicken 65"];
[RecipeList addObject:#"Chicken Do Pyaza"];
CGRect frame = self.view.frame;
RecipeTable = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
RecipeTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
RecipeTable.backgroundColor = [UIColor cyanColor];
RecipeTable.delegate = self;
RecipeTable.dataSource = self;
[self.view addSubview:RecipeTable];
[RecipeTable setScrollEnabled:YES];
[RecipeTable setUserInteractionEnabled:YES];
[RecipeTable setRowHeight:35];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"RECIPES";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return RecipeList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [RecipeList objectAtIndex:indexPath.row];
return cell;
}
#end
Just pass recipe name to the detailViewController. Then depending on the name of the recipe you can load the suitable image in UIImageView.
In the code recipename is an NSString in detailViewController. Declare it as a property..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSString *name=[recipe objectAtIndex:indexPath.row];
detailViewController.recipename=name;
[self.navigationController pushViewController:detailViewController animated:YES];
}
Then in the detaiViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if([recipename isEqualToString:#"Pizza"])
{
UIImage *image = [UIImage imageNamed: #"pizza.png"];
[img setImage:image];
}
else if([recipename isEqualToString:#"Chicken 65"])
{
UIImage *image = [UIImage imageNamed: #"chicken.png"];
[img setImage:image];
}
}
you must use the DidSelectRow Delegate of UITableView . Then if you select a recipe it calls the DidSelectRow delegate .Inside this delegate you do the coding for pushing to a new ViewController . Create an UIImageView on top of your new ViewController , then pass the image from your TableViewCell to the UIImageView of your new viewController
If you want the details of a particular Dish populated in another ViewController that is pushed, the easiest way is by creating an NSObject with all the details required for that particular dish and maintaining it as an array. When you select a row in tableView, the didSelectRowAtIndexPath: get the object at that indexPath and pass it to the next VC which is being pushed. In the detail VC you can populate the details with that from the object.
Instead of doing like this
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:#"Chicken Kabab"];
do something like
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:dishObj];
where dishObj is an NSObject, of Class Dish which contains Dish name, image and recipe.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
Dish *dishObject = [RecipeList objectAtIndex:indexPath.row];
cell.textLabel.text = dishObject.dishName;
return cell;
Try this ,
you must use the DidSelectRow Delegate of UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSString *strDishName = [RecipeList objectAtIndex:indexPath.row];
detailViewController.strDishDetail =strDishName;
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
I hope the following tutorial will useful for u..Segue

Can't push a new view controller from UITableView cell

I am trying to create a UITableView application on Xcode 4.2. I just want each cells (ex. Cali) to push a new UIViewController when it's touched.
The issue I'm running into is whenever I press the cell it's not pushing the new view controller.
When I put a break point at the didSelectRowAtIndexPath method, I see 5 threads.
Here's my code:
#import <UIKit/UIKit.h>
#interface Adam : UITableViewController
{
NSMutableArray *states;
}
#end
#import "Adam.h"
#import "ViewController.h"
#implementation Adam
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSMutableArray arrayWithObjects:
#"cali",
#"ohio",
nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states 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];
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[states objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[states objectAtIndex:indexPath.row] isEqual:#"cali"])
{
ViewController *cali = [[ViewController alloc] initWithNibName:#"cali" bundle:nil];
[self.navigationController pushViewController:cali animated:YES];
}
}
#end
First the TableViewCell has a textLabel property so you don't need to create your own label.
cell.textLabel.text= [states objectAtIndex:indexPath.row];
and is there a reason your deselecting the cell?Try deselecting it after you use the indexPath
And try:
if([states objectAtIndex:indexPath.row] isEqualToString:#"cali"]];){
}

Resources