gpt4 book ai didi

iphone - 无法在我的 tableView 中使用我的 NSString 对象

转载 作者:行者123 更新时间:2023-12-01 17:00:50 25 4
gpt4 key购买 nike

抱歉标题中缺少细节,但我必须解释一下。

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;

代码给了我一个理由: '-[__NSCFType objectForKey:]: unrecognized selector sent to instance 0x6a19ee0'
事件标题给你的只是一段文字。我在我的数据源方法中使用了它,但由于某种原因在委托(delegate)中它不会让我使用它。

以下是字典的设置方式。请记住,这只是一个片段:
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;

编辑:

这是完整的类(class)。如果我要学习,我需要知道我在泄漏什么!
#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

我不知道我是否正确,但是在 Data Source 方法中,当我利用列表数组中的 Dictionary 对象时,我将它们分配给 rowData,然后释放它们。我是在释放实际的字典对象而不是它们的实例吗?因此,我在列表数组中什么都没有?如果是这样的话,我永远不会释放它们吗?

最佳答案

  • 当您不再拥有对象时,请正确解除分配对象。
  • 什么是 list ?需要保留还是分配更多细节。

  • 该错误表明您在无法识别的对象中调用了一个方法,因此这可能意味着您意外释放了该对象并在垃圾内存上调用了一个方法。

    关于iphone - 无法在我的 tableView 中使用我的 NSString 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126865/

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