我有一个以编程方式创建的 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;
}
真的很困惑它可能是什么。我对代码感到困惑和困惑,但我觉得这可能只是一个我不明白的概念导致了问题。