user1330343
user1330343

Reputation: 21

Use the text from a cell in UITableView

I want to use the text of a selected cell in a UITableView. It will be used on a switch statement in a different view. How can I do it?

This is the code for the UITableView:

- (void)viewDidLoad
{
[super viewDidLoad];

_list = [[NSArray alloc] initWithObjects:@"Academic Advising Center", @"Academic Vice      President", @"Administrative Services Vice President", @"Admissions", @"Advanced Standing and Transcript", @"Alcohol and Substance Abuse Programs", @"Allied Health Department", @"Alumni Network", nil];

_displayItems = [[NSMutableArray alloc] initWithArray:_list];

}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayItems count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

cell.textLabel.text = [_displayItems objectAtIndex:indexPath.row];   //TRY THIS ONE!!
return cell;
}

This is the code for the view that will use the text of the cell in UITableView (right now, I am using the cell number in a switch statement, but I want to use the contents of the cell instead of the cell number):

- (void)configureView 
{
// Update the user interface for the detail item.


switch (_itemNumber)
{
    case 0:
        [_phoneButton setTitle:@"233-123-2133" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Acdemic Adv. Center";
        break;
    case 1:
        [_phoneButton setTitle:@"324-123-4231" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Acdemic VP";
        break;
    case 2:
        [_phoneButton setTitle:@"232-222-3333" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Admin. Serv. VP";
        break;
    case 3:
        [_phoneButton setTitle:@"111-222-3333" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Admissions";
        break;
    case 4:
        [_phoneButton setTitle:@"343-333-2222" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Adv. Stand. & Trans.";
        break;
    case 5:
        [_phoneButton setTitle:@"333-333-3333" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Alc. & Subs. Abuse Prog.";
        break;
    case 6:
        [_phoneButton setTitle:@"444-332-2222" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Allied Health Dept.";
        break;
    case 7:
        [_phoneButton setTitle:@"343-222-4444" forState:UIControlStateNormal];
        [_emailButton setTitle:@"[email protected]" forState:UIControlStateNormal];
        self.title = @"Alumni Network";
        break;

    default:
        [_phoneButton setTitle:@"" forState:UIControlStateNormal];
        [_emailButton setTitle:@"" forState:UIControlStateNormal];
        self.title = @"";
        break;
    }
}



- (void)viewDidLoad
{
[super viewDidLoad];

_itemNumber = [[NSUserDefaults standardUserDefaults]integerForKey:@"Cell"];

// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}

I know that: _itemNumber = [[NSUserDefaults standardUserDefaults]integerForKey:@"Cell"]; Will use the number of the cell, how can I use the contents?

Upvotes: 0

Views: 111

Answers (2)

JonathanC
JonathanC

Reputation: 977

Use an NSArray with multiple NSDictionary instances as your data store (do not load your table directly from this NSArray though). This is what Ivan Alek suggests in another answer.

Then, store in an NSMutableArray the indices (NSNumbers) of the NSDictionary instances that match the search. That way your basic data store will not be changed by the Search bar, only this array of indices.

Some sample code:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSInteger listIndex = [[_searchResultIndices objectAtIndex:[indexPath row]] integerValue];
    // assuming you create a dictionary where the title has the key title
    cell.textLabel.title = [[_items objectAtIndex:listIndex] objectForKey:@"title"];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_searchResultIndices count];
}

Upvotes: 0

Ivan Alek
Ivan Alek

Reputation: 1919

This is actually very bad approach for doing things like this. I would suggest you to store data into NSArray with multiple NSDictinary, which will contain user info name/phone/email. Then map every object from NSArray into table view, so cell with certain index will have data into array with same index.

Upvotes: 1

Related Questions