gpt4 book ai didi

ios - UITableView 拒绝转到DetailViewController

转载 作者:行者123 更新时间:2023-11-29 04:19:16 24 4
gpt4 key购买 nike

编辑:此问题已移至 Getting an error from Xcode that is not on the internet related to index path因为这个问题已得到解决,但出现了新的错误。

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

ViewController.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

DetailViewController.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];
}

我还添加了一张图片来显示我的连接: enter image description here

最佳答案

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

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

enter image description here

2) Player 类发生了什么?这是一个扩展 UITableViewCell 的类吗?当您单击 Storyboard中的原型(prototype)单元时,身份检查器中会列出什么类?是 Player 还是 UITableViewCell?属性检查器中单元格的标识符是什么?它是 Player 还是空白(根据您的代码,它需要是“Player”)?尝试将 Player 类(至少暂时)排除在外。保留单元格的标识符为 Player,但在身份检查器中,将原型(prototype)单元格的自定义类设置为 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的成员视为NSString。他们不可能两者兼而有之。

3) 摆脱 tableView:didSelectRowAtIndexPath: 方法。这个很重要。这里有两种相互竞争的 Segue 方法。一个是在 Storyboard 和这个方法中设置的。按照现在编写代码的方式,单击单元格将调用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's Table View Programming Guide 。它将帮助您理清生命周期和流程。

关于ios - UITableView 拒绝转到DetailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com