0

当我在 tableView 中点击一个元素时,它会出现错误。我已经检查了所有的连接,但仍然无法解决。这是我的代码

#import "CGViewController.h"

#import "CGAppDelegate.h"

#import "sls.h"

#import "patientController.h"

@interface CGViewController ()

@end

@implementation CGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Patients";
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    patients = delegate.patients;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    self.navigationItem.leftBarButtonItem = addButtonItem;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark UITableViewDataSource Methods

- (UITableViewCell *)tableView:(UITableView *)tv
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
    if( nil == cell) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:@"cell"];
    }
    if (indexPath.row < patients.count) {
        sls *thissls = [patients objectAtIndex:indexPath.row];
       cell.textLabel.text = thissls.patientName;
    }
        else {
            cell.textLabel.text = @"";
            cell.textLabel.textColor = [UIColor lightGrayColor];
        }
        return cell;
}


-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    if ( editing != self.editing) {
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:animated];

        NSArray *indexes =
        [NSArray arrayWithObject:
         [NSIndexPath indexPathForRow:patients.count inSection:0]];
        if ( editing == YES ) {
            [self.tableView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
          }  else {
              [self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
            }
    }

    [self.tableView reloadData];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < patients.count ) {
        return UITableViewCellEditingStyleDelete;
    }
    else {
        return UITableViewCellEditingStyleNone;
    }
}

- (NSInteger)tableView:
(UITableView *)tv numberOfRowsInSection:
(NSInteger)section {
    NSInteger count = patients.count;
    if (self.editing) {
        count = count + 1;
    }
    return count;
}

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    patientController *patient = [[patientController alloc] init];
    [delegate.navController pushViewController:patient animated: YES];

    [tv deselectRowAtIndexPath:indexPath animated: YES];
}
-(void) tableView:(UITableView *)tv
commitEditingStyle:(UITableViewCellEditingStyle)editing forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editing == UITableViewCellEditingStyleDelete) {
        [patients removeObjectAtIndex:indexPath.row];
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                  withRowAnimation:UITableViewRowAnimationLeft];
    }
}
@end

如果您知道有什么问题的任何想法,请您尽快提供帮助,因为我在截止日期前

提前致谢。

错误说Thread 1:signal SIGABRT

错误在 main.m

#import <UIKit/UIKit.h>

#import "CGAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([CGAppDelegate class]));
    }
}

它说错误在这里

patientController *patient = [[patientController alloc] init];

这是我的 patientController.h 文件

#import <UIKit/UIKit.h>

@interface patientController : UIViewController {
    NSIndexPath *index;

    IBOutlet UIImageView * pictureView;
    IBOutlet UILabel * descriptionView1;
    IBOutlet UILabel * descriptionView2;
    IBOutlet UILabel * descriptionView3;
}

- (id)initwithIndexPath:(NSIndexPath *)indexPath;

@end

和我的 .m 文件

#import "patientController.h"

#import "CGAppDelegate.h"

#import "sls.h"

@interface patientController ()

@end

@implementation patientController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    sls *thissls = [delegate.patients objectAtIndex:index.row];
    self.title = thissls.patientName;
    self->descriptionView1.text = thissls.patientName;
    self->descriptionView2.text = thissls.surnameName;
    self->descriptionView3.text = thissls.dateOfBirth;
    self->pictureView.image = thissls.patientImage;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (id)initwithIndexPath:(NSIndexPath *)indexPath {
    if ( (self == [super init]) ) {
        index = indexPath;
    }
    return self;
}
@end

请任何人都可以帮助

4

4 回答 4

0

采用

 [self.navController pushViewController:patient animated: YES];

每个视图控制器本身都有其导航控制器引用,无需调用应用程序委托来调用它[导航控制器必须被初始化并且所有视图控制器必须被推入导航堆栈直到这个使其工作]

所以使用它来设置 appdelegate 中的导航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

//Note : this is an example set like this with your viewcontroller instance
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [self.window makeKeyAndVisible];
    return YES;
}
于 2013-06-15T08:28:58.127 回答
0

如果您当前在 navigationController 中(您必须这样做才能推送新的 ViewController),那么您可以通过调用这些行来推送您的新控制器

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
   patientController *patient = [[patientController alloc] init];
   [self.navigationController pushViewController:patient animated: YES];
}
于 2013-06-15T08:32:25.467 回答
0

要检查引发异常的位置,在 Xcode 中,在断点导航选项卡中单击左下角的加号按钮并添加一个Exceptions Breakpoint. 然后从 Xcode 运行应用程序。下次引发异常时,就像您的情况一样,您的应用程序的执行将停止,Xcode 将指向引发异常的代码行。

无论如何,为了让您了解 navigationControllers 的工作原理:

UINavigationController 是一个 UIViewController,它控制一堆其他 UIViewController 实例。据说其他视图控制器位于“导航堆栈”中。导航堆栈中的顶部项目是视图控制器,您在给定时间在屏幕上看到其视图。您可以在导航堆栈上/从导航堆栈中推送(呈现)或弹出(关闭)视图控制器。

UINavigationController 用'rootViewController' 初始化,这将是堆栈上的第一个视图控制器,因此当您将navigationController 添加到屏幕上时,您会首先看到它(例如,通过将其设置为appDelegate 窗口的rootViewController)。导航堆栈上的每个 UIViewController 通过属性“navigationController”引用它的 navigationController。

在任何给定点,您都可以使用 UINavigationController 方法pushViewController:animated:popViewControllerAnimated:/或从 navigationStack 添加或删除 viewController popToViewController:animated:popToRootViewControllerAnimated:当然,要查看此方法的结果,您应该在屏幕上显示 navigationController。

有关更多信息,请参阅UINavigationController 类参考

于 2013-06-15T08:45:49.120 回答
0

采用:

[self.navigationController pushViewController:patient animated: YES];
于 2013-06-30T14:44:37.630 回答