1

我在更复杂的模式对话框中内置了一个 tableview。在显示对话框之前,我在外部提供了所选单元格的索引,并在 tv:willDisplayCell: 中提供了对话框进程,以便正确的单元格具有粗体字体。但是当对话框最终弹出时,我需要允许它在我选择其他行时也发生变化。
我可能遗漏了一些东西,但我该怎么做呢?如何将选定的单元格标题字体设置为粗体?

4

2 回答 2

1

您可以在适当的委托方法中更改字体:

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *c = [tv cellForRowAtIndexPath:ip];
    c.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; // for example
}
于 2012-12-09T21:20:20.767 回答
1

为了使它健壮,您需要将所选单元格的索引存储在tableView:didSelectRowAtIndexPath:不只是更新单元格中。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSIndexPath *previousIndexPath = self.selectedIndexPath;
    self.selectedIndexPath = indexPath;
    [tableView reloadRowsAtIndexPaths:@[ previousIndexPath, indexPath]
                     withRowAnimation:UITableViewRowAnimationNone];
}

现在,先前选择的单元格和新选择的单元格都将被重新加载,并且假设您cellForRowAtIndexPath:使用某种方式执行此操作,您当前的使文本变为粗体的实现应该可以工作

if ([indexPath isEqual:self.selectedIndexPath]) {
  // bold
} else {
  // not bold
}

这比简单地更新单元格更强大的原因tableView:didSelectRowAtIndexPath:是,如果您将单元格滚动到屏幕之外,当它重新打开时,它将正确突出显示。在这里,我们不仅更新了视图,还更新了模型。

于 2012-12-09T23:26:50.530 回答