私はプログラムで作成した 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;
}
それが何であるかについて本当に混乱しています。私はコードをいじくり回しましたが、問題を引き起こしているのは理解できない概念にすぎないように感じます。