OGeek|极客世界-中国程序员成长平台

标题: ios - 在我的 UITableView 中选择一个单元格时,选择似乎是随机的 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:03
标题: ios - 在我的 UITableView 中选择一个单元格时,选择似乎是随机的

我有一个以编程方式创建的 UITableView 以及单元格,它显示了一个字体列表,用户可以从中选择以更改他们阅读时使用的字体。但是,在点击表格 View 中的单元格时,与委托(delegate)一起返回的字体系列不是单元格中显示的字体。点击“Times New Roman”一分钟会将其更改为 Helvetica,然后将其更改为 Baskerville。

以下是相关代码:

- (void)tableViewUITableView *)tableView didDeselectRowAtIndexPathNSIndexPath *)indexPath {
    // Depending on which row (font) they selected, use the delegate to change the
    // reading text to that font
    switch (indexPath.row) {
        case 0:
            [self.delegate changeFontTo"Helvetica"];
            break;
        case 1:
            [self.delegate changeFontTo"Times New Roman"];
            break;
        case 2:
            [self.delegate changeFontTo"Baskerville"];
            break;
        case 3:
            [self.delegate changeFontTo"OpenDyslexic"];
            break;
    }
}

还有委托(delegate)方法:

- (void)changeFontToNSString *)fontFamily {
    self.textToReadLabel.font = [UIFont fontWithName:fontFamily size:self.textToReadLabel.font.pointSize];
}

如果有帮助,这里是用于创建行的 UITableView 委托(delegate)方法:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"FontCell";

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    int row = indexPath.row;

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.fontFamilyLabel.text = self.fonts[row];

    if ([self.fonts[row] isEqualToString"Helvetica"]) {
        cell.fontFamilyLabel.font = [UIFont fontWithName"Helvetica" size:17.0];
    }
    else if ([self.fonts[row] isEqualToString"Times New Roman"]) {
        cell.fontFamilyLabel.font = [UIFont fontWithName"Times New Roman" size:17.0];
    }
    else if ([self.fonts[row] isEqualToString"Baskerville"]) {
        cell.fontFamilyLabel.font = [UIFont fontWithName"Baskerville" size:17.0];
    }
    else if ([self.fonts[row] isEqualToString:@"Dyslexic"]) {
        cell.fontFamilyLabel.font = [UIFont fontWithName:@"OpenDyslexic" size:17.0];
    }

    return cell;
}

真的很困惑它可能是什么。我把代码弄得一头雾水,但我觉得这可能只是一个我不明白的概念导致了问题。



Best Answer-推荐答案


您在委托(delegate)中使用了 didDeselectRowAtIndexPath 而不是 didSelectRowAtIndexPath

关于ios - 在我的 UITableView 中选择一个单元格时,选择似乎是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15788024/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4