2012-12-09 68 views
1

我在更復雜的模式對話框中內置了tableview。在顯示對話框之前,我在外部提供選定單元格的索引,並在電視中提供對話框過程:willDisplayCell:以便右側單元格具有粗體字體。但是當對話框最終彈出時,我需要允許這也改變,因爲我選擇了其他行。
我可能錯過了一些東西,但我該怎麼做?我將如何將選定的單元格標題字體設置爲粗體?選中時將單元格標題設置爲粗體

回答

1

您可以在相應的委託方法更改字體:

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)ip 
{ 
    UITableViewCell *c = [tv cellForRowAtIndexPath:ip]; 
    c.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; // for example 
} 
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:中的單元格更強大,原因是因爲如果在單元格重新打開時將單元格從屏幕上滾動出來,它將被正確突出顯示。在這裏,我們正在更新模型,而不僅僅是視圖。

相關問題