所以我做了一个功能很好的小应用程序,我只是不确定一些最佳实践/效率。该应用程序有一个Core Data源,它被加载到表视图中。可以从文本字段将项添加到表视图中。问题出在我的TVViewController的[_tvds init]
方法中的行cellForRowAtIndexPath
。我认为当我向上和向下滚动表时,这一行将导致_tvds
实例变量重新实例化 - 这会导致性能不佳吗?我还是iOS和Objective C的新手,所以请随时给我指点如何重构我的代码。分离视图控制器和数据存储(使用Core Data方法等)似乎没有必要,但我试图练习我的MVC设计。这是代码(我遗漏了App Delegate和xib文件 - 它们是标准的东西)。该应用程序有效,但它对我的初学者来说似乎不是最佳选择。
TVViewController.h
#import <UIKit/UIKit.h>
#import "TVDataSource.h"
#import "TVAppDelegate.h"
@interface TVViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) TVDataSource *tvDS;
@property (weak, nonatomic) IBOutlet UITextField *addSomethingTextField;
- (IBAction)goButton:(id)sender;
@end
TVViewController.m
#import "TVViewController.h"
@interface TVViewController ()
@end
@implementation TVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_tvDS = [[TVDataSource alloc]init];
NSLog(@"%@",[_tvDS dataArray]);
NSLog(@"%@",[[_tvDS dataArray] valueForKey:@"name"]);
[_addSomethingTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
// If you're serving data from an array, return the length of the array:
return [[_tvDS dataArray] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set the data for this cell:
cell.textLabel.text = @"dfjklsl";
NSInteger myInt = [indexPath row];
[_tvDS init];
NSObject *myObj = [[_tvDS dataArray] objectAtIndex:myInt];
[[cell textLabel]setText:[myObj valueForKey:@"name"]];
// NSArray *toBeDisplayed = [_tvDS getAnArray];
// [[cell textLabel]setText:[toBeDisplayed objectAtIndex:myInt]];
// set the accessory view:
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (IBAction)goButton:(id)sender {
NSString *textFieldContents = [_addSomethingTextField text];
[[_tvDS dataArray] addObject:textFieldContents];
[_tvDS addItem:textFieldContents];
[_addSomethingTextField setText:@""];
[[self tableView] reloadData];
NSLog(@"%u",[[_tvDS dataArray] count]);
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self goButton:textField];
[_addSomethingTextField resignFirstResponder];
//NSLog(@"%@", [_addSomethingTextField text]);
return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSLog(@"%@", [[_tvDS dataArray] objectAtIndex:row]);
}
@end
TVDataSource.h
#import <Foundation/Foundation.h>
#import "TVAppDelegate.h"
@interface TVDataSource : NSObject
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) NSManagedObjectContext *context;
@property (nonatomic, retain) NSFetchRequest *request;
@property (nonatomic, retain) NSError *error;
//@property (nonatomic, strong) NSArray *obj
//@property (nonatomic, weak) NSManagedObjectContext *context;
//-(void)saveInput;
-(void)addItem:(id)input;
@end
TVDataSource.m
#import "TVDataSource.h"
@implementation TVDataSource
- (id)init
{
self = [super init];
TVAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Todo" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDesc];
// NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", [_name text]];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count]==0) {
NSLog(@"nothing there");
} else {
matches = objects[0];
[self setDataArray:[objects mutableCopy]];
NSLog(@"initialise %@",[self dataArray]);
//NSLog(@"%@",[_dataArray valueForKey:@"name"]);
}
return self;
}
-(void)addItem:(id)input
{
TVAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newTask;
newTask = [NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:context];
[newTask setValue:input forKey:@"name"];
if ([input isEqualToString:@""]) {
NSLog(@"CONTENTS BLANK");
} else {
NSLog(@"Contents were %@", input);
NSLog(@"goButton: was called");
NSError *error;
[context save:&error];
}
}
@end
感谢帮助人员。我认为这将是一件简单的事情。
修改
应该更清楚我尝试过的事情/我遇到的问题。
我必须再次执行init的原因是,当我从文本字段添加新内容时,我可以从Core Data存储重新填充。如果我删除了第二个init
,那么当我添加一个新项目并尝试向下滚动到它时,我会收到SIGABRT错误。我相信这个错误是因为我将文本字段的内容添加到没有“名称密钥”的数组中。我不知道如何解决这个问题。我已经考虑过只为视图控制器创建一个工作数组来显示给用户,然后将此更新发送到Core Data存储。这是一个很好的解决方案还是有更好的方法?
答案 0 :(得分:2)
您已经init
方法viewDidLoad
使用了数据源,因此在返回单元格时不需要它。
我会完全删除它,看看你是否有任何问题。如果你的班级是正确的那么你是对的,每次调用它时都会浪费时间。
由于未在不调用init的情况下将其添加到数据源,因此只有在添加新对象时才能调用init。或者,更好的是,修复您的addItem
方法,使其无需通过添加此行重新初始化即可运行:
[self.dataArray addObject:input];