2013-01-27 5 views
0

Je souhaite utiliser le texte d'une cellule sélectionnée dans un UITableView. Il sera utilisé sur une instruction switch dans une vue différente. Comment puis-je le faire?Utiliser le texte d'une cellule dans UITableView

C'est le code pour le 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; 
} 

Ceci est le code de la vue qui utilisera le texte de la cellule UITableView (en ce moment, je suis en utilisant le nombre de cellules dans un changer la déclaration, mais je veux utiliser le contenu de la cellule au lieu du nombre de cellules):

- (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]; 
} 

Je sais que: _itemNumber = [[NSUserDefaults standardUserDefaults] int egerForKey: @ "Cell"]; Utilisera le numéro de la cellule, comment puis-je utiliser le contenu?

+0

S'il vous plaît, retirez les adresses e-mail réel et les numéros de téléphone de votre question! – Jacco

Répondre

1

C'est en fait une très mauvaise approche pour faire des choses comme ça. Je vous suggère de stocker des données dans NSArray avec plusieurs NSDictinary, qui contiendra le nom d'utilisateur/téléphone/email. Mappez ensuite chaque objet de NSArray en vue tableau, de sorte que la cellule avec un certain index aura des données dans un tableau avec le même index.

+0

à droite, mais j'utilise également un searchBar et un NSMutableArray pour afficher les éléments recherchés. _displayItems = [[NSMutableArray alloc] initWithArray: _list]; et crée une erreur logique avec l'élément de tableau, car lorsque la liste des éléments devient plus courte (lors d'une recherche), le texte n'est pas dans la même position de tableau. – user1330343

0

Utilisez un NSArray avec plusieurs instances NSDictionary comme magasin de données (ne chargez pas votre table directement à partir de ce NSArray). C'est ce que suggère Ivan Alek dans une autre réponse. Puis, stockez dans un NSMutableArray les index (NSNumbers) des instances NSDictionary correspondant à la recherche. De cette façon, votre magasin de données de base ne sera pas modifié par la barre de recherche, seulement ce tableau d'indices.

Quelques exemples de 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]; 
} 
+0

Je suis très nouveau à ce sujet. Comment créeriez-vous un dictionnaire? – user1330343

+0

NSDictionary * entry1 = [[NDDictionary alloc] initWithObjectsAndKeys: @ "233-123-2133", @ "téléphone", @ "[email protected]", @ "email", @ "Centre Adv. Acdémique", @ " titre ", néant]; Pour accéder au titre (par exemple) de l'objet que vous faites [entry1 objectForKey: @ "title"]; – JonathanC

+0

@ user1330343 a fait ce travail pour vous – JonathanC

Questions connexes