UITableView 崩溃且没有错误

发布于 2024-12-04 17:21:21 字数 4635 浏览 2 评论 0原文

基本上,我从导航控制器的根(表视图)构建一个 URL 字符串,其中包含我想要在新 tableView 上显示的信息。在新的表视图中,我使用 NSURL 请求的 URL。我的代码没有给我任何错误,但在 NSLog: 创建单元之后崩溃了。在我自杀之前帮忙。抱歉,应该全部放在一起,但复制粘贴代码显然效果不佳。

#import "CatDisplayViewController.h"
#import "CatViewController.h"


implementation CatDisplayViewController

@synthesize downvotebutton;
@synthesize upvotebutton;
//@synthesize tripsHold;
@synthesize URLtoUse;






NSArray *trips;


- (void)viewDidLoad {
[super viewDidLoad];



NSURL *url = [NSURL URLWithString:URLtoUse];

NSLog(@"%@", url);

responseData = [[NSMutableData data] retain];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@" recieved response ");
[responseData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@" Did recieve data ");
[responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@" connection failed ");
NSLog(@"%@", error);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(@" our connect finished loading");
NSString *responseString = [[NSString alloc] initWithData:responseData     encoding:NSUTF8StringEncoding];
[responseData release];

NSLog(@"%@", responseString);

NSLog(@" released data ");
NSDictionary *results = [responseString JSONValue];
NSLog(@" built JSON dictionary ");
NSArray *allTrips = [results objectForKey:@"results"];
NSLog(@" built array from dictionary ");

trips = allTrips;
NSLog(@" done with connectFinish method ");

[self.tableView reloadData];

}

#pragma mark -
#pragma mark Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.

NSLog(@" in sections number ");

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"%@", trips);
return [trips count];

}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@" in height for row ");
return 80;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" Trying to build table ");
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
NSLog(@"made cells");

NSDictionary *trip = [trips objectAtIndex:[indexPath row]];

NSLog(@" made dictionary from array ");

cell.textLabel.text = [trip objectForKey:@"txt"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.detailTextLabel.text = [trip objectForKey:@"name"];

UIButton *upvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];

UIImage *upVoteBack = [UIImage imageNamed:@"arrowup.png"];
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal];
[upvote setTitle:@"+" forState:UIControlStateNormal];
upvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[upvote addTarget:self action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:upvote];

UIButton *downvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *downVoteBack = [UIImage imageNamed:@"arrowdown.png"];
[upvote setBackgroundImage:downVoteBack forState:UIControlStateNormal];
[downvote setTitle:@"-" forState:UIControlStateNormal];
downvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[downvote addTarget:self action:@selector(downvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:downvote];



//NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"profile_image_url"]];
//NSData *data = [NSData dataWithContentsOfURL:url];
//cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

Basically, from the root of the navigation controller, a table view, I build a URL string that contains the information i want to display on the new tableView. in the new table view i use the URL for an NSURL request. My code gives me no error but crashes, right after the NSLog: made cells. help before i kill myself. sorry should all be together but copy pasting code obviously doesn't work well.

#import "CatDisplayViewController.h"
#import "CatViewController.h"


implementation CatDisplayViewController

@synthesize downvotebutton;
@synthesize upvotebutton;
//@synthesize tripsHold;
@synthesize URLtoUse;






NSArray *trips;


- (void)viewDidLoad {
[super viewDidLoad];



NSURL *url = [NSURL URLWithString:URLtoUse];

NSLog(@"%@", url);

responseData = [[NSMutableData data] retain];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@" recieved response ");
[responseData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@" Did recieve data ");
[responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@" connection failed ");
NSLog(@"%@", error);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(@" our connect finished loading");
NSString *responseString = [[NSString alloc] initWithData:responseData     encoding:NSUTF8StringEncoding];
[responseData release];

NSLog(@"%@", responseString);

NSLog(@" released data ");
NSDictionary *results = [responseString JSONValue];
NSLog(@" built JSON dictionary ");
NSArray *allTrips = [results objectForKey:@"results"];
NSLog(@" built array from dictionary ");

trips = allTrips;
NSLog(@" done with connectFinish method ");

[self.tableView reloadData];

}

#pragma mark -
#pragma mark Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.

NSLog(@" in sections number ");

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"%@", trips);
return [trips count];

}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@" in height for row ");
return 80;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" Trying to build table ");
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
NSLog(@"made cells");

NSDictionary *trip = [trips objectAtIndex:[indexPath row]];

NSLog(@" made dictionary from array ");

cell.textLabel.text = [trip objectForKey:@"txt"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.detailTextLabel.text = [trip objectForKey:@"name"];

UIButton *upvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];

UIImage *upVoteBack = [UIImage imageNamed:@"arrowup.png"];
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal];
[upvote setTitle:@"+" forState:UIControlStateNormal];
upvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[upvote addTarget:self action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:upvote];

UIButton *downvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *downVoteBack = [UIImage imageNamed:@"arrowdown.png"];
[upvote setBackgroundImage:downVoteBack forState:UIControlStateNormal];
[downvote setTitle:@"-" forState:UIControlStateNormal];
downvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[downvote addTarget:self action:@selector(downvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:downvote];



//NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"profile_image_url"]];
//NSData *data = [NSData dataWithContentsOfURL:url];
//cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-12-11 17:21:21
trips = [allTrips retain];

您的 trips 数组正在自动释放 - 尝试保留它并查看是否仍然收到错误。

trips = [allTrips retain];

Your trips array is being autoreleased - try retaining it and see if you still get the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文