Je suis assez loin avec celui-ci, mais je suis suspendu à la partie où j'ai lu les informations sur les chaînes.Problème lors de la détermination de la capacité multiligne UITableViewCell
J'ai fait une cellule qui reçoit des données d'un fichier XML externe, tout fonctionne très bien mais certaines cellules contiennent beaucoup de texte que je veux afficher sur plusieurs lignes. Aussi pas de problème. Mais la partie délicate est la hauteur dynamique de ma cellule. J'ai configuré ceci dans la méthode heightForRowAtIndexPath: mais j'ai besoin de connaître la quantité de texte (rangées) que la cellule contient, et je suis bloqué sur la partie comment la connecter à ma variable cellText (string). Toute aide serait la bienvenue :-)
Voici mon code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
// Set the text in the cell for the section/row.
NSString *endDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.end]];
int endDateLength = endDate.length;
NSString *endTime = [NSString stringWithFormat:@"%@", [endDate substringFromIndex:endDateLength -7]];
NSString *startDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
int startDateLength = startDate.length;
NSString *startTime = [NSString stringWithFormat:@"%@", [startDate substringFromIndex:startDateLength -7]];
NSString *date = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
int dateLength = date.length;
NSString *dateString = [NSString stringWithFormat:@"%@", [date substringToIndex:dateLength -7]];
NSString *cellText = nil;
NSString *cellExplainText = nil;
//Pre title arrays
dateTitleArray = [[NSArray alloc] initWithObjects:@"Dag", @"Start tijd", @"Eind tijd", nil];
nameTitleArray = [[NSArray alloc] initWithObjects:@"Naam", @"Graad",nil];
addressTitleArray = [[NSArray alloc] initWithObjects:@"Dojo", @"Straat", @"Plaats",nil];
infoTitleArray = [[NSArray alloc] initWithObjects:@"Kosten", @"Contact", @"Details", nil];
dateArray = [[NSArray alloc] initWithObjects: dateString, startTime, endTime, nil];
nameArray = [[NSArray alloc] initWithObjects: stage.teacher, stage.grade, nil];
addressArray = [[NSArray alloc] initWithObjects: stage.dojo, stage.street, stage.city, nil];
infoArray = [[NSArray alloc] initWithObjects: stage.cost, stage.contact, stage.details, nil];
switch (indexPath.section)
{
case 0:
cellExplainText = [dateTitleArray objectAtIndex:indexPath.row];
cellText = [dateArray objectAtIndex:indexPath.row];
break;
case 1:
cellExplainText = [nameTitleArray objectAtIndex:indexPath.row];
cellText = [nameArray objectAtIndex:indexPath.row];
break;
case 2:
cellExplainText = [addressTitleArray objectAtIndex:indexPath.row];
cellText = [addressArray objectAtIndex:indexPath.row];
break;
case 3:
cellExplainText = [infoTitleArray objectAtIndex:indexPath.row];
cellText = [infoArray objectAtIndex:indexPath.row];
break;
default:
break;
}
[dateTitleArray release];
[nameTitleArray release];
[addressTitleArray release];
[infoTitleArray release];
[dateArray release];
[nameArray release];
[addressArray release];
[infoArray release];
cell.textLabel.text = cellExplainText;
cell.detailTextLabel.text = cellText;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellTextSize = ????????????;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 12;
}
Salut merci pour l'aide jusqu'à présent. J'ai implémenté le code que vous m'avez fourni. Le bloc de texte ci-dessus. Mais la hauteur dynamique de la cellule ne change pas. Je vois toujours les 3 lignes/ou plus de texte étant pressé dans la cellule de taille standard. Il ne prend pas le nombre de lignes en compte. Aucune suggestion? – iJar
[code] (NSString * celltext; commutateur (indexPath.section) {case 0: celltext = [dateTitleArray objectAtIndex: indexPath.row]; break; cas 1: celltext = [nameTitleArray objectAtIndex: indexPath. ligne]; break; cas 2: celltext = [addressTitleArray objectAtIndex: indexPath.row]; break; cas 3: celltext = [infoTitleArray objectAtIndex: indexPath.row]; break; défaut: break; } ) ... – iJar
[code] (UIFont * cellFont = [UIFont fontWithName: @ "Helvetica" taille: 14.0]; CGSize constraintSize = CGSizeMake (280.0f, MAXFLOAT); CGSize labelSize = [celluleText sizeWithFont: cellFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap]; return labelSize.height + 12;) – iJar