1
votes

im a newbie and I'm really stuck and don't know what to do

i followed a tutorial on how to make a uitableview and populate it with data from nsmutablearray everything is fine till here

i wanted to add more description for the cell "subtitle" like numbers for my app i made it using another mutable array and its showing in the tableview

now when i press on the cell it goes to the detail view with only the cell title shown

i don't know how i can link the subtitle text to another label

please help

here is my main.h

@property (nonatomic, strong) NSMutableArray *objects; @property (nonatomic, strong) NSMutableArray *numbers; @property (nonatomic, strong) NSMutableArray *results; @property (nonatomic, strong) NSMutableArray *resultsnumber;

@property (nonatomic, strong) IBOutlet UISearchBar *searchBar;

here is my main.m

#import "main.h"
#import "DetailView.h"

@interface MasterTableViewController ()

@end

@implementation MasterTableViewController

- (NSMutableArray *)objects
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }

    return _objects;
}

- (NSMutableArray *)numbers
{
    if (!_numbers) {
        _numbers = [[NSMutableArray alloc] init];
    }

    return _numbers;
}


- (NSMutableArray *)results
{
    if (!_results) {
        _results = [[NSMutableArray alloc] init];
    }

    return _results;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add some objects to our array
    [self.objects addObject:@"YouTube"];
    [self.objects addObject:@"Google"];
    [self.objects addObject:@"Yahoo"];
    [self.objects addObject:@"Apple"];
    [self.objects addObject:@"Amazon"];
    [self.objects addObject:@"Bing"];
    [self.objects addObject:@"Udemy"];
    [self.objects addObject:@"Flickr"];

    [self.numbers addObject:@"1"];
    [self.numbers addObject:@"2"];
    [self.numbers addObject:@"3"];
    [self.numbers addObject:@"4"];
    [self.numbers addObject:@"5"];
    [self.numbers addObject:@"6"];
    [self.numbers addObject:@"7"];
    [self.numbers addObject:@"8"];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchThroughData
{
    self.results = nil;


    NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];
    self.results = [[self.objects filteredArrayUsingPredicate:resultsPredicate] mutableCopy];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self searchThroughData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.objects.count;
    } else {
        [self searchThroughData];
        return self.results.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (tableView == self.tableView) {
        cell.textLabel.text = self.objects[indexPath.row];
       cell.detailTextLabel.text = self.numbers[indexPath.row];
    } else {
        cell.textLabel.text = self.results[indexPath.row];

    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchDisplayController.isActive) {
        [self performSegueWithIdentifier:@"ShowDetail" sender:self];
    }
}

if ([[segue identifier] isEqualToString:@"ShowDetail"]) {
NSString *object = nil;
NSString *numbers = nil;

NSIndexPath *indexPath = nil;

if (self.searchDisplayController.isActive) {
    indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    object = cell.textLabel.text;
     numbers = cell.detailTextLabel.text;

} else {
    indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    object = cell.textLabel.text;
     numbers = cell.detailTextLabel.text;
}

[[segue destinationViewController] setDetailLabelContents:object];



    }
}

@end

and this is my detail view.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

@property (strong, nonatomic) NSString *detailLabelContents;
@property (strong, nonatomic) NSString *numberdetailLabelContents;

@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberdetailLabel;

and this is my detail view.m

#import "DetailView.h"
#import "main.h"


@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the label text
    self.detailLabel.text = self.detailLabelContents;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

i hope someone can help

1
You're already passing the detail label string to the detail controller, you just do the same thing with self.numbersArray to pass that value to numberdetailLabelContents.rdelmar

1 Answers

0
votes

This part of your code in prepareForSegue: is brittle:

if (self.searchDisplayController.isActive) {
            indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
            object = self.results[indexPath.row];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            object = self.objects[indexPath.row];
        }

It is brittle because you are getting the detail text from your results array and not the from the detail text label from the cell itself. Try getting a pointer to the selected cell and passing its detailTextLabel to your detailViewController instead. Make sure you set an IBOutlet to your tableView in MasterTableViewController. I'll call it resultsTableView in this example.

if (self.searchDisplayController.isActive) {
            indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
            UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
            object = cell.detailTextLabel.text;

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
            object = cell.detailTextLabel.text;
        }

DetailViewController *detailVC = [segue destinationViewController];
detailVC.detailLabelContents = numbers;

Let us know if this works!