我有一個UITableView,它與單元一起編程創建,顯示用戶可以選擇更改字體的字體列表。但是,在輕擊tableview中的單元格時,與委託單元一起發回的字體族不是單元格中顯示的字體。一分鐘敲擊「Times New Roman」會將其改爲Helvetica,接下來它會將其改爲Baskerville。在我的UITableView中選擇一個單元格時,選擇看起來是隨機的
下面是相關代碼:
- (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;
}
真的很困惑,它可能是什麼。我混淆並混淆了代碼,但我覺得它可能只是一個我不明白導致問題的概念。
'tableView:didDeselectRowAtIndexPath:'代替'tableView:didSelectRowAtIndexPath:'可能會導致這種情況。 – Zen