2009-10-15 16 views
1

我最近下載了蘋果的示例應用程序CoreDataBooks和學習的目的決定細胞添加到最後一個記錄在RootViewController.m表視圖,這將顯示的記錄數那些被提取。CoreDataBooks添加新小區的tableview

我添加的代碼如下所示。我沒有達到添加讀取計數的目的,因爲我的代碼得到了不好的結果。如果您下載CoreDataBooks並用此處的代碼替換表格視圖數據源方法,您將看到以下內容:

  1. 新的單元格被添加到表格視圖的底部。
  2. 當您滾動回到表格的頂部時,另一個單元格接收的格式只應該提供給表格的最後一個單元格。就我而言,在「Bill Bryson」的作者部分下,當您滾動回到表格的頂部時,書名「來自大國的筆記」變爲藍色並居中。

任何幫助解決此問題將不勝感激。

這裏是一個示例應用程序CoreDataBooks的鏈接link text

這裏是RootViewController.m修改後的代碼:

/* 
The data source methods are handled primarily by the fetch results controller 
*/ 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [[fetchedResultsController sections] count] + 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 
if (section < [[fetchedResultsController sections] count]) 
{ 
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

return 1; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
NSLog(@"indexPath.section: %i",indexPath.section); 
if (indexPath.section < [[fetchedResultsController sections] count]){ 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

// Configure the cell. 
[self configureCell:cell atIndexPath:indexPath]; 
} 
else{ 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
cell.textLabel.text = (@"This is a NEW Cell..."); 
cell.textLabel.textAlignment = UITextAlignmentCenter; 
cell.textLabel.textColor = [UIColor blueColor]; 
} 
return cell; 
} 


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

// Configure the cell to show the book's title 
Book *book = [fetchedResultsController objectAtIndexPath:indexPath]; 
cell.textLabel.text = book.title; 
} 


- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section { 
// Display the authors' names as section headings. 
    if (section < [[fetchedResultsController sections] count]) 
    return [[[fetchedResultsController sections] objectAtIndex:section] name]; 

    return @""; 

} 

這就是全部。感謝您的時間。

回答

3

我看到兩個解決方案:

  1. 使用不同的小區標識爲您的細胞。

爲此,定義一個新的小區標識符的另一後和移動兩個不同的細胞的情況下在細胞內複用的代碼:

static NSString *CellIdentifier = @"Cell"; 
static NSString* myCellIdentifier = @"MyCell"; // new cell identifier 

// move inside two cell cases 
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
NSLog(@"indexPath.section: %i",indexPath.section); 
if (indexPath.section < [[fetchedResultsController sections] count]){ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  // only reuse this type of cell here 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 
[self configureCell:cell atIndexPath:indexPath]; 
} 
else{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here 

if (cell == nil) { 

2。配置單元時覆蓋所有適用的格式:

cell.textLabel.text = (@"This is a NEW Cell..."); // You are already overriding the only cell attribute that the book cell configures 
cell.textLabel.textAlignment = UITextAlignmentCenter; 
cell.textLabel.textColor = [UIColor blueColor]; 
} 
return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

// Configure the cell to show the book's title 
Book *book = [fetchedResultsController objectAtIndexPath:indexPath]; 
cell.textLabel.text = book.title; 
cell.textLabel.textAlignment = UITextAlignmentLeft; // I assume Left is the default alignment 
cell.textLabel.textColor = [UIColor blackColor]; // I assume black is the default color 
} 
+0

非常好!有道理,效果很好。我選擇了#1。 謝謝你。 – CraigH