2009-12-21 5 views
0

Je dois créer une vue de paramètres personnalisés utilisée dans l'application. L'application est une maquette donc elle en a déjà une, elle utilise un UITableViewCellStyleValue1, mais rien n'est éditable. Donc essentiellement ce que je voudrais avoir est la même chose, mais avec detailTextLabel éditable (l'étiquette sur le côté droit), quelle est la meilleure façon de le faire?comment créer une vue personnalisée des paramètres/créer une étiquette de cellule modifiable

Répondre

2

Vous pouvez continuer à utiliser UITableViewCellStyleValue1. Vous devez créer des instances UITextField pour chaque cellule que vous souhaitez modifier. Ensuite, vous affectez chaque UITextField à la propriété accessoryView de la cellule appropriée.

Vous pouvez également correspondre à la couleur du texte de l'étiquette via:

yourTextField.textColor = cell.detailTextLabel.textColor; 

Vous pouvez avoir besoin de jouer avec la propriété cadre des instances de UITextField pour obtenir l'alignement juste.

0

OK, voici le code que je suis maintenant, le champ de texte n'apparaît pas, je ne sais pas pourquoi ...

  - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
static NSString* reuseIdentifier = @"SettingsCell"; 

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

if (!cell) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease]; 
    [cell setBackgroundColor:[UIColor baeDarkGrayColor]]; 


    UILabel* cellLabel = [cell textLabel]; 
    [cellLabel setTextColor:[UIColor whiteColor]]; 
    [cellLabel setBackgroundColor:[UIColor clearColor]]; 

    [cell setUserInteractionEnabled:YES]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
} 

// Populate cell with corresponding data. 
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section]; 

// Set the label 
UILabel* textLabel = [cell textLabel]; 
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row]; 
[textLabel setText:labelString]; 

// Set the detail string 
// UILabel* detailLabel = [cell detailTextLabel]; 
// NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row]; 
// [detailLabel setText:detailLabelString]; 

// Set the accessory view 
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue]; 
switch (accessoryType) 
{ 
    case BAESettingsTableCellAccessoryDisclosureIndicator: 
    { 
     UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]]; 
     [cell setAccessoryView:disclosureView]; 
     [disclosureView release]; 
     break; 
    } 
    case BAESettingsTableCellAccessoryOnOffSwitch: 
    { 
     UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [onOffSwitch setOn:YES]; 
     [cell setAccessoryView:onOffSwitch]; 
     [onOffSwitch release]; 
     break; 
    } 
    case BAESettingsTableCellAccessoryNone: 
     // default: 
     // break; 
    { 
     UITextField* textField = [[UITextField alloc] init]; 
     NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row]; 
     textField.text = detailLabelString; 
     textField.textColor = [UIColor whiteColor]; 
     [cell setAccessoryView:textField]; 
     [textField release]; 
     break; 
    } 

} 

return cell; 

}

0

Votre champ de texte n'apparaît pas parce que vous ne jamais définir le cadre du champ de texte. Vous devez définir le cadre avec une largeur et une hauteur positives. La hauteur que j'utiliserais est de 43 (hauteur de ligne - 1px buffer de ligne grise).

0

Aha! OK j'ai compris! Maintenant, ce que j'essaye de faire est de définir dynamiquement la largeur du champ de texte de détail en regardant la largeur de l'étiquette de titre laissée dans la cellule. La plupart du temps je lance ceci dans le simulateur la taille de l'étiquette de titre est 0, mais de temps en temps j'obtiens un résultat. Cela n'a aucun sens pour moi, je pense que c'est peut-être parce que les cellules sont réutilisées? Avez-vous des idées?

J'ajouté ce morceau de code après Textlabel est défini:

CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font]; 

Et puis dans le bloc de cas au début je fais:

CGRect cellFrame = [cell frame]; 

     float detailTextFieldWidth = cellFrame.size.width - (textLabelSize.width + 50); 
     float detailTextFieldHeight = cellFrame.size.height - 1; 

     NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight); 


     CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight); 


     UITextField* textField = [[UITextField alloc] initWithFrame:frame]; 
2

Je pense que la façon plus facile de le faire est d'utiliser detailTextLabel pour afficher le texte et gérer l'affichage et la masquage des champs d'édition de texte dans le code de sélection de ligne. Je possède ce travail avec les éléments suivants:

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITextField *tmpView = [self detailLabel:indexPath]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UILabel *detailLabel = [cell detailTextLabel]; 

    CGRect tmpFrame = [detailLabel frame]; // the existing frame 
    tmpFrame.size.width += 3; // space for the cursor 
    tmpView.frame = tmpFrame; 
    tmpView.textColor = detailLabel.textColor; 
    cell.accessoryView = tmpView; 
    detailLabel.text = nil; 

    [tableView addSubview:tmpView]; 
    [tmpView becomeFirstResponder]; 

    [tableView setNeedsDisplay]; 
} 
 

et ce qui suit sur le Désélectionnez

 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UITextField *tmpView = (UITextField *) cell.accessoryView; 
    UILabel *label = [cell detailTextLabel]; 

    label.text = tmpView.text; 
    cell.accessoryView = nil; 

    [tableView setNeedsDisplay]; 
} 
 

La seule autre astuce consistait à définir la sélection de ligne mettant en évidence dans le cellForRowAtIndexPath sans pareil ..

 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
Questions connexes