更新排序UITable上的标签文本

时间:2018-03-07 21:38:38

标签: ios objective-c uitableview uilabel

我有一个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;
    }

1 个答案:

答案 0 :(得分:0)

segmentedControl选择操作触发时,您应该调用

 [self.tableView reloadData];