1
我最近下載了蘋果的示例應用程序CoreDataBooks和學習的目的決定細胞添加到最後一個記錄在RootViewController.m表視圖,這將顯示的記錄數那些被提取。CoreDataBooks添加新小區的tableview
我添加的代碼如下所示。我沒有達到添加讀取計數的目的,因爲我的代碼得到了不好的結果。如果您下載CoreDataBooks並用此處的代碼替換表格視圖數據源方法,您將看到以下內容:
- 新的單元格被添加到表格視圖的底部。
- 當您滾動回到表格的頂部時,另一個單元格接收的格式只應該提供給表格的最後一個單元格。就我而言,在「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 @"";
}
這就是全部。感謝您的時間。
非常好!有道理,效果很好。我選擇了#1。 謝謝你。 – CraigH