點擊 Start 按鈕後,NSTableView 從第一列開始逐行高亮選取,每列停留數秒,類似播放清單的當前播放指示器。
@property (strong) NSTimer *highlightTimer;
@property (assign) NSInteger currentRow;
- (IBAction)startHighlight:(id)sender {
self.currentRow = 0;
self.highlightTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 // 每列停留 2 秒
target:self selector:@selector(highlightNextRow:) userInfo:nil repeats:YES];
}
- (void)highlightNextRow:(NSTimer *)timer {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:self.currentRow];
[self.tableView selectRowIndexes:indexSet byExtendingSelection:NO];
[self.tableView scrollRowToVisible:self.currentRow]; // 確保可見
self.currentRow++;
if (self.currentRow >= [self.tableView numberOfRows]) {
[timer invalidate];
self.highlightTimer = nil;
}
}
selectRowIndexes:byExtendingSelection: 第二參數 NO 表示單選模式,取消先前選取。scrollRowToVisible: 防止高亮行超出可視區域。viewDidDisappear 或 dealloc 中 [timer invalidate] 避免洩漏。實作 NSTableViewDelegate 的 tableView:rowViewForRow: 回傳自訂 NSTableRowView,覆寫 drawSelectionInRect: 繪製漸層、脈動動畫等效果。