所以我制作了一个运行良好的小应用程序,我只是不确定一些最佳实践/效率。该应用程序有一个加载到表视图中的核心数据源。可以从文本字段将项目添加到表格视图中。问题在于我的 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
电视数据源.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
电视数据源.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
,那么当我添加一个新项目并尝试向下滚动到它时,我会收到一个 SIGABRT 错误。我相信这个错误是因为我将文本字段的内容添加到没有“名称键”的数组中。我不知道如何解决这个问题。我考虑过为视图控制器创建一个工作数组以向用户显示,然后将此更新到核心数据存储。这是一个好的解决方案还是有更好的方法?