ios - didSelectRowAtIndexPath 在调试器中没有任何错误

标签 ios objective-c xcode

我是 xcode 的新手,所以我在这里学习,我可能完全错了,但这是我的场景。我有一个打开 View Controller 的 Storyboard,带有一个转到导航 Controller 的按钮,它使用 TableView 从 mysql 数据库加载我的娱乐列表(这工作正常)它显示的 TableView 中的一个项目转到详细信息 View ,但没有任何反应,调试器中没有错误。我做错了什么或者我以错误的方式解决了这个问题。我认为问题与 ENTERTAILMENTLISTING.M 中 didSelectRowAtIndexPath 内容所在的部分有关,我已将到目前为止的所有内容包含在下面。

Storyboard enter image description here

APPDELEGATE.H

//
//  AppDelegate.h


#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

APPDELEGATE.M

//
//  AppDelegate.m


#import "AppDelegate.h"


@implementation AppDelegate





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

    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

娱乐列表.H

//
//  EntertainmentListingViewController.h


#import <UIKit/UIKit.h>

@interface EntertainmentListingViewController : UIViewController{

    IBOutlet UITableView *mainTableView;
    NSArray *events;
    NSMutableData *data;


}


@end

ENTERTAINMENTLISTING.M

//
//  EntertainmentListingViewController.m


#import "EntertainmentListingViewController.h"
#import "EntertainmentDetailsViewController.h"

@interface EntertainmentListingViewController ()

@end

@implementation EntertainmentListingViewController

- (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.
    // VIEW TITLE
    self.title = @"Entertainment";

    // SHOW NETWORK ACTIVITY INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //GET DATA URL
    NSURL *url = [NSURL URLWithString:@"http://mydomain.com/myfile.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

//START CODE FOR TALBE VIEW

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //PROCESS JSON DATA HERE
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    events = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    [mainTableView reloadData];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // WASNT ABLE TO CONNECT INTERNET THROW ERROR

    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Connection To The Internet Is Available" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    // TURN OFF NETWORk ACTIVITY INDICATOR
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}


- (int) numberOfSectionsInTableView: (UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;

}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [events count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)IndexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"MainCell"];



    }



    NSURL *url = [NSURL URLWithString: [[events objectAtIndex:IndexPath.row] objectForKey:@"eImg"]];
    NSData *idata = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:idata];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    cell.imageView.image = imgView.image;


    cell.textLabel.text = [[events objectAtIndex:IndexPath.row] objectForKey:@"eName"];
    cell.detailTextLabel.text = [[events objectAtIndex:IndexPath.row] objectForKey:@"date_string"];
    return cell;
}





-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    EntertainmentDetailsViewController *entertainmentdetailsViewController = [[EntertainmentDetailsViewController alloc]
    initWithNibName:@"EntertainmentDetailsViewController" bundle:nil];
    entertainmentdetailsViewController.title = [[events objectAtIndex:indexPath.row] objectForKey:@"eName"];
    entertainmentdetailsViewController.entertainmentArticle = [events objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:entertainmentdetailsViewController animated:YES];
    // NOTHING HAPPENING HAS TO DO WITH THIS AREA I THINK
    NSLog(@"Navigation Cnntroller %@",self.navigationController);
    NSLog(@"Events COntroller %@", entertainmentdetailsViewController);

}


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

@end

ENTERTAINMENTDETAILS.H

//
//  EntertainmentDetailsViewController.h


#import <UIKit/UIKit.h>

@interface EntertainmentDetailsViewController : UIViewController{

    NSDictionary *entertainmentArticle;

    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *timeLabel;
    IBOutlet UITextView *descTextView;
}

@property (nonatomic, copy) NSDictionary *entertainmentArticle;

@end

ENTERTAINMENTDETAILS.M

//
//  EntertainmentDetailsViewController.m


#import "EntertainmentDetailsViewController.h"

@interface EntertainmentDetailsViewController ()

@end

@implementation EntertainmentDetailsViewController
@synthesize entertainmentArticle;

- (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.
    titleLabel.text = [entertainmentArticle objectForKey:@"eName"];
    timeLabel.text = [entertainmentArticle objectForKey:@"date_string"];
    descTextView.text = [entertainmentArticle objectForKey:@"eDetails"];
}

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

@end

最佳答案

您忘记了委托(delegate)和数据源。如果你使用一个 uitableviewcontroller,所有的都被连接起来,如果你将一个 tableview 拖到 uiviewcontroller 你必须做几件事:

编辑这一行:

@interface EntertainmentListingViewController : UIViewController  <UITableViewDataSource, UITableViewDelegate>

ctrl+将您的表格拖到 View Controller 下方的黄色框中。设置为代理,再次拖动并设置为源。

关于ios - didSelectRowAtIndexPath 在调试器中没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871294/

相关文章:

ios - 如何用Parse解决Swift中 "no results matched the query"的错误

ios - 在不同部分的索引处选择行

ios - UIScrollView contentsize 在使用自动布局时重置为零

iphone - 模拟器、设备和应用商店的行为测试有什么区别

ios - [可能重复]:体系结构i386的 undefined symbol :

ios - 指向框架中泄漏的仪器——发现泄漏在其他地方(为什么?)

iphone - 带按钮的 UIscrollview 中的动画

ios - 从 Xcode 打印的字典日志创建 NSDictionary

iphone - 获取 writeToFile :atomically: URL saved to

ios - 如果我安装 xcode 6,它会取代我的旧 Xcode 和其他问题吗