2

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

以下是相关代码:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)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;
    }
}

和委托方法:

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
}

真的很困惑它可能是什么。我对代码感到困惑和困惑,但我觉得这可能只是一个我不明白的概念导致了问题。

4

3 回答 3

1

您使用didDeselectRowAtIndexPath而不是didSelectRowAtIndexPath在您的委托中。

于 2013-04-03T13:02:10.490 回答
0

检查您期望的“didSelect”的方法名称是否可能以某种方式意外地得到“didDeselect”。

于 2013-04-03T13:04:38.273 回答
0

似乎您使用的是 didDeselect 而不是 didSelect。

于 2013-04-03T13:06:43.393 回答