[英]Segue from TableViewCell NIB to ViewController - Pls have a look
After hours if trying to solve this error i decided to reach out for help. 数小时后,如果尝试解决此错误,我决定寻求帮助。 I Keep on getting the same error : 我不断遇到同样的错误:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'" “由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'***-[__ NSCFConstantString stringByAppendingString:]:无参数”
#import <UIKit/UIKit.h>
@interface BrowseTableViewController : UITableViewController
@property (strong) NSMutableArray *emp; //An array to store all the information of clicked entries
@end
#import <CoreData/CoreData.h>
#import "BrowseClickViewController.h"
#import "ItemTableViewCell.h"
@interface BrowseTableViewController ()<UITableViewDelegate, UITableViewDataSource>
@end
@implementation BrowseTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.tableView registerNib:[UINib nibWithNibName:@"ItemTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell1"];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[_emp removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadData];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Employee"];
self.emp = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; // Obtaining all the entries of persistance store to show in Table view
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.emp.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell1";
ItemTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *empp = [self.emp objectAtIndex:indexPath.row]; //obtaining the informatio of selected row in empp object
[cell.itemName setText:[NSString stringWithFormat:@"%@", [empp valueForKey:@"name"]]];
[cell.quantityLabel setText:[NSString stringWithFormat:@"%@", [empp valueForKey:@"quantity"]]];
[cell.shopLabel setText:[NSString stringWithFormat:@"%@", [empp valueForKey:@"store"]]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
BrowseClickViewController *initViewController = [storyBoard instantiateViewControllerWithIdentifier:@"Click"];
[self.navigationController presentViewController:initViewController animated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ //Segue which is called when user selects any row, and this segue modifies the value of emp object of BrowseClickViewController with the clicked row details.
if ([segue.identifier isEqualToString:@"recordClicked"]){
NSManagedObject *selectedEmp = [self.emp objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
BrowseClickViewController *destViewController = segue.destinationViewController;
destViewController.emp = selectedEmp;
}
}
@end
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface BrowseClickViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;
@property (weak, nonatomic) IBOutlet UILabel *barcodeLabel;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *quantityLabel;
@property (weak, nonatomic) IBOutlet UILabel *storeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imagePicture;
@property (strong) NSManagedObject *emp;
@property (weak, nonatomic) NSString *picLabel;
@end
#import "BrowseClickViewController.h"
#import "BrowseTableViewController.h"
#import <CoreData/CoreData.h>
#import "ItemTableViewCell.h"
@interface BrowseClickViewController ()
@end
@implementation BrowseClickViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
_nameLabel.text=[@"Name:" stringByAppendingString:[self.emp valueForKey:@"name"]];
_barcodeLabel.text=[@"Barcode:" stringByAppendingString:[self.emp valueForKey:@"barcode"]];
_categoryLabel.text=[@"Category:" stringByAppendingString:[self.emp valueForKey:@"category"]];
_priceLabel.text=[@"Price:"stringByAppendingString:[self.emp valueForKey:@"price"]];
_quantityLabel.text=[@"Quantity:" stringByAppendingString:[self.emp valueForKey:@"quantity"]];
_storeLabel.text=[@"Store:" stringByAppendingString:[self.emp valueForKey:@"store"]];
_picLabel=[self.emp valueForKey:@"pic"];
_imagePicture.image=[self loadImage:_picLabel];
}
-(UIImage *)loadImage: (NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:name]];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface ItemTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *itemName;
@property (nonatomic, weak) IBOutlet UILabel *quantityLabel;
@property (nonatomic, weak) IBOutlet UILabel *shopLabel;
@end
#import "ItemTableViewCell.h"
#import "BrowseClickViewController.h"
@implementation ItemTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
BrowseClickViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
}
@end
Most possibly , reason is that some of your appended strings values are nil.Do defensive coding.Check the below code sample and change all your code accordingly. 原因很可能是您的某些附加字符串值为nil。进行防御性编码。检查以下代码示例,并相应地更改所有代码。
if([self.emp valueForKey:@"name"])
{
_nameLabel.text=[@"Name:" stringByAppendingString:[self.emp valueForKey:@"name"]];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.