0

编辑:此问题已移至从 Xcode 获取与索引路径无关的错误,因为此问题已修复但出现新错误。

我已经在这里待了4天!我在日志中没有收到任何错误,但是每当我单击填充有从 mysql 下载的动态数组的单元格时,它都不会转换。有趣的是,我没有从 prepareForSegue 中的自我放置的 nslog 中得到任何回应......

视图控制器.h

   #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController<UITableViewDataSource,
 CLLocationManagerDelegate,NSXMLParserDelegate, UISearchBarDelegate> {
    IBOutlet UISearchBar *searchBar;
    IBOutlet UITableView *tableView;
     IBOutlet UILabel *Label;


    CLLocationManager *locationManager;

    NSMutableArray *coolarray;

    float latitude;
    float longitude;
}



@property (nonatomic, retain) CLLocationManager *locationManager;


@end

我已经排除了已解析的 xml 下载数据,但将 nsarray 的值留在了我的实现文件中

   #import "ViewController.h"
    #import "DetailViewController.h"
    #import "Player.h"

    @interface ViewController ()

    @end

    @implementation ViewController



    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (void)dealloc
    {
        [super dealloc];
        [self.locationManager release];
        if ( coolarray )
            [coolarray release];
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        coolarray = NULL;

        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        [self.locationManager startUpdatingLocation];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }

    // Table data delegate
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        if ( coolarray != NULL ) {
            return [coolarray count];
        }
        return 0;
    }

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            Player *cell = [tableView dequeueReusableCellWithIdentifier:@"Player"];

        if (cell == nil)
        {
            cell = [[[Player alloc] initWithFrame:CGRectZero reuseIdentifier:@"Player"] autorelease];
        }


        NSDictionary *itemAtIndex = (NSDictionary *)[coolarray objectAtIndex:indexPath.row];
        UILabel *nameLabel =[cell textLabel];
        [nameLabel setText:[itemAtIndex objectForKey:@"name"]];
        return cell;
    }
                - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                        Player *cell = [tableView dequeueReusableCellWithIdentifier:@"Player"];

                    if (cell == nil)
                    {
                        cell = [[[Player alloc] initWithFrame:CGRectZero reuseIdentifier:@"Player"] autorelease];
                    }


                    NSDictionary *itemAtIndex = (NSDictionary *)[coolarray objectAtIndex:indexPath.row];
                    UILabel *nameLabel =[cell textLabel];
                    [nameLabel setText:[itemAtIndex objectForKey:@"name"]];
                    return cell;
                }

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

            NSString *selectedLang = [coolarray objectAtIndex:indexPath.row];


            DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 

            myDetViewCont.myDogLang = selectedLang; 
            [self  performSegueWithIdentifier:@"showDogDetail" sender:nil];

            [myDetViewCont release]; 
            myDetViewCont = nil;

        }

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ 
    if ([[segue identifier] isEqualToString:@"showDogDetail"])
    {
        DetailViewController *detailViewController =
        [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView
                                    indexPathForSelectedRow];
        NSString *dogLang = [self.coolarray objectAtIndex:indexPath.row];
        detailViewController.myDogLang = dogLang;
                    }

            }
@end

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
 {

    IBOutlet UILabel *myLabel;

    NSString *myDogLang;

}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@property (nonatomic, retain) NSString *myDogLang;

@end

细节视图控制器.m

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize myLabel;
@synthesize myDogLang;

- (void)viewDidLoad {
    [super viewDidLoad];
    myLabel.text = myDogLang;
        self.myLabel.text = [self.myLabel objectAtIndex:0];
}

- (void)dealloc {
    [myLabel release];
    [myDogLang release];
    [super dealloc];
}

我还添加了一张图片来显示我的连接: 在此处输入图像描述

4

2 回答 2

1

看起来您在这里混合和匹配了几种不同的样式...我建议简化您的代码:

