【发布时间】:2023-03-21 18:39:01
【问题描述】:
我有一个 UITable,它按通用名称和科学名称的字母顺序对单元格进行排序。 UITable 的每个单元格都有两个垂直堆叠的标签,分别对应物种的通用名和学名。当用户选择不同的排序值时,我希望标签的文本发生变化;即选择“通用名”时,最上面的标签应该是物种通用名。
在cellForRowAtIndexPath 中,我通过调用setSpeciesImage 来设置单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"speciesCell";
SpeciesCell* speciesCell = (SpeciesCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Species *theSpecies = [speciesFetchedResultsController objectAtIndexPath:indexPath];
[speciesCell setSpeciesImage:theSpecies usingImageType:currentImageType sortBy:sortDescriptor];
speciesCell.checkMark.hidden = YES;
return speciesCell;
}
在setSpeciesImage 中,我根据排序描述符更新标签文本。然而,什么都没有改变。我的标签保持不变。
- (void)setSpeciesImage:(Species *)species usingImageType:(NSInteger)imageType sortBy:(NSInteger)sortBy
{
switch (sortBy)
{
case kSortByCommonNameFirst:
_primaryLabel.text = [species commonNameFirstLast];
_secondaryLabel.text = species.scientificName;
self.isPrimaryLabelItalic = NO;
self.isSecondaryLabelItalic = YES;
break;
case kSortByCommonNameLast:
_primaryLabel.text = [species commonNameLastFirst];
_secondaryLabel.text = species.scientificName;
self.isPrimaryLabelItalic = NO;
self.isSecondaryLabelItalic = YES;
break;
case kSortByScientificName:
_primaryLabel.text = species.scientificName;
_secondaryLabel.text = [species commonNameFirstLast];
self.isPrimaryLabelItalic = YES;
self.isSecondaryLabelItalic = NO;
break;
default:
_primaryLabel = nil;
_secondaryLabel = nil;
self.isPrimaryLabelItalic = NO;
self.isSecondaryLabelItalic = NO;
break;
}
NSString *imagePath = nil;
switch (imageType)
{
case kImageTypeLeaf:
imagePath = [species.ExampleImageLeaf pathForLocalImageUsingThumbnail:YES];
break;
case kImageTypeFlower:
imagePath = [species.ExampleImageFlower pathForLocalImageUsingThumbnail:YES];
break;
case kImageTypeFruit:
imagePath = [species.ExampleImageFruit pathForLocalImageUsingThumbnail:YES];
break;
default:
_speciesImage = nil;
break;
}
【问题讨论】:
标签: ios objective-c uitableview uilabel