TableView 单元格不显示信息
Posted
技术标签:
【中文标题】TableView 单元格不显示信息【英文标题】:TableView Cells not displaying info 【发布时间】:2014-05-07 17:25:27 【问题描述】:我的设置: 我有一个 UIViewController,其中插入了一个 TableView。同样在 viewController 我有一个文本字段和一个滑块。我正在尝试从我的文本字段中保存数据,存储它,然后让获取请求使用放置在 textField 中的任何信息填充单元格。但是,当我点击保存按钮时,tableCell 中没有任何内容。下面的 .m 文件属于我的 UIVeiwController。任何关于为什么单元格没有填充的帮助都会很好。谢谢!
#import "BottleViewController.h"
#import "LogEntry.h"
#import "CoreDataStack.h"
@interface BottleViewController () <NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *ouncesLabel;
@property (strong, nonatomic) UISlider *slider;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *entryAmount;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end
@implementation BottleViewController
- (void)viewDidLoad
[super viewDidLoad];
[self.fetchedResultsController performFetch:nil];
-(void)dismissSelf
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
-(void)insertLogEntry
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
LogEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"LogEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
entry.amount = self.slider.value;
entry.date = [[NSDate date] timeIntervalSince1970];
entry.testString = self.textField.text;
[coreDataStack saveContext];
- (IBAction)backWasPressed:(id)sender
[self dismissSelf];
- (IBAction)mySlider:(id)sender
self.slider = (UISlider*)sender;
NSString *newText = [[NSString alloc] initWithFormat:@"%d", (int)self.slider.value];
self.ouncesLabel.text = newText;
- (IBAction)saveWasPressed:(id)sender
[self.view endEditing:YES];
[self insertLogEntry];
-(NSFetchRequest *)entryListFetchRequest
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"LogEntry"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
return fetchRequest;
-(NSFetchedResultsController *)fetchedResultsController
if(_fetchedResultsController != nil)
return _fetchedResultsController;
CoreDataStack *coreDataStack =[CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self entryListFetchRequest];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
#pragma mark UITableViewDelegates
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return self.fetchedResultsController.sections.count;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
id<NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
LogEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = entry.testString;
return cell;
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
[self.tableView reloadData];
@end
【问题讨论】:
【参考方案1】:没关系。我忘了连接数据源和委托。
【讨论】:
以上是关于TableView 单元格不显示信息的主要内容,如果未能解决你的问题,请参考以下文章
我在模拟器中运行应用程序后,xcode 中的原型单元格不显示