Objective c 选中时,将单元格标题设置为粗体

Objective c 选中时,将单元格标题设置为粗体,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我在更复杂的模态对话框中内置了一个tableview。在显示对话框之前,我在外部提供所选单元格的索引,并在tv:willDisplayCell:中进行对话框处理,以便正确的单元格具有粗体字体。但当对话框最终弹出时,我需要允许在选择其他行时也更改此选项。 我可能遗漏了什么,但我该怎么做呢?如何将所选单元格标题字体设置为粗体?您可以使用适当的委托方法更改字体: - (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPa

我在更复杂的模态对话框中内置了一个tableview。在显示对话框之前,我在外部提供所选单元格的索引,并在tv:willDisplayCell:中进行对话框处理,以便正确的单元格具有粗体字体。但当对话框最终弹出时,我需要允许在选择其他行时也更改此选项。

我可能遗漏了什么,但我该怎么做呢?如何将所选单元格标题字体设置为粗体?

您可以使用适当的委托方法更改字体:

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *c = [tv cellForRowAtIndexPath:ip];
    c.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; // for example
}

要使此功能强大,您需要将所选单元格的索引存储在
tableView:didSelectRowAtIndexPath:
中,而不仅仅是更新单元格

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSIndexPath *previousIndexPath = self.selectedIndexPath;
    self.selectedIndexPath = indexPath;
    [tableView reloadRowsAtIndexPaths:@[ previousIndexPath, indexPath]
                     withRowAnimation:UITableViewRowAnimationNone];
}
现在,先前选择的单元格和新选择的单元格都将被重新加载,如果您在
cellforrowatinexpath:
中使用某种类型的

if ([indexPath isEqual:self.selectedIndexPath]) {
  // bold
} else {
  // not bold
}
这比在
tableView:didSelectRowAtIndexPath:
中简单地更新单元格更可靠的原因是,如果您在单元格返回屏幕时将其从屏幕上滚动,它将正确高亮显示。在这里,我们正在更新模型,而不仅仅是视图