Iphone 无法在我的tableView中使用我的NSString对象
很抱歉标题中缺少细节,但这是我必须解释的Iphone 无法在我的tableView中使用我的NSString对象,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,很抱歉标题中缺少细节,但这是我必须解释的 NSUInteger row = [indexPath row]; NSDictionary *selectedEvent = [list objectAtIndex:row]; NSString *eventTitle = [selectedEvent objectForKey:@"Title"]; NSString *detailMessage = [[NSString alloc] initWithFormat: @"Yo
NSUInteger row = [indexPath row];
NSDictionary *selectedEvent = [list objectAtIndex:row];
NSString *eventTitle = [selectedEvent objectForKey:@"Title"];
NSString *detailMessage = [[NSString alloc] initWithFormat: @"You pressed the button for %@.", eventTitle];
childController.message = detailMessage;
childController.title = eventTitle;
代码给出了一个原因:'-[\uu NSCFType objectForKey:]:无法识别的选择器发送到实例0x6a19ee0'
事件标题给你的只是一段文字。我在数据源方法中使用过它,但由于某些原因,在委托中它不允许我使用它
以下是字典的设置方式。请记住,这只是一个片段:
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys: poster, @"Poster", @"The Addam's Family", @"Title", @"Dancap Productions", @"PresentedBy", @"The weird and wonderful family comes to devilishly delightful life in THE ADDAMS FAMILY.", @"Description", nil];
NSArray *array = [[NSArray alloc] initWithObjects: row1, nil];
self.list = array;
编辑:
这是全班同学。如果我想学习的话,我需要知道我在泄露什么
#import <UIKit/UIKit.h>
#import "SecondLevelViewController.h"
@class EventsDetailController;
#define kPosterValueTag 1
#define kTitleValueTag 2
#define kPresentedValueTag 3
#define kDescriptionValueTag 4
@interface EventsButtonController : SecondLevelViewController {
NSArray *list; //array holding all of the events
EventsDetailController *childController;
NSString *theatreType;
}
@property (nonatomic, retain) NSArray *list;
@property (nonatomic, retain) NSString *theatreType;
@end
#import "EventsButtonController.h"
#import "TCA_BaseAppDelegate.h";
#import "EventsDetailController.h";
@implementation EventsButtonController
@synthesize list;
@synthesize theatreType;
-(void)viewDidLoad{
//table heading
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 360, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 360, 60)] autorelease];
headerLabel.text = NSLocalizedString(@"Header for the table", @"");
headerLabel.textColor = [UIColor blackColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor redColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
//end table heading
//poster image
UIImage *poster = [UIImage imageNamed:@"test_poster.png"];
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys: poster, @"Poster", @"The Addam's Family", @"Title", @"Dancap Productions", @"PresentedBy", @"The weird and wonderful family comes to devilishly delightful life in THE ADDAMS FAMILY.", @"Description", nil];
NSArray *array = [[NSArray alloc] initWithObjects: row1, nil];
self.list = array;
[row1 release];
[array release];
[super viewDidLoad];
}
-(void)viewDidUnload{
self.list = nil;
[childController release], childController = nil;
}
-(void)dealloc{
[list release];
[childController release];
[super dealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *DisclosureButtonCellIdentifier = @"DisclosureButtonCellIdentifier"; //set identifier for the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: DisclosureButtonCellIdentifier]; //set the identifier for dequeue ie. cells hidden from the screen
NSUInteger row = [indexPath row]; //put this stuff first so we can retrieve the size of the text
NSDictionary *rowData = [list objectAtIndex:row];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:DisclosureButtonCellIdentifier] autorelease];
//start adding custom subviews to the table cell
//add subview for the poster
UIImageView *posterValue = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,105,175)];
posterValue.tag = kPosterValueTag;
[cell.contentView addSubview:posterValue];
[posterValue release];
//addsubview for the Title
UILabel *titleValue = [[UILabel alloc] initWithFrame:CGRectMake(125,10,185,30)];
titleValue.tag = kTitleValueTag;
titleValue.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:titleValue];
[titleValue release];
//addSubview for the PresentedBy
UILabel *presentedValue = [[UILabel alloc] initWithFrame:CGRectMake(125,30,185,30)];
presentedValue.tag = kPresentedValueTag;
presentedValue.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:presentedValue];
[presentedValue release];
//addSubview for Description
UILabel *descValue = [[UILabel alloc] init];
NSString *descString = [rowData objectForKey:@"Description"];
CGSize maximumSize = CGSizeMake(185, 140);
UIFont *descFont = [UIFont fontWithName:@"Helvetica" size:12];
CGSize descStringSize = [descString sizeWithFont:descFont
constrainedToSize:maximumSize
lineBreakMode:descValue.lineBreakMode];
CGRect descFrame = CGRectMake(125, 60, 185, descStringSize.height);
descValue.frame = descFrame;
//descValue.backgroundColor = [UIColor redColor];
descValue.font = descFont;
descValue.tag = kDescriptionValueTag;
descValue.lineBreakMode = UILineBreakModeWordWrap;
descValue.numberOfLines = 0;
[cell.contentView addSubview:descValue];
[descValue release];
}
//add content
UIImageView *poster = (UIImageView *)[cell.contentView viewWithTag:kPosterValueTag];
poster.image = [rowData objectForKey:@"Poster"];
UILabel *title = (UILabel *)[cell.contentView viewWithTag:kTitleValueTag];
title.text = [rowData objectForKey:@"Title"];
UILabel *presented = (UILabel *)[cell.contentView viewWithTag:kPresentedValueTag];
presented.text = [rowData objectForKey:@"PresentedBy"];
UILabel *desc = (UILabel *)[cell.contentView viewWithTag:kDescriptionValueTag];
desc.text = [rowData objectForKey:@"Description"];
//add accessoryType for chevron icon.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; //disables the blue highlight when the cell is selected
[rowData release];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//makes the next drill down's button have the label of "back"
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
if(childController == nil){
childController = [[EventsDetailController alloc] initWithNibName: @"EventsDetail" bundle:nil];
}
childController.title = @"Disclosure Button Pressed";
NSUInteger row = [indexPath row];
NSDictionary *selectedEvent = [list objectAtIndex:row];
NSString *eventTitle = [selectedEvent objectForKey:@"Title"];
NSString *detailMessage = [[NSString alloc] initWithFormat: @"You pressed the disclosure button for %@.", eventTitle];
childController.message = detailMessage;
childController.title = eventTitle;
[detailMessage release];
[self.navigationController pushViewController:childController animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 195;
}
@end
我不知道我是否正确,但是在数据源方法中,当我使用列表数组中的Dictionary对象时,我将它们分配给rowData,然后释放它们。我是否释放了实际的dictionary对象而不是它们的实例?因此,列表数组中没有任何内容?如果是这样的话,我不会释放他们吗
当您不再拥有对象时,请正确释放它们
什么是列表?需要保留或分配更多细节
该错误表示您在一个无法识别的对象中调用了一个方法,因此这可能意味着您意外地释放了该对象,并在垃圾内存中调用了一个方法
当您不再拥有对象时,请正确释放它们
什么是列表?需要保留或分配更多细节
该错误表示您在一个无法识别的对象中调用了一个方法,因此这可能意味着您意外地释放了该对象,并在垃圾内存中调用了一个方法 在第一段代码的第2行和第3行之间插入这一行
if ([list isKindOfClass:[NSDictionary class]] {
NSLog(@"It's NSDictonary");
}
为了确保您实际获得了NSDictionary列表。在第一段代码的第2行和第3行之间插入这一行
if ([list isKindOfClass:[NSDictionary class]] {
NSLog(@"It's NSDictonary");
}
确保您实际将列表作为NSDictionary获取。我认为,当您在初始化childController后访问列表时,viewDidLoad可能尚未在self上运行,在这种情况下,列表仍然为零 由于在nil上调用方法只需静默处理,因此还不会出现运行时错误。但是selectedEvent变量仍然包含垃圾。现在,如果您调用[SelectedEventObjectForkey:row],您将在垃圾上调用它,这可能会导致运行时错误 尝试几件事: 使用NSLog 在viewDidLoad中添加NSLog条目,以指示调用了例程:
NSLog(@"viewDidLoad")
在访问列表之前,请检查列表是否为非零。使用
NSLog(@"self.list: %@", self.list);
选中selectedEvent:
这会告诉你发生了什么。如有必要,添加更多NSLog条目
调试
在访问self.list和ViewDidLoad的行上放置断点。检查变量、单步执行代码等。这会让您更深入地了解可能发生的情况
从这里我们看不到这样的东西- 这可能是因为,当您在初始化childController之后访问list时,viewDidLoad可能还没有在self上运行,在这种情况下,list仍然为零 由于在nil上调用方法只需静默处理,因此还不会出现运行时错误。但是selectedEvent变量仍然包含垃圾。现在,如果您调用[SelectedEventObjectForkey:row],您将在垃圾上调用它,这可能会导致运行时错误 尝试几件事: 使用NSLog 在viewDidLoad中添加NSLog条目,以指示调用了例程:
NSLog(@"viewDidLoad")
在访问列表之前,请检查列表是否为非零。使用
NSLog(@"self.list: %@", self.list);
选中selectedEvent:
这会告诉你发生了什么。如有必要,添加更多NSLog条目
调试
在访问self.list和ViewDidLoad的行上放置断点。检查变量、单步执行代码等。这会让您更深入地了解可能发生的情况
从这里我们看不到这样的东西- 查看我的编辑。我不认为这是相关的,因为在我的数据源方法中使用了完全相同的代码。Oh and list是一个实例数组,我使用了@property//synthetic。看起来肯定是内存管理问题,但从发布的代码中,我只看到可能的泄漏,不会导致崩溃。您可能应该使用self.list。查找任何修改self.list或list的代码,尤其是releasese我的编辑。我不认为这是相关的,因为在我的数据源方法中使用了完全相同的代码。Oh and list是一个实例数组,我使用了@property//synthetic。看起来肯定是内存管理问题,但从发布的代码中,我只看到可能的泄漏,不会导致崩溃。您可能应该使用self.list。查找任何修改self.list或list的代码releases@property非原子,保留NSArray*列表;这就是你的意思吗?请参阅上面的完整代码。感谢您致电objectForKey进行非NSDictionary检查、设置断点和调试。使用GDB检查NSArray和NSDictionary中的内容。PO将打印出对象,您可以对其进行检查。@property nonatomic,retain NSArray*l
ist;这就是你的意思吗?请参阅上面的完整代码。感谢您致电objectForKey进行非NSDictionary检查、设置断点和调试。使用GDB检查NSArray和NSDictionary中的内容。PO将打印出对象,您可以检查它。好吧,它不是NSDictionary,您正在尝试执行“[SelectedEventObjectForkey:@Title]”这就是错误所在。在调用objectForKey之前,请确保您确实获得了NSDictionary:。嗯,它不是NSDictionary,您正在尝试执行“[selectedEvent objectForKey:@Title]”这就是错误所在。在调用objectForKey:之前,请确保您确实获得了NSDictionary:您能看一下最后一段吗?我删除了rowData版本,它成功了。但我不确定这是否是错的。我假设既然rowData在列表中,列表被发布了,那么所有得到的东西都被发布了。你能看一下最后一段吗?我删除了rowData版本,它成功了。但我不确定这是否是错的。我假设因为rowData在列表中,列表被释放,所以所有得到的东西都被释放了。