我開發了一個OS X 10.9和OS X 10.10的應用程序。這個程序有2視圖:在NSTableCellView上的鼠標懸停隨機
- 登錄查看
- 歷史視圖
在登錄鑑於用戶應該只輸入用戶名和密碼。當用戶按下登錄按鈕時,它會調用Web服務來授權用戶查看新聞。
當用戶登錄時,應用程序顯示歷史視圖,其中存儲了所有發送的新聞。爲了回顧這個消息,它調用了一個Web服務來獲得一個包含所有新聞的歷史的JSON。要顯示所有消息,我使用了NSTableView
。
當用戶將鼠標移動到此行的這一行上時,該行的背景變爲灰色。
所有的工作相當不錯,但保持更新新聞列表我來調用Web服務來獲取歷史信息每隔10秒鐘,這樣我把下面的代碼在我HistoryViewController
:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(sendRequestToUrl) userInfo:nil repeats:YES];
其中sendRequestToUrl
是從Web服務獲取歷史記錄的方法。所以每次重新載入新聞列表時,都會顯示一行結束,而沒有鼠標懸停。爲什麼?
要繪製鼠標滑過我在自定義單元格類插入下面的代碼:
#import "HistoryCustomCell.h"
@implementation HistoryCustomCell {
NSTrackingArea *_trackingArea;
NSEvent *event;
}
- (void)awakeFromNib {
[self createTrackingArea];
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (void)createTrackingArea {
_trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited|NSTrackingActiveInActiveApp owner:self userInfo:nil];
[self addTrackingArea:_trackingArea];
NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
mouseLocation = [self convertPoint:mouseLocation fromView:nil];
if (NSPointInRect(mouseLocation, [self bounds])) {
[self mouseEntered:nil];
} else {
[self mouseExited:nil];
}
}
- (void)mouseEntered:(NSEvent *)theEvent {
[self.onMouseOverImage setImage:[NSImage imageNamed:@"mouse_over_back_cell.png"]];
}
- (void)mouseExited:(NSEvent *)theEvent {
[self.onMouseOverImage setImage:[NSImage imageNamed:nil]];
}
@end
任何想法,爲什麼它不工作的罰款?
謝謝
謝謝,你用'-prepareForReuse'保存了我的生活 – lucgian841