2012-07-27 58 views
0

這個問題是關於cellForRowAtIndexPath。cellForRowAtIndexPath:XCode模板是否正確?

當我知道正確的方法是

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) { 
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"]; 
return cell; 

但是,當我創建主從模板,代碼看起來像這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
[self configureCell:cell atIndexPath:indexPath]; 
return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
cell.textLabel.text = [[object valueForKey:@"timeStamp"] description]; 
} 

編輯:現在的問題是不是configureCell atIndexPath。正如您在XCode模板中看到這些字符串不存在一樣。

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:cellIdentifier] ; 

是XCode的模板是否正確?

+0

檢查這個,它會給你答案:http://stackoverflow.com/questions/5467831/why-do-programmers-use-configurecellatindexpath-method-to-config-the-tableview – Dhruv 2012-07-27 09:45:03

回答

4

模板代碼是正確的。

從iOS 5.0開始,可以爲單元標識符註冊一個筆尖。無論何時表需要一個單元並且沒有可重用的單元,它都會實例化該單元並自行創建單元。

如果你這樣做dequeueReusableCellWithIdentifier:總是返回一個單元;你不必自己創建它。

註冊筆尖的API是:registerNib:forCellReuseIdentifier: