Avoir du mal à remplacer les propriétés UITableViewcell
-
12-11-2019 - |
Question
J'ai une table sur mesure que j'ai créée. Chaque cellule a deux uilabels que je veux être des couleurs différentes. De plus, j'ai une section de ma table de table que je veux avoir une couleur d'arrière-plan et une couleur de texte différente.
Pour le moment, je ne peux pas faire changer la couleur du texte en utilisant mon uilabel.textColor; Bien que je l'ai fait fonctionner avec Cell.TextLabel.TextColor. La couleur de la cellule de la cellule ne fonctionne pas non plus.
J'ai pu faire fonctionner les modifications de la propriété cellulaire si je le faisais à l'intérieur de ma déclaration de commutation en bas, mais les propriétés cellulaires ne se déshabilleraient pas correctement, alors que je faisait défiler les cellules aléatoires de haut en bas deviendrait du texte blanc sur fond rouge.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyIdentifier";
UILabel* itemLabel;
UILabel* amountLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
itemLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 35.0)] autorelease];
itemLabel.tag = ITEMLABEL_TAG;
itemLabel.font = [UIFont systemFontOfSize:14.0];
itemLabel.textAlignment = UITextAlignmentLeft;
itemLabel.autoresizingMask = UIViewAutoresizingNone;
itemLabel.backgroundColor = [UIColor clearColor];
itemLabel.textColor = [UIColor blueColor]; //This doesn't work
if (indexPath.section == 4)
{
cell.textLabel.textColor = [UIColor whiteColor]; //This works
cell.backgroundColor = [UIColor redColor]; //This doesn't work
}
[cell.contentView addSubview:itemLabel];
cell.backgroundColor = [UIColor whiteColor];
amountLabel = [[[UILabel alloc]initWithFrame:CGRectMake(225.0, 0.0, 55.0, 35.0)] autorelease];
amountLabel.tag = AMOUNTLABEL_TAG;
amountLabel.font = [UIFont systemFontOfSize:14.0];
amountLabel.textAlignment = UITextAlignmentLeft;
amountLabel.textColor = [UIColor blackColor]; //This doesn't work
amountLabel.backgroundColor = [UIColor clearColor];
amountLabel.autoresizingMask = UIViewAutoresizingNone;
[cell.contentView addSubview:amountLabel];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [monthlyExpenses objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [oneTimeFees objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [renters objectAtIndex:indexPath.row];
break;
case 3:
cell.textLabel.text = [travelExpenses objectAtIndex:indexPath.row];
break;
case 4:
cell.textLabel.text = @"Generate Report";
break;
}
return cell;
}
La solution
Vous devrez modifier la couleur d'arrière-plan de la cellule dans un - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
C'est là que vous pouvez faire la personnalisation juste avant que le tableau n'affiche la cellule.