我有一個UITableView使用自定義單元格,從nibs加載,並連接到控制器(使用initWithNibName:bundle :)。基本上,我注意到dequeueReusableCellWithIdentifier返回一個已經加載的單元格,但是它不應該。dequeueReusableCellWithIdentifier返回錯誤的單元格
這裏是我的委託方法:
- (UITableViewCell *) tableView: (UITableView *) tv cellForRowAtIndexPath: (NSIndexPath *) indexPath {
return [[self controllerForTableView: tv cellForRowAtIndexPath: indexPath] cell];
}
- (TableViewCellBaseController *) controllerForTableView: (UITableView *) tv cellForRowAtIndexPath: (NSIndexPath *) indexPath {
[self checkValidTableView: tv];
UIViewController *controller;
Class class;
Event *event;
int row = [indexPath row];
DLOG(@"at index path row: %i", row);
if (row == [currentEvents count]) {
controller = [self tableCellContainerFromNibName: nibName
tableView: tv
atIndex: row
withClass: class];
} else {
class = [EventFeaturedTableViewCell class]; // TODO: OR class = [EventNonFeaturedTableViewCell class];
event = [self findEventFromIndex: row];
lastSelectedEvent = event;
DLOG(@"event: %@", event);
controller = [self tableCellContainerFromNibName: NSStringFromClass(class)
tableView: tv
atIndex: row
withClass: class
perform: @selector(initTableCellWithController:)
on: self];
}
return controller;
}
- (TableViewCellBaseController *) tableCellContainerFromNibName: (NSString *) nibName
tableView: (UITableView *) tableView
atIndex: (int) index
withClass: (Class) class
perform: (SEL) selector
on: obj {
CustomTableViewCell *cell = (CustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier: nibName];
TableViewCellBaseController *controller;
DLOG(@"Cell: %@", cell);
DLOG(@"Cell Identifier: %@", nibName);
if (cell == nil) {
controller = [[class alloc] initWithNibName: nibName bundle: nil];
if (obj) {
[obj performSelector: selector withObject: controller];
}
// run this after setting data as controller.view eager loads the view
cell = controller.cell = controller.view;
cell.controller = controller;
} else {
controller = cell.controller;
if (obj) {
[obj performSelector: selector withObject: controller];
}
}
return controller;
}
- (void) initTableCellWithController: (EventsIndexTableViewCell *) controller {
controller.event = lastSelectedEvent;
}
自定義細胞在IB被掛接到使用控制器的視圖屬性,以及一個「細胞」財產控制器。自定義單元格具有直接在筆尖中設置的唯一標識符。
我已經驗證控制器中的數據是正確的。
這裏是我看到在模擬器的效果:
http://screencast.com/t/NI2Tpc7GKEi
注意事件「大西洋灣」首先在表格後面顯示了在表中,然後。當向後滾動時,它不再顯示爲表格中的第一個條目!
但是,控制器的setEvent:方法已被覆蓋,以修改單元格內容或不是?因爲如果答案不是,你基本上改變出列的單元格結構(cell.controller)而不是內容。考慮一下當控制器Nib被加載並創建了它的視圖,並且如果你沒有使用控制器的setEvent顯式更新它時,單元格結構會被創建一次:我沒有看到代碼中的其他位置。表中的第一個單元格很好,因爲每次都調用Nibs,直到系統開始重用隊列單元爲止。 – viggio24 2011-02-08 22:18:40
嗨viggio24 - 請參閱下面Rob的帖子下的評論。 – smtlaissezfaire 2011-02-09 16:45:28