2

私はプログラムで作成した UITableView をセルと一緒に持っています。これは、ユーザーが読むフォントを変更するために選択できるフォントのリストを表示します。ただし、テーブルビューでセルをタップすると、デリゲートと共に返されたフォント ファミリは、セルに表示されるフォントではありません。「Times New Roman」を 1 分間タップすると 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

didSelect の代わりに didDeselect を使用しているようです。

于 2013-04-03T13:06:43.393 に答える