raginggoat
raginggoat

Reputation: 3600

Populating UITextView with Specific Info when UITableView Row is Selected

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

Upvotes: 0

Views: 833

Answers (5)

D.M Patel
D.M Patel

Reputation: 145

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 .

Upvotes: 0

Shantanu
Shantanu

Reputation: 3136

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

Upvotes: 1

Jitendra
Jitendra

Reputation: 5081

- (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.

Upvotes: 0

Janene Pappas
Janene Pappas

Reputation: 1366

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
}

Upvotes: 0

matt
matt

Reputation: 535850

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.

Upvotes: 0

Related Questions