【问题标题】:Update text of label on sort UITable更新排序 UITable 上的标签文本
【发布时间】: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


    【解决方案1】:

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

     [self.tableView reloadData];
    

    【讨论】:

    • 我不使用分段控件进行排序
    • 你发送 sortBySegmentedControl.selectedSegmentIndex 到 sortBy
    • 除了排序数据逻辑应该在单元格之外的任何地方然后你根据最后的选择重新加载已经排序的数据的表格
    • 刚刚更新了我的文本,以便我通过 sortDescriptor,而不是 sortBySegmentedControl.selectedSegmentIndex(这是旧代码,实际上不正确)。那我应该在cellForRowAtIndexPath 的末尾打电话给[self.tableview reloadData] 吗?
    • 不,这永远不会造成无限循环,我想知道是什么触发了用户单击以使用不同值进行排序的排序???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2023-03-09
    相关资源
    最近更新 更多