1)删除表和详细视图之间的当前segue。通过按住控制键并从原型单元格(我假设这是一个原型表)拖动到目标视图来创建一个新的。不要忘记将标识符命名为与您在 prepareForSegue 代码中使用的名称相同的名称)。此外,如果您将表视图控制器嵌入到导航控制器中(这是通常在可导航表视图上所期望的),则 segue 的样式应该是“推送”。否则样式应该是“模态”(不过,根据您的应用程序的可用性,您可能需要重新考虑这种方法)。

在此处输入图像描述

2) Player 类发生了什么?这是一个扩展 UITableViewCell 的类吗?当您单击情节提要中的原型单元格时,身份检查器中列出了什么类?是 Player 还是 UITableViewCell?属性检查器中单元格的标识符是什么?它是 Player 还是空白(给定您的代码,它需要是“Player”)?尝试至少暂时将 Player 课程排除在外。将单元格的标识符保持为Player,但在身份检查器中,将原型单元格的自定义类设置为UITableViewCell。将您的方法更改tableView:cellForRowAtIndexPath:为:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Player"];

    NSDictionary *itemAtIndex = (NSDictionary *)[self.coolarray objectAtIndex:indexPath.row];
    cell.textLabel.text = [itemAtIndex objectForKey:@"name"];
    return cell;
}

另外作为旁注,您在此方法中表明coolarray 的成员是 NSDictionary 类型。在prepareForSegue 中,您将coolarray 的成员视为NSStrings。他们不可能两者兼有。

3)摆脱tableView:didSelectRowAtIndexPath:方法。这个很重要。这里发生了两种相互竞争的 segues 方法。在情节提要和此方法中设置的一个。现在编写代码的方式,单击单元格将调用 prepareForSegue -> didSelect -> prepareForSegue。您在此处尝试执行的所有操作都可以在该prepareForSegue:方法中完成(请参阅下一步)。

4)改变你的 prepareForSegue: 方法看起来像这样(你甚至可以摆脱“if”语句,因为你现在只有一个 segue):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ 
    if ([[segue identifier] isEqualToString:@"showDogDetail"])
    {
        DetailViewController *detailViewController =
        [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView
                                    indexPathForSelectedRow];
        NSString *dogLang = [self.coolarray objectAtIndex:indexPath.row];
        // or if the array contains NSDictionaries, change the previous line to:
        // NSString *dogLang = [[self.coolarray objectAtIndex:indexPath.row] objectForKey:@"name"];
        detailViewController.myDogLang = dogLang;
    }
}

5)您的 DetailViewController 的 viewDidLoad 方法正在用一些错误代码覆盖标签文本(UILabel 不是数组)。将代码更改为:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myLabel.text = self.myDogLang;
}

鉴于我上面提到的一些概念性问题,我强烈建议阅读Apple 的 Table View Programming Guide。它将帮助您阐明生命周期和流程。

于 2012-11-03T05:02:44.450 回答
0

1-您应该选择要使用的方法:在情节提要内使用连接或以编程方式加载的详细信息控制器(使用 didSelectRowAtIndexPath)。现在你正在做两件事:不好。尝试评论您所有的 didSelectRowAtIndexPath 代码并仅使用情节提要。此外,在您的 didSelectRowAtIndexPath 中,您正在从笔尖加载详细信息 vc ???哪个笔尖?你没有笔尖,你有一个故事板!(您可以以编程方式从情节提要中加载内容,但代码不同)

2-如果您使用 Push 进行转场,您的故事板中必须有一个 UINavigationController。放置导航控制器,当您将其放置在情节提要中时,它附带了一个 UITableViewController。删除此 UITV 并附加您的。将单元重新连接到详细控制器并记住为连接提供标识符名称,确保连接从单元开始。我不确定您是否需要 UINavigationController 即使是模态转场。

试着让我知道

于 2012-11-03T12:53:02.373 回答